我是Start-Job cmdlet的新手,在调用带有参数的cmdlet的脚本块时遇到了问题。这是我到目前为止所知道的:
Start-Job -ScriptBlock {
$ServiceObj = Get-Service -Name $ServiceName -ComputerName $Computer -ErrorAction Stop
Stop-Service -InputObj $ServiceObj -erroraction stop
}当我运行receive-job时,我看到-ComputerName参数为null或空,以及-InputObj参数为null或空的错误。在这两种情况下,情况都不是这样。上面的代码片段是从两个foreach循环内部调用的:
foreach($Computer in $ComputerNames) {
foreach($ServiceName in $ServiceNames) {
#..call snippet above
}
}我尝试过在调用脚本块时使用-ArgumentList,但也没有成功。我确定我漏掉了什么?
发布于 2012-09-25 03:51:51
您确实需要使用ArgumentList (除非您使用的是PowerShell V3),例如:
Start-Job -ScriptBlock {param($ComputerName)
$ServiceObj = Get-Service -Name $ServiceName -CN $ComputerName -ErrorAction Stop
Stop-Service -InputObj $ServiceObj -erroraction stop
} -ArgumentList $Computer如果您使用的是PowerShell V3,则可以使用using变量限定符,例如:
Start-Job -ScriptBlock {
$ServiceObj = Get-Service -Name $ServiceName -CN $using:Computer -ErrorAction Stop
Stop-Service -InputObj $ServiceObj -erroraction stop
}https://stackoverflow.com/questions/12571788
复制相似问题