2016-08-26 44 views
0

嗨我再次運行此腳本多個服務器(initially posted here) &我想獲得每個特定的服務器名稱將出現在結果中。但是現在,我可以獲得標題CPU & Memory Usage &,然後依次查看每臺服務器的使用情況。請讓我知道如何獲得每個服務器名稱&的結果。CPU和內存使用腳本

$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

我看到你與服務器名稱的循環中運行($ servernames節點是每個迭代每個服務器),那麼你爲什麼不使用:

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

在第一行在「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

謝謝埃米爾...這一個工作 「Working on $ ServerNames ..」| Out-File $ Output -Append –