Complete the code to send an email alert using PowerShell's Send-MailMessage cmdlet.
Send-MailMessage -To "admin@example.com" -From "monitor@example.com" -Subject "Alert" -Body "Disk space low" -SmtpServer [1]
The -SmtpServer parameter requires the SMTP server address to send the email. "smtp.example.com" is a valid SMTP server address.
Complete the code to check if free disk space on C: drive is less than 10 GB.
$freeSpace = (Get-PSDrive -Name C).Free; if ($freeSpace [1] 10GB) { Write-Output "Low disk space" }
The operator -lt means 'less than'. We want to check if free space is less than 10GB.
Fix the error in the script to send an email alert only if disk space is low.
if ($freeSpace [1] 5GB) { Send-MailMessage -To "admin@example.com" -From "monitor@example.com" -Subject "Disk Alert" -Body "Disk space critically low" -SmtpServer "smtp.example.com" }
The condition should check if free space is less than 5GB to trigger the alert, so -lt is correct.
Fill both blanks to create a hashtable with drive letter as key and free space as value, filtering drives with more than 20GB free.
$drives = Get-PSDrive | Where-Object { $_.Free [1] 20GB } | ForEach-Object { @{ $_.Name = $_.Free [2] } }The filter uses -gt to select drives with free space greater than 20GB. The hashtable uses = to assign the free space value to the drive letter key.
Fill the blanks to create a script that checks disk space, sends alert if below threshold, and logs the event.
$threshold = 15GB $freeSpace = (Get-PSDrive -Name D).Free if ($freeSpace [1] $threshold) { Send-MailMessage -To "admin@example.com" -From "monitor@example.com" -Subject "Disk Alert" -Body "Drive D low space" -SmtpServer [2] Add-Content -Path "C:\Logs\disk_alert.log" -Value "Drive D low space at $(Get-Date)" } else { Write-Output "Disk space sufficient" }
The condition uses -lt to check if free space is less than threshold. The SMTP server is "smtp.example.com" to send the email.