I use a custom PowerShell script to monitor some servers' CPU, memory, and disk usage. The script runs every minute via the Windows Task Scheduler and generates a CSV file from which I can analyze to identify performance issues.
###########################
# author : std.rocks
# version : 1.0
# date : 2023.05.08
# role : monitor cpu, memory and disk usage
# other : Tested on Windows 2019 Server
#get current date
$date = Get-Date -Format "yyyy.MM.dd_H:m"
#The script will saved collected informations to C:\Monitoring\ to a YYYY.MM_monitor_result.csv file
$csv_file = "C:\Monitoring\" + $(Get-Date -Format "yyyy.MM_") + "monitor_result.csv"
#####
#CPU#
#####
#Note : Use "Get-Counter -ListSet *" to get all available counters
#Get cpu usage
#FR :
#$cpu = (Get-Counter '\Processeur(_Total)\% temps processeur').CounterSamples.CookedValue
#EN :
$cpu = (Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue
$cpu = [math]::round($cpu,2)
########
#MEMORY#
########
#Get memory usage
#FR :
#$mem = (Get-Counter '\mémoire\mégaoctets disponibles').CounterSamples.CookedValue
#EN :
$mem = (Get-Counter '\Memory\Available MBytes').CounterSamples.CookedValue
######
#DISK#
######
#Get every C: disk Infos
$disk = (Get-WmiObject Win32_PerfRawData_PerfDisk_LogicalDisk | Where-Object { $_.Name -like "C*"})
$disk_queue = $disk.CurrentDiskQueueLength
$DBytes = $disk.DiskBytesPerSec
$DRead = $disk.DiskReadBytesPerSec
$DWrite = $disk.DiskWriteBytesPerSec
#Create csv file
if (!$(Test-Path -Path $csv_file)) {
#if the file doesn't exist, create it with csv columns id
'date,cpu,mem,disk_kbytes,disk_reads,disk_writes,disk_queue' | Out-File -Encoding utf8 $csv_file
} else {
#otherwise, add collected values
$date + "," + $cpu.ToString().replace(",",".") + "," + $mem.ToString() + "," + ($DBytes /1000) + "," + ($DRead /1000) + "," + ($DWrite /1000) + "," + $disk_queue | Out-File -Encoding utf8 $csv_file -Append
}
Follow this link to create a Windows task in order to run the script periodically.
Using the generated CSV file, we can create elegant graphics of this type.
Contact :