Monitoring scripts with email alerts in PowerShell - Time & Space Complexity
When running monitoring scripts that send email alerts, it's important to know how the script's work grows as it checks more items.
We want to understand how the time to complete changes when the number of checks increases.
Analyze the time complexity of the following code snippet.
$servers = @('server1', 'server2', 'server3')
foreach ($server in $servers) {
$status = Test-Connection -ComputerName $server -Count 1 -Quiet
if (-not $status) {
Send-MailMessage -To 'admin@example.com' -Subject "Alert: $server down" -Body "Server $server is not responding."
}
}
This script checks each server's status once and sends an email alert if the server is down.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each server to test its connection.
- How many times: Once per server in the list.
As the number of servers increases, the script checks each one individually, so the total work grows directly with the number of servers.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 connection checks, up to 10 email sends |
| 100 | 100 connection checks, up to 100 email sends |
| 1000 | 1000 connection checks, up to 1000 email sends |
Pattern observation: The work grows steadily and directly with the number of servers.
Time Complexity: O(n)
This means the time to run the script increases in a straight line as you add more servers to check.
[X] Wrong: "Sending emails inside the loop doesn't affect the time complexity because emails are sent quickly."
[OK] Correct: Sending an email is part of the repeated work and happens once per server that is down, so it adds to the total time as the number of servers grows.
Understanding how monitoring scripts scale helps you write efficient checks and alerts, a useful skill in real-world system management.
"What if we added a nested loop to check multiple services per server? How would the time complexity change?"
Practice
Solution
Step 1: Understand monitoring scripts
Monitoring scripts check system health or status automatically without manual checks.Step 2: Role of email alerts
Email alerts send notifications immediately when an issue is detected, so admins can act fast.Final Answer:
To notify administrators immediately when a problem occurs -> Option AQuick Check:
Alerts = Notify admins fast [OK]
- Thinking email alerts slow scripts
- Confusing alerts with local log storage
- Assuming alerts increase runtime
Solution
Step 1: Identify correct cmdlet and parameters
The correct cmdlet is Send-MailMessage with parameters -From, -To, -Subject, -Body, and -SmtpServer.Step 2: Check each option's syntax
Send-MailMessage -From 'admin@example.com' -To 'user@example.com' -Subject 'Alert' -Body 'Issue detected' -SmtpServer 'smtp.example.com' uses correct cmdlet and parameter names. Others use incorrect cmdlet names or parameter names.Final Answer:
Send-MailMessage -From 'admin@example.com' -To 'user@example.com' -Subject 'Alert' -Body 'Issue detected' -SmtpServer 'smtp.example.com' -> Option CQuick Check:
Send-MailMessage + correct params = A [OK]
- Using wrong cmdlet names like Send-Mail or Send-Email
- Incorrect parameter names like -Sender or -Receiver
- Missing -SmtpServer parameter
if ((Get-Process -Name 'notepad' -ErrorAction SilentlyContinue) -eq $null) {
Send-MailMessage -From 'alert@example.com' -To 'admin@example.com' -Subject 'Notepad not running' -Body 'Please start Notepad.' -SmtpServer 'smtp.example.com'
'Alert sent'
} else {
'Notepad is running'
}Solution
Step 1: Understand the condition
The script checks if Notepad process exists; if not found, condition is true.Step 2: Analyze the if-else branches
If Notepad is not running, it sends an email and outputs 'Alert sent'. Otherwise, outputs 'Notepad is running'.Final Answer:
'Alert sent' if Notepad is not running, else 'Notepad is running' -> Option BQuick Check:
Process check controls output [OK]
- Assuming Send-MailMessage blocks output
- Thinking condition always true
- Confusing error handling with syntax error
$freeSpace = (Get-PSDrive C).Free
if ($freeSpace -lt "100MB") {
Send-MailMessage -From 'alert@example.com' -To 'admin@example.com' -Subject 'Low Disk Space' -Body 'Disk space below 100MB' -SmtpServer 'smtp.example.com'
}But no email is sent even when disk space is low. What is the likely problem?
Solution
Step 1: Check the size comparison
PowerShell does not recognize '100MB' as a valid number; it must be converted to bytes.Step 2: Understand Get-PSDrive output
Get-PSDrive returns Free space in bytes as a number, so comparing to '100MB' string fails silently.Final Answer:
100MB is not a valid size unit in PowerShell comparison -> Option DQuick Check:
Size units must be numeric bytes [OK]
- Assuming Get-PSDrive Free is not available
- Thinking Send-MailMessage needs -Port always
- Confusing less than and greater than operators
Solution
Step 1: Understand monitoring frequency
Checking CPU every 5 minutes requires a loop with a 300-second (5-minute) sleep between checks.Step 2: Use Get-Counter and conditional email
Get-Counter can measure CPU usage; if usage > 80%, Send-MailMessage sends alert.Step 3: Evaluate options
Use a loop with Start-Sleep 300, check CPU with Get-Counter, send email if over 80% uses loop with Start-Sleep 300 and conditional email, matching requirements. Others either run once or wrong intervals.Final Answer:
Use a loop with Start-Sleep 300, check CPU with Get-Counter, send email if over 80% -> Option AQuick Check:
Loop + 5 min sleep + CPU check + email = C [OK]
- Running check only once without loop
- Using wrong sleep duration
- Confusing scheduled task frequency with script loop
Ready to prove it?
Pass the test and this pattern moves unknown
