Skip to content

Instantly share code, notes, and snippets.

@alexinnes
Last active February 19, 2016 08:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexinnes/0b6983c75e30ea7f5e99 to your computer and use it in GitHub Desktop.
Save alexinnes/0b6983c75e30ea7f5e99 to your computer and use it in GitHub Desktop.
Gets the health events from O365 via API call.
<#
.Synopsis
Gets office 365 events
.DESCRIPTION
Users the Office 365 API to get all events.
.EXAMPLE
get-O365Events
.EXAMPLE
get-O365Events -last 5
[Can add username in now]
get-O365Events -username email@address.com
#>
function Get-O365Events
{
[CmdletBinding()]
[Alias()]
[OutputType([int])]
Param
(
# Param1 help description
[Parameter(Mandatory=$false,
ValueFromPipelineByPropertyName=$false,
Position=0)]
[int]$last = 0,
[string]$username
)
Begin
{
Try{
$cred = Get-Credential -Message "Office 365 Credentials" -UserName $username
$Json = (@{userName=$cred.username;password=$cred.GetNetworkCredential().password;} | convertto-json).tostring()
$cookie = (invoke-restmethod -contenttype "application/json" -method Post -uri "https://api.admin.microsoftonline.com/shdtenantcommunications.svc/Register" -body $json).RegistrationCookie
#preferredEventType -- "0" to represents a Service Incident, "1" to represent a Maintenance Event, and "2" to represent a Message Center communication
$load = (@{lastCookie=$cookie;locale="en-US";preferredEventTypes=@(0,1,2);} | convertto-json).tostring()
$events = (invoke-restmethod -contenttype "application/json" -method Post -uri "https://api.admin.microsoftonline.com/shdtenantcommunications.svc/GetEvents" -body $load)
}
Catch{
Write-Warning -Message "Please Check your credentials"; break
}
Finally{}
}
Process
{
$newEvent = foreach($evnt in $events.events){
New-Object psobject -Property @{
ID = $evnt.ID
Title = $evnt.Title
ServiceAftected = $evnt.AffectedServiceHealthStatus.servicename
Status = $evnt.Status
Time = $evnt.starttime
LastUpdated = $evnt.LastUpdatedTime
Message = $evnt.messages.messagetext
}
}
}
End
{
if($last -eq 0){$last = 10}
$newEvent| sort -property Time | select -Property Title,ServiceAftected,Status, Time,LastUpdated, message -Last $last | fl
}
}
@alexinnes
Copy link
Author

API has stopped working - I will need to update this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment