2017-01-05 5 views
0

複数のサーバーからCPU使用率を取得するには、以下のスクリプトを実行しています。以下のスクリプトが永久にスクロールする場所、つまりコンソール出力はありません。だから私は特定の時間の後に殺すJobコマンドを使用しています。しかし、このコードを実行しようとすると、エラー以下になってしまいます。個々のPowerShellコマンドの制限

個々の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
+0

[タイムアウト付きコピー項目](の可能性のある重複http://stackoverflow.com/questions/38623767/copy-item- with-timeout) –

+0

'Start-Job -Computer $ computername -Scriptblock {Get-WmiObject Win32_Processor | ...; ...} -Timeout ... ' –

+0

リプレイのおかげで@Angar Wiechers以下のコードは出力を表示しません:$ computername =" XYZ " $ timeout = 300 $ job = Start-Job -computername $ computername -Scriptblock {Get-WmiObject win32_processor | Measureオブジェクト - プロパティLoadPercentage - 平均|平均を選択} 待機ジョブ-Job $ジョブ - タイムアウト$タイムアウト 停止ジョブ-Job $ジョブ 受信ジョブ-Job $ジョブ ジョブを削除-Job $ジョブ – Raj

答えて

0

申し訳ありませんが、私はあなたがPowerShellのV2を使用していることを見逃していました。スコープ修飾子using:はPowerShell v3より前では使用できませんでしたので、コンピュータ名をスクリプトブロックに渡すには-ArgumentListパラメータを使用する必要があります。

変更この:この中

$job = Start-Job -ScriptBlock { 
    Get-WmiObject Win32_Processor -ComputerName $using:computername | 
     Measure-Object -Property LoadPercentage -Average | 
     Select Average 
}

$job = Start-Job -ScriptBlock { 
    Get-WmiObject Win32_Processor -ComputerName $args[0] | 
     Measure-Object -Property LoadPercentage -Average | 
     Select -Expand Average 
} -ArgumentList $computername
+0

ありがとうございました.. :) – Raj

関連する問題