且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

传递给函数的 Powershell 参数似乎不起作用

更新时间:2023-11-12 09:42:10

PowerShell 中的函数的调用方式类似于 cmdlet,因此您无需使用逗号分隔参数.

Functions in PowerShell are called similar to cmdlets, so you don't need to separate arguments with commas.

您的调用可能如下所示:

Your invocation likely looks like this:

getPropertyOfFile($foo, $bar, $baz)

这导致 $a 具有值 $foo, $bar, $baz (一个数组)而 $b$c$null.

which results in $a having the value $foo, $bar, $baz (an array) while $b and $c are $null.

你需要这样称呼它:

getPropertyOfFile $foo $bar $baz

如前所述,这与您调用 cmdlet 的方式相同.你甚至可以这样做

which, as noted, is identical to how you call cmdlets. You could even do

getPropertyOfFile -a $foo -c $baz -b $bar

此时您可能会注意到您的函数参数没有很好地命名;-)

at which point you probably notice that your function arguments aren't named very well ;-)

编辑:如前所述,您对函数的声明很好.问题出在您没有发布的代码中,但对于具有 PowerShell 经验的人来说很容易推断出来.即,您的函数的调用.

As noted before your declaration of the function is fine. The problem is in the code you didn't post but is easily inferrable for people with PowerShell experience. Namely, the invocation of your function.