Powershell 运行作业时的参数解释
为什么上面的代码块会引发以下错误Powershell 运行作业时的参数解释,powershell,Powershell,为什么上面的代码块会引发以下错误 $h = "host1.example.com" $code = { $(Get-WmiObject -Class "Win32_ComputerSystem" -Namespace "root\cimv2" -ComputerName $h) } $timeout = 5 $jobstate = $(Wait-Job -Job ($job = $(Start-Job -ScriptBlock $code)) -Timeout $timeout) $wmic
$h = "host1.example.com"
$code = {
$(Get-WmiObject -Class "Win32_ComputerSystem" -Namespace "root\cimv2" -ComputerName $h)
}
$timeout = 5
$jobstate = $(Wait-Job -Job ($job = $(Start-Job -ScriptBlock $code)) -Timeout $timeout)
$wmicomobj = $(Receive-Job -Job $job)
无法验证参数“ComputerName”上的参数。参数为null或
空的。请提供一个不为null或空的参数,然后重试该命令
再一次。
+CategoryInfo:InvalidData:(:)[Get WMIOObject],ParameterBindingValidationException
+FullyQualifiedErrorId:ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand
+PSComputerName:localhost
我想用它来实现在循环中获取多个主机的WMI对象时的超时。但首先,我需要通过作业执行获得结果。脚本块中无法使用脚本全局范围中定义的变量,除非您使用
using
限定符:
Cannot validate argument on parameter 'ComputerName'. The argument is null or
empty. Supply an argument that is not null or empty and then try the command
again.
+ CategoryInfo : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand
+ PSComputerName : localhost
或者将它们作为参数传入,如下所示:
$code = {
Get-WmiObject -Class "Win32_ComputerSystem" -Namespace "root\cimv2" -ComputerName $using:h
}
$code = {
Param($hostname)
Get-WmiObject -Class "Win32_ComputerSystem" -Namespace "root\cimv2" -ComputerName $hostname
}
$jobstate = Wait-Job -Job ($job = $(Start-Job -ScriptBlock $code -ArgumentList $h)) -Timeout $timeout
或者像这样:
$code = {
Get-WmiObject -Class "Win32_ComputerSystem" -Namespace "root\cimv2" -ComputerName $using:h
}
$code = {
Param($hostname)
Get-WmiObject -Class "Win32_ComputerSystem" -Namespace "root\cimv2" -ComputerName $hostname
}
$jobstate = Wait-Job -Job ($job = $(Start-Job -ScriptBlock $code -ArgumentList $h)) -Timeout $timeout