开发者

powershell pass system.object[] without unrolling

开发者 https://www.devze.com 2023-01-16 03:44 出处:网络
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

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)
$psCallS开发者_如何学PythontackType = $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)


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


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消