# PRTG-O365Status # # CustomXML-Sensor to read all Office 365 Status messages and generate a PRTG-XML-File # Simply Counting "open" issues per Product without # # 20160515 Version 1.0 FC initial Version based on some other scripts param ( $Username = "", $password = "" ) write-host "PRTG-O365Status:Start" $error.clear() [bool]$errorfound = $false [string]$prtgresult="" $prtgresult+="`r`n" $prtgresult+="`r`n" if ($Username -eq "") { $cred = get-credential -UserName $Username -Message "Office 365 Account" } else { $Securepassword = ConvertTo-SecureString -String $password -AsPlainText -Force $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $Username, $Securepassword } write-host "PRTG-O365Status: Generate JSON Request" # JSON Anfrage formulieren $jsonPayload = (` @{ UserName=$cred.Username; ` password=$cred.GetNetworkCredential().password;} ` | convertto-json).tostring() # Anmeldecookie anfordern if ($error) { write-host "Error: Invalid Parameter" $prtgresult+=" 2`r`n" $prtgresult+=" Error: Invalid Parameter`r`n" $errorfound = $true } if (!$errorfound) { try { write-host "PRTG-O365Status: Requesting Session Cookie" $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() } catch { write-host "Error: Unable to get cookie" $prtgresult+=" 2`r`n" $prtgresult+=" Error: Unable to get cookie`r`n" $errorfound = $true } } if (!$errorfound) { try { write-host "PRTG-O365Status: Retrieving Service Endpoints" $Serviceenpoints = (invoke-restmethod ` -contenttype "application/json" ` -method Post ` -uri "https://api.admin.microsoftonline.com/shdtenantcommunications.svc/GetServiceInformation" ` -body $jsonPayload) } catch { write-host "Error: Unable to get ServiceEndpoints" $prtgresult+=" 2`r`n" $prtgresult+=" Error: Unable to get events`r`n" $errorfound = $true } } if (!$errorfound) { try { write-host "PRTG-O365Status: Retrieving Status" $events = (invoke-restmethod ` -contenttype "application/json" ` -method Post ` -uri "https://api.admin.microsoftonline.com/shdtenantcommunications.svc/GetEvents" ` -body $jsonPayload) } catch { write-host "Error: Unable to get events" $prtgresult+=" 2`r`n" $prtgresult+=" Error: Unable to get events`r`n" $errorfound = $true } } if (!$errorfound) { write-host "PRTG-O365Status: Parsing Status Result" [hashtable]$statuslist = @{} $totalopen = 0 foreach ($serviceenpoint in $Serviceenpoints) { $productname = $serviceenpoint.ServiceName if (!$statuslist.containskey($productname)) { $statuslist[$productname] = [int]0 } foreach ($event in $events.Events) { if (($event.AffectedServiceHealthStatus.ServiceName -eq $productname) ` -and ($event.endtime -eq $null)){ $statuslist[$productname]++ $totalopen++ } } } write-host "PRTG-O365Status: Generating Result" $prtgresult+=" `r`n" $prtgresult+=" Total`r`n" $prtgresult+=" "+$totalopen+"`r`n" $prtgresult+=" 0`r`n" $prtgresult+=" Absolute" $prtgresult+=" Events" $prtgresult+=" 1" $prtgresult+=" 1" $prtgresult+=" 1" $prtgresult+=" `r`n" foreach ($key in $statuslist.keys) { $prtgresult+=" `r`n" $prtgresult+=" "+$key+"`r`n" $prtgresult+=" "+$statuslist[$key]+"`r`n" $prtgresult+=" 0`r`n" $prtgresult+=" Absolute" $prtgresult+=" Events" $prtgresult+=" 1" $prtgresult+=" 1" $prtgresult+=" 1" $prtgresult+=" `r`n" } } $prtgresult+="" if ($errorfound) { write-host "Error Found. Ending with EXIT Code" ([xml]$prtgresult).prtg.error } write-host "PRTG-O365Status: Sending PRTGRESULT to STDOUT" $prtgresult if ($errorfound) { exit ([xml]$prtgresult).prtg.error }