关于如何在不展开Get-PSCallStack的情况下传递Get-PSCallStack的任何想法。它似乎是一个system.object [],但从我在网上阅读的内容来看,它们在传递和“展开”时并不完整。我尝试在前面放一个逗号以防止它,但这不起作用。
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)
答案 0 :(得分:6)
当你将一个参数传递给一个函数而没有将它传递给它时,就不会展开集合,例如。
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,您可以这样指定:
function Pass-Callstack([object[]]$array)
注意使用“系统”。名称空间前缀是可选的如果找不到类型,PowerShell将会添加前缀。此外,将参数指定为[object]本质上是一个无操作,因为这是默认类型。这是[object]$arg0
与$arg0
相同。
您还将$ null传入Pass-Callstack(尽管包含在单个元素数组中)。变量$ psCallStack对函数是私有的,除了你的前缀是$script:psCallStack
之类的修饰符之外,它在它之外是不可见的。一般来说,我不推荐这种方法。您应该从Describe-Callstack输出$ pscallstack,如下所示:
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
答案 1 :(得分:0)
定义一个数组并在括号内放置你想成为数组的内容。
如果返回null,则返回大小为1的数组,该值为空。
$ x = @(CallSomeMethodHere)
所以我想你会想要: $ x = @($ psCallStack)