PowerShell: Check your local server performance

You can call this server performance script on a scheduled task too!

'D:\PowerShell\Check-LocalPerf.ps1'
# Set the output folder path
mkdir D:\PowerShell\Logs -Force -Verbose
$outputFolderPath = "D:\PowerShell\Logs"

# Set the output file path
$day = Get-Date -Format "yyyyMMdd"
$outputFileName = "$day-$env:COMPUTERNAME-Performance.txt"
$outputFilePath = "$outputFolderPath\$outputFileName" 

# Set the time limit to 30 seconds
$startTime = Get-Date
$timeLimit = New-TimeSpan -Seconds 30

# Collects performance data for 30 seconds and exits
while ((Get-Date) -lt ($startTime + $timeLimit)) {
# Get your system performance metrics
$cpuUsage = Get-Counter -Counter "\Processor(_Total)\% Processor Time" | Select-Object -ExpandProperty CounterSamples | Select-Object -ExpandProperty CookedValue
$memoryUsage = Get-Counter -Counter "\Memory\Available MBytes" | Select-Object -ExpandProperty CounterSamples | Select-Object -ExpandProperty CookedValue
$diskIO = Get-Counter -Counter "\LogicalDisk(_Total)\Disk Bytes/sec" | Select-Object -ExpandProperty CounterSamples | Select-Object -ExpandProperty CookedValue
$networkTraffic = Get-Counter -Counter "\Network Interface(*)\Bytes Total/sec" | Select-Object -ExpandProperty CounterSamples | Measure-Object -Property CookedValue -Sum | Select-Object -ExpandProperty Sum

# Write data to output file in percentages
Add-Content -Path $outputFilePath -Value "CPU Usage: $($cpuUsage)%"
Add-Content -Path $outputFilePath -Value "Memory Usage: $($memoryUsage) MB"
Add-Content -Path $outputFilePath -Value "Disk IO: $($diskIO) bytes/sec"
Add-Content -Path $outputFilePath -Value "Network Traffic: $($networkTraffic / 1MB) MB/sec"
}

Set the ‘$timeLimit = New-TimeSpan -Seconds 30’ to what you need, just watch the size!


Posted

in

by

Tags: