嗨,我再次在多个服务器上运行此脚本(最初在此处发布),我想让每个特定的服务器名称出现在结果中。但是现在,我能够CPU & Memory Usage
逐个了解每个服务器的标题和使用情况。请让我知道如何获取每个服务器名称和结果。
$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
我看到您在服务器名称上运行了一个循环($ 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”知道如何处理)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句