0
0
PowerShellscripting~30 mins

Monitoring scripts with email alerts in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Monitoring scripts with email alerts
📖 Scenario: You are managing a small server that runs important tasks. You want to check if a specific service is running and get an email alert if it is stopped. This helps you fix problems quickly.
🎯 Goal: Build a PowerShell script that checks the status of the Spooler service and sends an email alert if the service is not running.
📋 What You'll Learn
Create a variable with the service name Spooler
Create a variable with the email recipient address admin@example.com
Check the service status using Get-Service
Send an email alert using Send-MailMessage if the service is stopped
Print a message confirming the alert was sent
💡 Why This Matters
🌍 Real World
System administrators use monitoring scripts like this to keep servers running smoothly and get notified quickly if something breaks.
💼 Career
Knowing how to automate monitoring and alerts is a key skill for IT support, system administration, and DevOps roles.
Progress0 / 4 steps
1
Set up service and email variables
Create a variable called serviceName and set it to "Spooler". Create another variable called emailTo and set it to "admin@example.com".
PowerShell
Need a hint?

Use = to assign values to variables. Strings go inside double quotes.

2
Get the service status
Use Get-Service with the variable serviceName and store the result in a variable called serviceStatus.
PowerShell
Need a hint?

Use Get-Service -Name $serviceName to get the service info.

3
Check if the service is stopped and send email
Write an if statement to check if $serviceStatus.Status equals "Stopped". Inside the if, use Send-MailMessage to send an email to $emailTo with subject "Service Alert" and body "The Spooler service is stopped.". Use -From "monitor@example.com" and -SmtpServer "smtp.example.com" in Send-MailMessage.
PowerShell
Need a hint?

Use -eq to compare strings. The Send-MailMessage command needs all parameters as shown.

4
Print confirmation message
After the Send-MailMessage command inside the if block, add a Write-Output command to print "Alert sent to admin@example.com".
PowerShell
Need a hint?

Use Write-Output to print text to the console.