Sea*_*ean 71 powershell 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但是我遇到了错误所以我必须要有语法错误或者其他什么.任何帮助是极大的赞赏.
Jay*_*kul 92
-ArgumentList基于与scriptblock命令一起使用,例如:
Invoke-Command -Cn (gc Servers.txt) {param($Debug=$False, $Clear=$False) C:\Scripts\ArchiveEventLogs\ver5\ArchiveEventLogs.ps1 } -ArgumentList $False,$True
当你用-File它调用它仍然传递参数像愚蠢的splatted数组.我已经提交了一个功能请求,要求将其添加到命令中(请将其投票).
所以,你有两个选择:
如果你有一个看起来像这样的脚本,可以在远程机器上访问的网络位置(注意这-Debug是隐含的,因为当我使用该Parameter属性时,脚本隐式获取CmdletBinding,因此,所有常见参数):
param(
   [Parameter(Position=0)]
   $one
,
   [Parameter(Position=1)]
   $two
,
   [Parameter()]
   [Switch]$Clear
)
"The test is for '$one' and '$two' ... and we $(if($DebugPreference -ne 'SilentlyContinue'){"will"}else{"won't"}) run in debug mode, and we $(if($Clear){"will"}else{"won't"}) clear the logs after."
而不会挂了的意思$Clear......如果你想调用,你可以使用以下的Invoke-Command语法:
icm -cn (gc Servers.txt) { 
    param($one,$two,$Debug=$False,$Clear=$False)
    C:\Scripts\ArchiveEventLogs\ver5\ArchiveEventLogs.ps1 @PSBoundParameters
} -ArgumentList "uno", "dos", $false, $true
在那个,我复制了我在scriptblock中关注的所有参数,所以我可以传递值.如果我可以对它们进行硬编码(这就是我实际做的那样),就没有必要这样做并使用PSBoundParameters,我可以通过我需要的那些.在下面的第二个例子中,我将传递$ Clear,只是为了演示如何传递switch参数:
icm -cn $Env:ComputerName { 
    param([bool]$Clear)
    C:\Scripts\ArchiveEventLogs\ver5\ArchiveEventLogs.ps1 "uno" "dos" -Debug -Clear:$Clear
} -ArgumentList $(Test-Path $Profile)
如果脚本在您的本地计算机上,并且您不想将参数更改为位置,或者您想要指定作为公共参数的参数(因此您无法控制它们),您将需要获取该脚本并将其嵌入您的scriptblock中:
$script = [scriptblock]::create( @"
param(`$one,`$two,`$Debug=`$False,`$Clear=`$False)
&{ $(Get-Content C:\Scripts\ArchiveEventLogs\ver5\ArchiveEventLogs.ps1 -delimiter ([char]0)) } @PSBoundParameters
"@ )
Invoke-Command -Script $script -Args "uno", "dos", $false, $true
如果您确实需要传入脚本名称的变量,那么您要做的将取决于变量是本地定义还是远程定义.通常,如果您有一个带有脚本名称的变量$Script或环境变量$Env:Script,您可以使用调用运算符(&)执行它:&$Script或者&$Env:Script
如果它是已经在远程计算机上定义的环境变量,那就是它的全部内容.如果它是局部变量,那么您必须将它传递给远程脚本块:
Invoke-Command -cn $Env:ComputerName { 
    param([String]$Script, [bool]$Clear)
    &$Script "uno" "dos" -Debug -Clear:$Clear
} -ArgumentList $ScriptPath, $(Test-Path $Profile)
我的解决方案是使用[scriptblock]:Create以下命令动态编写脚本块:
# Or build a complex local script with MARKERS here, and do substitutions
# I was sending install scripts to the remote along with MSI packages
# ...for things like Backup and AV protection etc.
$p1 = "good stuff"; $p2 = "better stuff"; $p3 = "best stuff"; $etc = "!"
$script = [scriptblock]::Create("MyScriptOnRemoteServer.ps1 $p1 $p2 $etc")
#strings get interpolated/expanded while a direct scriptblock does not
# the $parms are now expanded in the script block itself
# ...so just call it:
$result = invoke-command $computer -script $script
传递参数是非常令人沮丧,尝试各种方法,例如
-arguments,$using:p1等,如没有问题,期望这只是工作。
因为我控制[scriptblock]以此方式创建(或脚本文件)的字符串的内容和变量扩展,所以“ invoke-command”命令没有真正的问题。
(不应该那么难。:))
我怀疑它是自这篇文章创建以来的一个新功能 - 使用 $Using:var 将参数传递给脚本块。然后它是一个简单的传递参数的材料,前提是脚本已经在机器上或相对于机器的已知网络位置
以主要示例为例:
icm -cn $Env:ComputerName { 
    C:\Scripts\ArchiveEventLogs\ver5\ArchiveEventLogs.ps1 -one "uno" -two "dos" -Debug -Clear $Using:Clear
}
| 归档时间: | 
 | 
| 查看次数: | 144680 次 | 
| 最近记录: |