Office 365 service health information (healthy, degraded, etc.,) can be checked at Office 365 Admin console or using Office 365 Admin App on the phone.
At least Office 365 Admin App (for iPhone/Android/Windows) alerts when there is a change in service status.
I wrote a script for old school boys and girls who want to receive the Office 365 service degradation in emails.
This script can be run in intervals using Windows Task Scheduler. There is no special requirements (PS Modules) to run the script in PowerShell.
First Step: Create a Password XML file
Copy/Paste the following two PowerShell commands to create the password XML file. We are saving just the password (of the Office 365 Administrator account) in encrypted form. When asked type the password carefully.
$text = Read-Host -AsSecureString -Prompt ‘Enter Password’
$text | Export-Clixml -Path “$home\Documents\mypassword.xml”
Note: We are only saving the password in encrypted form in XML file in the user profile. Since only the user has access to their user profile, password file is in safe location. And we are not storing the user name, so it is not useful without knowing which user this password belongs to.
Another Note: if you are going to run the script in different user context in Task Scheduler, you have move the mypassword.xml file to the user profile location of the correct user context.
Now the script:
Either copy/paste the script in NotePad and name it Get-O365-Service-Health-Alerts.ps1 or DOWNLOAD THE SCRIPT.
Important: Make sure you assign appropriate values in the beginning of the script.
<#
***********************************************************************
Script Name: Get-O365-Service-Health-Alerts.ps1
Purpose: Get the Office 365 tenant service health alerts using
JSON and send email to admins if the any degraded service is
spotted.
Prerequiste 1: mypassword.xml file in your documents folder
You have to create the password.xml file using the
following PowerShell commands:
$text = Read-Host -AsSecureString -Prompt ‘Enter Password’
$text | Export-Clixml -Path “$home\Documents\mypassword.xml”
Prerequiste 2: Assign correct values to the variables
in this script (see below)
Written by: Anand, the awesome, Venkatachalapathy
***********************************************************************
#>
#Assign the appropriate values in these variables
$username = ‘awesomeanand@company.com’
$SMTPServername = “mail-relay.company.com”
$SMTPPort = 25
$From = “Office-365-Service-Health-Alerts@company.com”
$To = “awesomeanand@company.com”
# read in the secret and encrypted password from file
$password = Import-Clixml -Path “$home\Documents\mypassword.xml”
# add the username and create a credential object
$credential = New-Object -TypeName PSCredential($username, $password)
# convert the credential to JSON format
$jsonPayload = (@{userName=$credential.username;password=$credential.GetNetworkCredential().password;} | convertto-json).tostring()
# Fetch the Office 365 sevice health status using REST
$cookie = (invoke-restmethod -contenttype “application/json” -method Post -uri “https://api.admin.microsoftonline.com/shdtenantcommunications.svc/Register” -body $jsonPayload).RegistrationCookie
$jsonPayload = (@{lastCookie=$cookie;locale=”en-US”;preferredEventTypes=@(0,1)} | convertto-json).tostring()
$events = (invoke-restmethod -contenttype “application/json” -method Post -uri “https://api.admin.microsoftonline.com/shdtenantcommunications.svc/GetEvents” -body $jsonPayload)
#Assign the service health events to a variable
$serviceEvents = $events.Events
#Check each event
foreach($event in $serviceEvents)
{
$status = $event.status
$detailedstatus = $event.AffectedServiceHealthStatus
$Title = $event.Title
$UpdatedTime = $event.LastUpdatedTime
$Messages = $event.Messages
#Check if the status is degraded
If ($status -like “*degradation” )
{
#form the mail subject
$subject = $detailedstatus.ServiceName + ” – $status at $UpdatedTime”
#display the subject
$subject
#form the mail body
$text = “”
foreach($Message in $Messages)
{
$text += $Message.MessageText
}
#Send email to the admin
Send-MailMessage -SmtpServer $SMTPServername `
-Port $SMTPPort `
-From $From -To $To `
-Subject $subject -Body $text
}
}
<#
******************* End of the script ********************
#>