Automating System Health Check with PowerShell
This PowerShell script is designed to automate the process of checking the health of a Windows system. It gathers various system metrics such as CPU usage, available memory, and disk space, and outputs the information in a readable format. This guide provides a detailed, step-by-step explanation of how the script works, making it easier for IT professionals and system administrators to maintain their systems effectively.
### Step 1: Define Variables for System Metrics
In this step, we will create variables that will hold the information about CPU, memory, and disk space.
“`powershell
# Variables for system performance metrics $cpuMetric = Get-Counter \Processor(_Total)\% Processor Time $memMetric = Get-Counter \Memory\Available MBytes $diskMetric = Get-Counter \LogicalDisk(_Total)\% Free Space
“`
### Explanation:
– We utilize the `Get-Counter` cmdlet to collect performance counters for CPU, memory, and disk space.
– `\Processor(_Total)\% Processor Time` retrieves total CPU usage.
– `\Memory\Available MBytes` retrieves the amount of available memory in megabytes.
– `\LogicalDisk(_Total)\% Free Space` retrieves the percentage of free space across all logical disks.
### Step 2: Store and Format Data for Output
Next, we will store the metrics in a structured format for easier output.
“`powershell
# Store formatted data in a custom PSObject $healthReport = [pscustomobject]@{ TimeChecked = Get-Date CPUUsage = $cpuMetric.CounterSamples.CookedValue AvailableMemory = $memMetric.CounterSamples.CookedValue FreeDiskSpace = $diskMetric.CounterSamples.CookedValue }
“`
### Explanation:
– We create a custom PowerShell object (`PSObject`) to neatly organize our health report.
– This structure includes the time when the check was performed, CPU usage, available memory, and free disk space percentage.
### Step 3: Output the Health Report to Console
Now, we will output the health report to the console in a user-friendly format.
“`powershell
# Output health report to the console Write-Host "System Health Report" Write-Host "====================" Write-Host "Time Checked: $($healthReport.TimeChecked)" Write-Host "CPU Usage: $([math]::Round($healthReport.CPUUsage, 2))%" Write-Host "Available Memory: $([math]::Round($healthReport.AvailableMemory, 2)) MB" Write-Host "Free Disk Space: $([math]::Round($healthReport.FreeDiskSpace, 2))%"
“`
### Explanation:
– `Write-Host` is used to print the health report to the console.
– We utilize `math::Round` to present the values in a clean format.
– This visual representation allows quick assessment of the systems health status.
### Step 4: Optional – Exporting Report to a Text File
For future reference, you may want to store the health report in a file. Heres how to do that.
“`powershell
# Export health report to a text file $reportPath = "C:\SystemHealthReport.txt" $healthReport | Out-File -FilePath $reportPath -Encoding UTF8 Write-Host "Health report exported to: $reportPath"
“`
### Explanation:
– The `Out-File` cmdlet saves the health report to a specified path.
– The file is created with UTF-8 encoding, ensuring that the text is correctly formatted for readability.
– This step is optional but highly recommended for documentation and future analysis.
### Conclusion
With this PowerShell script, IT professionals can automate system health checks efficiently. The script can easily integrate into scheduled tasks, providing routine insights into system performance and health. By following the detailed steps outlined above, you can modify and extend this script to meet the specific needs of your operational environment.