Scenario: You want to monitor the ActiveSyncProxy server component state. If ActiveSyncProxy becomes inactive, you want the script to automatically resolve the issue and to send an alert via email.
Script:
#Collect Servers $1 = Get-exchangeserver Ex* | Get-servercomponentstate | Where {($_.Component -eq "ActiveSyncProxy") -and ($_.state -ne "Active")} #Loop Servers for Component State $1 | Select -ExpandProperty Identity | %{$s = [string]$_;Set-servercomponentstate $s -component ActiveSyncProxy -state Active -Requester HealthAPI} #Send an Email If($1 -ne $null){ $1_body = $1 | Out-String #Email $body ="" $smtp = "smtp.domain.com" [string[]]$to = "Steve@Domain.com","Fred@Domain.com" $from = "EASMonitor@domain.com" $subject = "ActiveSync Monitor - Activesync is currently not working on a server" $body += "<b><Font color=Black>There could be an issue with ActiveSync as the server component state is not active on the following servers: </b></font><br><br>" $body += "<Font color=red>$1_body </font><br><br><br>" #Send Email send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high }