2016-08-26 9 views
0

こんにちは私はこのスクリプトを複数のサーバーに再実行しています(initially posted here) &結果にはそれぞれの特定のサーバー名が表示されます。しかし今、私は見出しでCPU & Memory Usage &を取得してから、各サーバーの使用状況を順番に調べることができます。 Plsは私に各サーバ名&を得る方法を知らせます。最初の行にCPUとメモリの使用スクリプト

"Working on $ServerNames.." | Out-File $Output -Append 

を:私はあなたが($ ServerNamesが反復ごとに各サーバーである)のサーバー名のループを実行しているので、なぜあなたが使用していない参照

$Output = 'C:\temp\Result.txt' 
$ServerList = Get-Content 'C:\temp\ServerNames.txt' 

$ScriptBLock = { 
$CPUPercent = @{ 



Label = 'CPUUsed' 
Expression = { 
    $SecsUsed = (New-Timespan -Start $_.StartTime).TotalSeconds 
    [Math]::Round($_.CPU * 10/$SecsUsed) 
} 
} 


$MemUsage = @{ 
Label ='RAM(MB)' 
Expression = { 
[Math]::Round(($_.WS/1MB),2) 
} 
    } 
Get-Process | Select-Object -Property Name, CPU, $CPUPercent, $MemUsage, 
Description | 
Sort-Object -Property CPUUsed -Descending | 
Select-Object -First 15 | Format-Table -AutoSize 
} 
foreach ($ServerNames in $ServerList) { 
Invoke-Command -ComputerName $ServerNames {Write-Output "CPU & Memory Usage"} 
| Out-File $Output -Append 
Invoke-Command -ScriptBlock $ScriptBLock -ComputerName $ServerNames | 
Out-File $Output -Append 

答えて

1

"foreach"文の後ろに?

とにかく、私はあなたがこのようにスクリプトを変更することができると思う:スクリプトブロックの追加を :

Write-Output "CPU & Memory Usage" 
hostname | out-file $output -Append # this will throw the server name 

あなたはスクリプトブロックでこれを持っていたら、あなたはこのようにそれを実行することができます。

Invoke-Command -ScriptBlock $ScriptBLock -ComputerName $ServerList 

($ ServerListは元のサーバーの配列で、 "Invoke-Command"はどのように処理するかを知っています)。

+0

ありがとうAmir ... this one worked "Working on $ ServerNames .." |出力ファイル$出力-Append –

関連する問題