V T
V T

Reputation: 47

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. 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

Upvotes: 0

Views: 1198

Answers (1)

Amir Saar
Amir Saar

Reputation: 90

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:

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

on the first line after the "foreach" statement?

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).

Upvotes: 1

Related Questions