简体   繁体   English

CPU和内存使用脚本

[英]CPU & Memory Usage Script

Hi I am running this script again multiple servers (initially posted here) & I would like to get each specific servers names to be appeared in the result. 嗨,我再次在多个服务器上运行此脚本(最初在此处发布) ,我想让每个特定的服务器名称出现在结果中。 But right now, I am able to get with the heading CPU & Memory Usage & then the usage for each server one after the other. 但是现在,我能够获得“ CPU & Memory Usage ”标题,然后是每个服务器的使用率。 Pls let me know how to get each server name & the result. 请让我知道如何获取每个服务器名称和结果。

$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

I see you're running with a loop on the servers names ($ServerNames is each server for each iteration), so why don't you use: 我看到您在服务器名称上运行一个循环($ ServerNames是每次迭代的每个服务器),所以为什么不使用:

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

on the first line after the "foreach" statement? 在“ foreach”语句之后的第一行?

Anyway, I think you can change your script like this: On the script block add: 无论如何,我认为您可以像这样更改脚本:在脚本块上添加:

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

Once you have this on the Scriptblock, you can run it like this: 将其放在脚本块上后,就可以像这样运行它:

Invoke-Command -ScriptBlock $ScriptBLock -ComputerName $ServerList

($ServerList is the original servers' array, which "Invoke-Command" knows how to handle). ($ ServerList是原始服务器的数组,“ Invoke-Command”知道如何处理)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM