[英]Can't seem to do a Function.Invoke with multiple parameters in powershell?
I'm trying to pass a function to a method and then pass parameters to the method I passed when calling it, but if I pass more than one parameter then the method fails with an error: 我试图将一个函数传递给一个方法,然后将参数传递给调用该函数时所传递的方法,但是如果我传递多个参数,则该方法将失败并显示错误:
function debugMeStuffs($someBlah, $somePoo) {
Write-Host $someBlah
Write-Host $somePoo
}
function invokeOnHosts ([scriptblock]$funcToCall, $param1, $param2, $startRange, $endRange) {
#Param($funcToCall)
$i = $startRange
for($i = [int]$startRange; $i -le $endRange; $i++) {
# HOW DO I MAKE THIS WORK WITH MULTIPLE PARAMETERS?!?!?!?
$funcToCall.Invoke('blah' 'poo')
}
}
invokeOnHosts $function:debugMeStuffs "param1" "param2" 4 7
Things I've tried: 我尝试过的事情:
$funcToCall("blah" "poo")
$funcToCall('blah' 'poo')
$funcToCall.Invoke("blah" "poo")
$funcToCall.Invoke('blah' 'poo')
$funcToCall 'blah' 'poo'
$funcToCall.Invoke 'blah' 'poo'
$funcToCall "blah" "poo"
$funcToCall.Invoke "blah" "poo"
None of the above seem to work. 以上似乎都不起作用。 Is there something else I need to do to make this work?
我还需要做其他事情才能使这项工作吗?
.Invoke()
is a .NET method , so the usual method-call syntax applies : you need .Invoke()
是.NET 方法 ,因此通常的方法调用语法适用 :您需要
(...)
- around the list of arguments (...)
-围绕参数列表 ,
,
$funcToCall.Invoke('blah', 'poo')
This contrasts with PowerShell's own syntax for calling cmdlets and functions , which is shell-like [1] : 这与PowerShell自己的调用cmdlet和函数的语法相反,后者类似于shell [1] :
no (...)
around the argument list 参数列表周围没有
(...)
arguments must be separated with spaces . 参数必须用空格分隔。
& $funcToCall blah poo # equivalent of the method call above.
A command such as the above is parsed in argument mode , which is why quoting the arguments in this simple case is optional . 在参数模式下解析上述命令,这就是为什么在这种简单情况下引用参数是可选的 。
Note the need for &
, PowerShell's call operator , which is needed to execute the script block stored in $funcToCall
; 注意需要
&
,PowerShell的调用运算符 , 执行存储在$funcToCall
的脚本块是必需的; this is generally necessary for invoking a command stored in a variable , and also for command names / paths that are quoted . 这通常对于调用存储在变量中的命令以及引用的命令名称/路径是必需的。
Given that it's easy to get confused between PowerShell's command syntax and .NET's method syntax, it's best to stick with PowerShell-native features [2] , if possible. 鉴于很容易将PowerShell的命令语法与.NET的方法语法混淆,因此, 最好尽可能使用PowerShell的本机功能 [2] 。
That said, being able to call methods on .NET types directly is a wonderful extensibility option. 也就是说,能够直接在.NET类型上调用方法是一个很好的扩展性选项。
To help avoid accidental use of method syntax when calling PowerShell commands, you can use Set-StrictMode -Version 2
or higher, but note that that entails additional strictness checks. 为帮助避免在调用PowerShell命令时意外使用方法语法,可以使用
Set-StrictMode -Version 2
或更高版本,但是请注意,这需要进行额外的严格性检查。
[1] PowerShell is, after all, a shell - but it is also a full-featured scripting language that offers near-unlimited access to the .NET framework. [1] PowerShell毕竟是一种外壳程序 -但它也是一种功能全面的脚本语言 ,可提供对.NET框架的几乎无限的访问。
Reconciling these two personalities is a difficult balancing act, and the shell-like command-invocation syntax is a frequent problem for newcomers with a programming background, given that the rest of the language looks like a traditional programming language and that calling methods on .NET types does use the traditional syntax. 调和这两种个性是一种困难的平衡,对于具有编程背景的新手来说,类似于shell的命令调用语法是一个常见问题,因为该语言的其余部分看起来像传统的编程语言,并且.NET上的调用方法类型确实使用传统语法。
[2] This means preferring PowerShell's cmdlets, functions, and operators to use of the underlying .NET types' methods; [2]这意味着偏爱PowerShell的cmdlet,函数和运算符使用底层.NET类型的方法; doing so also usually provides rewards you with operating at a higher level of abstraction.
这样做通常还可以使您在更高的抽象水平上进行操作而获得回报。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.