System Usages Details in CSV file - Knowledgebase - CLOUDRAIN

System Usages Details in CSV file Print

  • 0

# Define thresholds
$cpuThreshold = 80
$ramThreshold = 80
$diskThreshold = 80

# Define intervals (in seconds) for checking
$interval = 5

# Get the system name
$systemName = $env:COMPUTERNAME

# Specify the path for the CSV file with the system name
$logFile = "$env:USERPROFILE\Downloads\$systemName.csv"

# Loop to continuously check the system's performance
while ($true) {
# Get current timestamp
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"

# Get CPU usage
$cpu = Get-Counter '\Processor(_Total)\% Processor Time'
$cpuUsage = [math]::Round($cpu.CounterSamples[0].CookedValue, 2)

# Get RAM usage
$ram = Get-Counter '\Memory\% Committed Bytes In Use'
$ramUsage = [math]::Round($ram.CounterSamples[0].CookedValue, 2)

# Get Disk usage
$disk = Get-Counter '\PhysicalDisk(_Total)\% Disk Time'
$diskUsage = [math]::Round($disk.CounterSamples[0].CookedValue, 2)

# Create object to represent the data
$data = [PSCustomObject]@{
Timestamp = $timestamp
"CPU Usage (%)" = $cpuUsage
"RAM Usage (%)" = $ramUsage
"Disk Usage (%)" = $diskUsage
}

# Append data to CSV file
$data | Export-Csv -Path $logFile -Append -NoTypeInformation -Force

# Report if any threshold is exceeded
if ($cpuUsage -gt $cpuThreshold) {
Write-Host "High CPU usage detected: $cpuUsage%"
}

if ($ramUsage -gt $ramThreshold) {
Write-Host "High RAM usage detected: $ramUsage%"
}

if ($diskUsage -gt $diskThreshold) {
Write-Host "High Disk usage detected: $diskUsage%"
}

# Wait for the specified interval before checking again
Start-Sleep -Seconds $interval
}


Was this answer helpful?

« Back

Powered by WHMCompleteSolution