powershell 通过 system.object[] 无需展开 [英] powershell pass system.object[] without unrolling
问题描述
关于如何在不展开 Get-PSCallStack 的情况下传递 Get-PSCallStack 的任何想法.它似乎是一个 system.object[] 但从我在网上阅读的内容来看,它们在通过和展开"时不会保持完整.我试着在前面放一个逗号来防止它,但没有用.
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)
推荐答案
当你将一个参数传递给一个函数而不用管道传递它时,不会展开集合,例如
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
此外,如果您希望参数数组传递给 Pass-Callstack,您可以像这样指定:
Also, if you are expecting an array for the parameter to Pass-Callstack you can specify that like so:
function Pass-Callstack([object[]]$array)
注意使用系统".命名空间前缀是可选的.如果 PowerShell 找不到类型,它将在前面加上.此外,将参数指定为 [object] 本质上是无操作的,因为这是默认类型.即 [object]$arg0
与 $arg0
相同.
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
.
您还将 $null 传入 Pass-Callstack(尽管包装在单个元素数组中).变量 $psCallStack 是该函数的私有变量,在函数外部不可见,除非您确实在它前面加上了像 $script:psCallStack
这样的修饰符.一般来说,我不推荐这种方法.您应该像这样从 Describe-Callstack 输出 $pscallstack:
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
并将其传递到 Pass-Callstack 例如:
And pass that into Pass-Callstack e.g.:
Pass-Callstack $cs
这篇关于powershell 通过 system.object[] 无需展开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!