0
0
PowerShellscripting~10 mins

Monitoring scripts with email alerts in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Monitoring scripts with email alerts
Start Script
Check System Metric
Is Metric OK?
NoSend Email Alert
Log Alert Sent
Wait/Delay
Repeat or End
The script starts, checks a system metric, sends an email if the metric is bad, logs the alert, waits, then repeats or ends.
Execution Sample
PowerShell
if ($cpuUsage -gt 80) {
  Send-MailMessage -To 'admin@example.com' -Subject 'High CPU Alert' -Body "CPU usage is $cpuUsage%" -SmtpServer 'smtp.example.com'
}
This script checks if CPU usage is above 80%, then sends an email alert with the current CPU usage.
Execution Table
StepActionCondition/EvaluationResult/Output
1Get CPU usage$cpuUsage = 75CPU usage recorded as 75%
2Check if $cpuUsage > 8075 > 80False, no email sent
3Wait before next checkN/APause for 60 seconds
4Get CPU usage$cpuUsage = 85CPU usage recorded as 85%
5Check if $cpuUsage > 8085 > 80True, proceed to send email
6Send email alertSend-MailMessage executedEmail sent to admin@example.com
7Log alertWrite to log fileAlert logged with timestamp
8Wait before next checkN/APause for 60 seconds
9Repeat or endScript loops or stopsScript ready for next iteration or exit
💡 Script stops when manually terminated or after set iterations
Variable Tracker
VariableStartAfter Step 1After Step 4Final
$cpuUsageundefined758585
Email SentNoNoYesYes
Log EntryNoneNoneCreatedCreated
Key Moments - 3 Insights
Why is no email sent when CPU usage is 75%?
Because the condition $cpuUsage > 80 is false at step 2, so the script skips sending the email.
What triggers the email alert to be sent?
When the CPU usage exceeds 80%, as shown at step 5, the condition becomes true and the email is sent.
Why does the script wait after checking the CPU usage?
The wait at steps 3 and 8 prevents the script from running continuously without pause, allowing time between checks.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $cpuUsage at step 4?
A75
B85
C80
DUndefined
💡 Hint
Check the 'Condition/Evaluation' column at step 4 in the execution table.
At which step does the script send the email alert?
AStep 6
BStep 5
CStep 2
DStep 7
💡 Hint
Look for the 'Send email alert' action in the 'Action' column.
If the CPU usage never exceeds 80%, what happens to the 'Email Sent' variable?
AIt changes to Yes
BIt becomes undefined
CIt remains No
DIt logs an error
💡 Hint
Refer to the variable_tracker for 'Email Sent' values when CPU usage is below threshold.
Concept Snapshot
Monitoring scripts check system metrics regularly.
If a metric crosses a threshold, an email alert is sent.
Use Send-MailMessage in PowerShell to send emails.
Include logging and wait times to avoid spamming.
Loop the script for continuous monitoring.
Full Transcript
This visual execution shows a PowerShell monitoring script that checks CPU usage. It starts by reading the CPU usage value. If the usage is above 80%, it sends an email alert to the administrator and logs the alert. If not, it waits before checking again. The script repeats this cycle until stopped. Variables like $cpuUsage and Email Sent track the system state and alert status. Key moments include understanding when emails are sent and why the script waits between checks. The quiz tests knowledge of variable values and script behavior at specific steps.