如何使用 Invoke-Command 传递命名参数?
我有一个可以通过 Invoke-Command 远程运行的脚本,
Invoke-Command -ComputerName (Get-Content C:\Scripts\Servers.txt) `
-FilePath C:\Scripts\ArchiveEventLogs\ver5\ArchiveEventLogs.ps1
只要我使用默认参数,它就可以正常工作。但是,该脚本有 2 个命名的 [switch] 参数(-Debug 和 -Clear)
如何通过 Invoke-Command 传递切换的参数?我已经尝试过 -ArgumentList 但出现错误,所以我一定是语法错误或其他问题。非常感谢任何帮助。
I have a script that I can run remotely via Invoke-Command
Invoke-Command -ComputerName (Get-Content C:\Scripts\Servers.txt) `
-FilePath C:\Scripts\ArchiveEventLogs\ver5\ArchiveEventLogs.ps1
As long as I use default parameters, it works fine. However, the script has 2 named [switch] parameters (-Debug and -Clear)
How can I pass the switched parameters via the Invoke-Command? I've tried the -ArgumentList but I'm getting errors so I must have the syntax wrong or something. Any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
-ArgumentList
基于与 scriptblock 命令的使用,例如:当您使用
-File
调用它时,它仍然像哑巴一样传递参数大批。我已经提交了 功能请求将其添加到命令中(请投票赞成)。因此,您有两个选择:
如果您有一个如下所示的脚本,位于可从远程计算机访问的网络位置(请注意,隐含
-Debug
,因为当我使用Parameter< /code> 属性,脚本隐式获取 CmdletBinding,因此获取所有常用参数):
无需纠结于
$Clear
的含义...如果您想调用它,您可以使用以下任一Invoke-Command
语法:在该语法中,我在 脚本块 中复制我关心的所有参数,以便可以传递值。如果我可以对它们进行硬编码(这就是我实际所做的),则无需这样做并使用 PSBoundParameters,我只需传递我需要的参数即可。在下面的第二个示例中,我将传递 $Clear 一个,只是为了演示如何传递开关参数:
另一个选项
如果脚本位于本地计算机上,并且您不想将参数更改为位置参数,或者您想要指定常见参数的参数(因此您无法控制它们),您将需要获取该脚本的内容并将其嵌入到您的脚本块中:
PostScript:
如果您确实需要要传入脚本名称的变量,您要做的事情取决于该变量是本地定义的还是远程定义的。一般来说,如果您有一个带有脚本名称的变量
$Script
或环境变量$Env:Script
,您可以使用调用运算符 (& ):&$Script
或&$Env:Script
如果它是已在远程计算机上定义的环境变量,则仅此而已。如果它是一个本地变量,那么您必须将其传递给远程脚本块:
-ArgumentList
is based on use with scriptblock commands, like:When you call it with a
-File
it still passes the parameters like a dumb splatted array. I've submitted a feature request to have that added to the command (please vote that up).So, you have two options:
If you have a script that looked like this, in a network location accessible from the remote machine (note that
-Debug
is implied because when I use theParameter
attribute, the script gets CmdletBinding implicitly, and thus, all of the common parameters):Without getting hung up on the meaning of
$Clear
... if you wanted to invoke that you could use either of the followingInvoke-Command
syntaxes:In that one, I'm duplicating ALL the parameters I care about in the scriptblock so I can pass values. If I can hard-code them (which is what I actually did), there's no need to do that and use
PSBoundParameters
, I can just pass the ones I need to. In the second example below I'm going to pass the $Clear one, just to demonstrate how to pass switch parameters:The other option
If the script is on your local machine, and you don't want to change the parameters to be positional, or you want to specify parameters that are common parameters (so you can't control them) you will want to get the content of that script and embed it in your scriptblock:
PostScript:
If you really need to pass in a variable for the script name, what you'd do will depend on whether the variable is defined locally or remotely. In general, if you have a variable
$Script
or an environment variable$Env:Script
with the name of a script, you can execute it with the call operator (&):&$Script
or&$Env:Script
If it's an environment variable that's already defined on the remote computer, that's all there is to it. If it's a local variable, then you'll have to pass it to the remote script block:
我的解决方案是使用
[scriptblock]:Create
动态编写脚本块:传递参数非常令人沮丧,尝试了各种方法,例如,
-arguments
、$using:p1
等,这只是按预期工作,没有任何问题。由于我以这种方式控制创建
[scriptblock]
(或脚本文件)的字符串的内容和变量扩展,因此“invoke-command”咒语没有真正的问题。(这不应该那么难。:))
My solution to this was to write the script block dynamically with
[scriptblock]:Create
:Passing arguments was very frustrating, trying various methods, e.g.,
-arguments
,$using:p1
, etc. and this just worked as desired with no problems.Since I control the contents and variable expansion of the string which creates the
[scriptblock]
(or script file) this way, there is no real issue with the "invoke-command" incantation.(It shouldn't be that hard. :) )
我怀疑这是自创建这篇文章以来的一个新功能 - 使用 $Using:var 将参数传递给脚本块。然后,只要脚本已经在机器上或相对于机器的已知网络位置,传递参数就很简单了,
主要示例如下:
I suspect its a new feature since this post was created - pass parameters to the script block using $Using:var. Then its a simple mater to pass parameters provided the script is already on the machine or in a known network location relative to the machine
Taking the main example it would be:
我需要一些东西来调用带有命名参数的脚本。我们的政策是不使用参数的顺序定位并要求参数名称。
我的方法与上面的方法类似,但获取要调用的脚本文件的内容并发送包含参数和值的参数块。
这样做的优点之一是您可以选择将哪些参数发送到脚本文件,从而允许使用默认值的非强制参数。
假设临时路径中有一个名为“MyScript.ps1”的脚本,该脚本具有以下参数块:
这就是我从另一个脚本调用此脚本的方式:
我已经在很多场景中使用了它,并且效果非常好。
您偶尔需要做的一件事是在参数值分配块周围加上引号。当值中存在空格时,总是会出现这种情况。
例如,此参数块用于调用脚本,该脚本将各种模块复制到 PowerShell
C:\Program Files\WindowsPowerShell\Modules
使用的标准位置,其中包含空格字符。希望这有帮助!
I needed something to call scripts with named parameters. We have a policy of not using ordinal positioning of parameters and requiring the parameter name.
My approach is similar to the ones above but gets the content of the script file that you want to call and sends a parameter block containing the parameters and values.
One of the advantages of this is that you can optionally choose which parameters to send to the script file allowing for non-mandatory parameters with defaults.
Assuming there is a script called "MyScript.ps1" in the temporary path that has the following parameter block:
This is how I would call this script from another script:
I have used this in lots of scenarios and it works really well.
One thing that you occasionally need to do is put quotes around the parameter value assignment block. This is always the case when there are spaces in the value.
e.g. This param block is used to call a script that copies various modules into the standard location used by PowerShell
C:\Program Files\WindowsPowerShell\Modules
which contains a space character.Hope this helps!
这是一个不幸的情况。位置参数起作用。
或者您可以硬编码默认值。
或者将脚本复制到那里:
This is an unfortunate situation. Positional parameters work.
Or you can hardcode a default.
Or copy the script there: