Powershell pass system.object [] without a reversal

any ideas on how I can pass Get-PSCallStack without deploying it. It seems to be system.object [], but from what I read on the Internet, they do not remain unchanged during the transfer and "unfold". I tried putting a comma in front to prevent it, but that didn't 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)
+3
source share
2 answers

When you pass an argument to a function without its pipeline, there is no collection reversal in it, for example.

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

In addition, if you expect an array for the Pass-Callstack parameter, you can indicate that this is so:

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

, "". . PowerShell , . , [object] -op, . [object]$arg0 $arg0.

$null Pass-Callstack ( ). $psCallStack , , $script:psCallStack. , . $pscallstack Describe-Callstack :

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 
}

:

$cs = Describe-Callstack 

Pass-Callstack, :

Pass-Callstack $cs
+6

, .

null, 1 .

$x = @(CallSomeMethodHere)

, , : $ x = @($ psCallStack)

0

Source: https://habr.com/ru/post/1764528/


All Articles