get office 365 service status to your email

#import 365 module for service communications api

Import-Module O365ServiceCommunications

#encrypt password for O365 local username
$mypass = cat C:\scripts\O365Passwd.txt | convertto-securestring
$mycreds = new-object -typename System.Management.Automation.PSCredential -argumentlist “[email protected]”,$mypass

# gather events from the Service Communications API
$MySession = New-SCSession -Credential $mycreds

$Events = Get-SCEvent -EventTypes Incident -Pastday 1 -SCSession $MySession |
Select-Object Id, Status, StartTime,
@{n=’ServiceName’; e={$_.AffectedServiceHealthStatus.servicename}},
@{n=’Message’;e={$_.messages[0].messagetext}}

if ($Events)
{
$Tables = foreach ($Event in $Events)
{
@”

Id ServiceName Status StartTime
$($Event.Id) $($Event.ServiceName) $($Event.Status) $($Event.StartTime)
$($Event.Message)

“@

}

$Html = @”

 
$Tables

“@

$Splat = @{
SmtpServer = ‘x.x.x.x’
Body = $Html
BodyAsHtml = $true
To = ‘your email address’
From = ‘[email protected]
Subject = ‘Office 365 Service Health Alerts’
Priority = ‘High’
}
Send-MailMessage @Splat
}