M44 - Scripting Lab: System Health Report
Scripting Lab: System Health Report
Build a small Bash or PowerShell script that records a timestamp, disk usage, memory information, and a short process summary into a readable log.
- Build a small Bash or PowerShell script that records a timestamp, disk usage, memory information, and a short process summary into a readable log.
The Goal: A Small Health Snapshot You Can Read Later
This lab is about turning several useful commands into one repeatable report.
We are not trying to build a full monitoring platform. We are trying to build a simple script that answers:
- when was the report taken?
- how full is the main disk?
- what does memory usage look like?
- which processes currently deserve attention?
That is enough to make the script worth scheduling later.
1. Pick a Safe Output Location
For learning, write the report into your user space, not a protected system log directory.
Examples:
- Windows:
C:\Users\You\Documents\health-report.log - Linux:
$HOME/health-report.log
That keeps the lab easy to run and verify.
2. Build the Report in Clear Sections
The report should be readable without opening the script.
Suggested sections:
- timestamp
- disk summary
- memory summary
- top processes
Windows Example
$LogFile = “$HOME\Documents\health-report.log” $Time = Get-Date -Format “yyyy-MM-dd HH:mm:ss”
”--- Health Report: $Time ---” | Out-File -FilePath $LogFile -Append “Disk:” | Out-File -FilePath $LogFile -Append Get-Volume -DriveLetter C | Select-Object DriveLetter, SizeRemaining | Out-File -FilePath $LogFile -Append
“Memory:” | Out-File -FilePath $LogFile -Append Get-CimInstance Win32_OperatingSystem | Select-Object TotalVisibleMemorySize, FreePhysicalMemory | Out-File -FilePath $LogFile -Append
“Top processes by CPU:” | Out-File -FilePath $LogFile -Append Get-Process | Sort-Object CPU -Descending | Select-Object -First 5 Name, CPU | Out-File -FilePath $LogFile -Append
"" | Out-File -FilePath $LogFile -Append
The Linux example below uses the same structure with standard text tools.
Linux Example
The Windows example above uses PowerShell objects for the same job.
#!/bin/bash
LOG=“$HOME/health-report.log” TIME=”$(date ’+%Y-%m-%d %H:%M:%S’)”
echo ”--- Health Report: $TIME ---” >> “$LOG” echo “Disk:” >> “$LOG” df -h / >> “$LOG”
echo "" >> “$LOG” echo “Memory:” >> “$LOG” free -h >> “$LOG”
echo "" >> “$LOG” echo “Top processes by CPU:” >> “$LOG” ps -eo pid,comm,%cpu,%mem —sort=-%cpu | head -n 6 >> “$LOG”
echo "" >> “$LOG”
3. Keep the First Version Boring and Trustworthy
A good first report is not fancy. It is predictable.
Resist the urge to add too much too early:
- external network checks can fail for reasons unrelated to the machine
- complex formatting makes debugging harder
- too many sections bury the important parts
If the file is readable and the script runs reliably, the lab is working.
What Makes This a Good Learning Script
This script is valuable because each section is easy to test by itself. If something looks wrong, you can run the disk, memory, or process command separately and compare the output.
4. How to Verify the Report
After running the script:
- open the report file
- confirm the timestamp changed
- confirm each section printed something useful
- run it a second time and make sure a new block was appended instead of overwriting the previous one
That verification loop matters as much as writing the script.
What You Just Learned
- A useful first health report can be small and still teach a lot.
- Writing to user space makes early scripting easier to test.
- Clear report sections matter more than clever formatting.
- Disk, memory, and process summaries give you a practical snapshot of system state.
- A script becomes more trustworthy when you can test each part independently.
Next, you will build a backup script that uses the same principles of clear inputs, safe output paths, and repeatable results.