powershell pass system.object[] without unrolling

这一生的挚爱 提交于 2019-12-22 09:47:20

问题


any ideas on how I can pass the Get-PSCallStack with out having it unroll. It appears to be a system.object[] but from what i have read online they don't remain intact when passed and " unroll ". I tried placing a comma in front to prevent it but that did not work.

function Pass-Callstack ([System.Object]$arg0) {
Write-Host 'Start Pass-Callstack'
$psCallStack = $arg0
$psCallStackType = $psCallStack.GetType()
$psCallStackLength = $psCallStack.Length
$psCallStackCommand0 = $psCallStack[0].command 
$psCallStackCommand1 = $psCallStack[1].command
Write-Host $psCallStackType
Write-Host $psCallStackLength
Write-Host $psCallStackCommand0
Write-Host $psCallStackCommand1
$arg0 | gm
}

function Describe-Callstack {
Write-Host 'Start Describe-Callstack'
$psCallStack = (Get-PSCallStack)
$psCallStackType = $psCallStack.GetType()
$psCallStackLength = $psCallStack.Length
$psCallStackCommand0 = $psCallStack[0].command 
$psCallStackCommand1 = $psCallStack[1].command
Write-Host $psCallStackType
Write-Host $psCallStackLength
Write-Host $psCallStackCommand0
Write-Host $psCallStackCommand1
$psCallStack | gm
}
Describe-Callstack
Pass-Callstack (,$psCallStack)

回答1:


When you pass an argument into a Function without piping it in there is no unrolling of collections e.g.

function ArgShape($p)
{
    $p.GetType().Fullname
    $p.Rank
    $p.Length
    $p[0].GetType().Fullname
}

ArgShape (Get-PSCallstack)

System.Object[]
1
2
System.Management.Automation.CallStackFrame

Also, if you are expecting an array for the parameter to Pass-Callstack you can specify that like so:

function Pass-Callstack([object[]]$array)

Note that the use of the "System." namespace prefix is optional. PowerShell will prepend that if it can't find the type. Also, to specify a parameter as [object] is essentially a no-op because that is the default type. That is [object]$arg0 is the same as $arg0.

You are also passing in $null into Pass-Callstack (albeit wrapped in a single element array). The variable $psCallStack is private to the function and not visible outside it unless you do prepend it with a modifier like $script:psCallStack. In general I don't recommend this approach. You should output $pscallstack from Describe-Callstack like so:

function Describe-Callstack { 
Write-Host 'Start Describe-Callstack' 
$psCallStack = (Get-PSCallStack) 
$psCallStackType = $psCallStack.GetType() 
$psCallStackLength = $psCallStack.Length 
$psCallStackCommand0 = $psCallStack[0].command  
$psCallStackCommand1 = $psCallStack[1].command 
Write-Host $psCallStackType 
Write-Host $psCallStackLength 
Write-Host $psCallStackCommand0 
Write-Host $psCallStackCommand1 
$psCallStack 
}

Then assign the output of the function call to a variable:

$cs = Describe-Callstack 

And pass that into Pass-Callstack e.g.:

Pass-Callstack $cs



回答2:


Define an array and place what you want to be the array inside the parenthesis.

If null is returned, an array of size 1 is returned and the value is blank.

$x = @(CallSomeMethodHere)

So i think you would want: $x = @($psCallStack)



来源:https://stackoverflow.com/questions/3701831/powershell-pass-system-object-without-unrolling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!