Requirement: One of my clients requested to check the status of the CPU utilization and Memory usage before starting the deployment. If any utilization is beyond the threshold (by taking the N samples of utilization with N intervals), the deployment process needs to stop in the remote machine and share the email notification with the support team.
Post by automation, though to write the post on the same and with sample examples. In Windows PowerShell, there is no exclusive cmdlet to find out the CPU and memory utilization rates. You can use the get-wmi object cmdlet along with the required parameters to fetch the Memory results.
STEP #1: Get the IP and hostname of the Remote server
Param ( [string]$iP, [string]$hostName )
STEP #2: Assign the username and password to connect the server to execute the remote script
$username = $env:serviceUsername
$password = $env:servicePwd
$userPassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$userCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $userPassword
STEP #3: Use Invoke-Command to connect the server and execute the script inside the connected Machine.
In the Invoke-Command pass the IP of the remote server which going to check the CPU & Memory usage.
Invoke-Command -ComputerName $iP -ScriptBlock {
#…Enter your Logic
} -credential $userCredential
STEP 4: Get the sample CPU usage with an interval of 2 seconds and Find the average.
We can achieve this by using the Get-Counter cmdlet in Powershell. It will get performance counter data directly from the performance monitoring instrumentation in the Windows family of operating systems. Get-Counter gets performance data from a local computer or remote computers. For this example, we fetched the CPU samples 5 times at 2-sec Intervals.
$CPUAveragePerformance = (GET-COUNTER -Counter “\Processor(_Total)\% Processor Time” -SampleInterval 2 -MaxSamples 5 |select -ExpandProperty countersamples | select -ExpandProperty cookedvalue | Measure-Object -Average).average
Write-Host “Average of CPU usage (calculated with 5 Sample with an interval of 2 sec):” $CPUAveragePerformance
STEP 5: Get the Memory usage using Get-WmiObject cmdlet.
$ComputerMemory = Get-WmiObject -ComputerName $env:COMPUTERNAME -Class win32_operatingsystem -ErrorAction Stop
$Memory = ((($ComputerMemory.TotalVisibleMemorySize – $ComputerMemory.FreePhysicalMemory)*100)/ $ComputerMemory.TotalVisibleMemorySize)
$RoundMemory = [math]::Round($Memory, 2)
Write-Host “Memory usage percentage of the system :” $RoundMemory
You can save this script as .PS1 (DEVMachine_ServerHealthCheck.ps1) and call in automation bypassing the IP & Hostname as parameters to get the result.
Full Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#Check the CPU & Memory Usage in the Remote Machine Param ( [string]$iP, [string]$hostName ) $username = $env:serviceUsername $password = $env:servicePwd $userPassword = ConvertTo-SecureString -String $password -AsPlainText -Force $userCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $userPassword try { Invoke-Command -ComputerName $iP -ScriptBlock { $CPUAveragePerformance = (GET-COUNTER -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 2 -MaxSamples 5 | select -ExpandProperty countersamples | select -ExpandProperty cookedvalue | Measure-Object -Average).average Write-Host "Average of CPU usage (calculated with 5 interval of 2 sec) :" $CPUAveragePerformance $ComputerMemory = Get-WmiObject -ComputerName $hostName -Class win32_operatingsystem $Memory = ((($ComputerMemory.TotalVisibleMemorySize - $ComputerMemory.FreePhysicalMemory)*100)/ $ComputerMemory.TotalVisibleMemorySize) $RoundMemory = [math]::Round($Memory, 2) Write-Host "Memory usage percentage of the system :" $RoundMemory } -credential $userCredential } catch{ Write-Host $error[0] } |