Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell pass system.object[] without unrolling

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)
like image 1000
alphadev Avatar asked Sep 16 '26 21:09

alphadev


1 Answers

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
like image 134
Keith Hill Avatar answered Sep 19 '26 11:09

Keith Hill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!