【发布时间】:2014-11-13 14:51:18
【问题描述】:
我正在尝试使用 PowerShell 实现一些递归函数。这是基本功能:
function MyRecursiveFunction {
param(
[parameter(Mandatory=$true,ValueFromPipeline=$true)]
$input
)
if ($input -is [System.Array] -And $input.Length -eq 1) {
$input = $input[0]
}
if ($input -is [System.Array]) {
ForEach ($i in $input) {
$i | ##### HOW DO I USE $MyInvocation HERE TO CALL MyRecursiveFunction??? #####
}
return
}
# Do something with the single object...
}
我查看过 Invoke-Expression 和 Invoke-Item 但无法正确使用语法。比如我试过
$i | Invoke-Expression $MyInvocation.MyCommand.Name
我猜如果你知道正确的语法,有一个简单的方法可以做到这一点:-)
【问题讨论】:
-
我建议将
$input更改为另一个变量名。$input是 powershell 脚本中的保留和自动填充变量:stackoverflow.com/a/885651/520612 -
@CB。点了,谢谢。我实际上希望用我的变量隐藏自动变量,但回想起来,也许这不是一个好主意。
标签: powershell