单个 PowerShell 命令的限制
Limit of an individual PowerShell command
我在 运行 下面的脚本中从多个 servers.However 获得 CPU 利用率 我遇到过几台计算机,下面的脚本永远存在,即没有错误,没有控制台输出。所以我在特定时间段后使用 Job 命令杀死。但是,当我尝试执行此代码时,出现以下错误。
有没有办法设置单个 PowerShell 命令的时间限制?
$ServerList = ("xyz","abc")
foreach ($computername in $ServerList) {
$timeout = 30
$job = Start-Job -ScriptBlock {
Get-WmiObject Win32_Processor -ComputerName $using:computername |
Measure-Object -Property LoadPercentage -Average |
Select Average
}
Wait-Job -Job $job -Timeout $timeout
Stop-Job -Job $job
Receive-Job $job
Remove-Job -Job $job
}
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
抱歉,我错过了您使用的是 PowerShell v2。 using:
范围修饰符在 PowerShell v3 之前不可用,因此您需要使用 -ArgumentList
参数将计算机名称传递到脚本块中。
改变这个:
$job = Start-Job -ScriptBlock {
Get-WmiObject Win32_Processor -ComputerName <b>$using:computername</b> |
Measure-Object -Property LoadPercentage -Average |
Select Average
}
进入这个:
$job = Start-Job -ScriptBlock {
Get-WmiObject Win32_Processor -ComputerName <b>$args[0]</b> |
Measure-Object -Property LoadPercentage -Average |
Select -Expand Average
} <b>-ArgumentList $computername</b>
我在 运行 下面的脚本中从多个 servers.However 获得 CPU 利用率 我遇到过几台计算机,下面的脚本永远存在,即没有错误,没有控制台输出。所以我在特定时间段后使用 Job 命令杀死。但是,当我尝试执行此代码时,出现以下错误。
有没有办法设置单个 PowerShell 命令的时间限制?
$ServerList = ("xyz","abc")
foreach ($computername in $ServerList) {
$timeout = 30
$job = Start-Job -ScriptBlock {
Get-WmiObject Win32_Processor -ComputerName $using:computername |
Measure-Object -Property LoadPercentage -Average |
Select Average
}
Wait-Job -Job $job -Timeout $timeout
Stop-Job -Job $job
Receive-Job $job
Remove-Job -Job $job
}
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
抱歉,我错过了您使用的是 PowerShell v2。 using:
范围修饰符在 PowerShell v3 之前不可用,因此您需要使用 -ArgumentList
参数将计算机名称传递到脚本块中。
改变这个:
$job = Start-Job -ScriptBlock {
Get-WmiObject Win32_Processor -ComputerName <b>$using:computername</b> |
Measure-Object -Property LoadPercentage -Average |
Select Average
}
进入这个:
$job = Start-Job -ScriptBlock {
Get-WmiObject Win32_Processor -ComputerName <b>$args[0]</b> |
Measure-Object -Property LoadPercentage -Average |
Select -Expand Average
} <b>-ArgumentList $computername</b>