Forum Discussion
Using PowerShell to talk to the Office 365 Service Communications API
I wrote the article that was originally referenced. It hasn't been updated for sometime.
The new Service Communications API appears to be in preview at this stage, but it's probably a good idea for me to get my article updated, i'll endevour to do that this week some time.
All the information on the new API appears here: https://msdn.microsoft.com/office-365/office-365-service-communications-api-reference
I would probably suggest using MSAL libraries this time around to simplify the obtainment of a token.
VasilMichev, Anne Michels, cammurray Thank you all.
First, I was able to use Cam's script to produe a spreadsheet. My PowerShell looks like this:
$cred = get-credential
$jsonPayload = (@{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 $jsonPayload).RegistrationCookie
$jsonPayload = (@{lastCookie=$cookie;locale="en-US";preferredEventTypes=@(2)} | convertto-json).tostring()
$events = (invoke-restmethod -contenttype "application/json" -method Post -uri "https://api.admin.microsoftonline.com/shdtenantcommunications.svc/GetEvents" -body $jsonPayload)
$events.Events | export-csv -LiteralPath "C:\temp\2017-04-03-Messages.csv" -NoTypeInformationIt works. It still needs some tweaking because one of the columns, with the message details, comes up containing objects. But it's good enough for this week. I'll improve on it next week when it is time for me to review the message center messages again. What I did after I produced the CSV is save it as an XLSX and format the data as a table. I deleted a few empty or not helpful columns and re-ordered the rest. I then added two columns, "Governance Assessment" and "Notes". I then filled in those columns as needed, documenting which Messages require further work on my part. For example, some changes "user will just figure out". Others might be something I have to discuss with a larger audience, such as the SharePoint Governane Team, the other O365 Admins, or Corporate Communications. In other words, I've solved my business need for this week, but incurred technical debt to do so.
Looking ahead, I want to use the new APIs. This means I will have to create an app registration in Azure AD. Anne Michels, are there newer instructions than https://msdn.microsoft.com/en-us/office-365/get-started-with-office-365-management-apis that show how to do that registration in the new Azure Admin Portal UI? Since this is a script that is calling the APIs rather than a web app, guidance on property values like Sign-On URL and App ID UI and Reply URL would be helpful.
Also, what permissions should it be granted if it just needs to read Message Center messages? I did not see a permission that provides that access.
cammurray it would be great if you could update your article to use the new API. As you can see from above, guidance on app registration in Azure AD is needed too. Do you know of any PowerShell examples for using the MSAL to get the token? Are the MSAL libraries something I need to install locally before I can reference them and use them in PowerShell? Where do I get them from?
Thanks,
Michael
- Apr 05, 2017
Attached is the Spreadsheet I created based on the messages I retrieved via the script. I think this is something companies will want to do weekly as part of their Office 365 governance process.
- Apr 10, 2017
Here's an improved version of the PowerShell. It still uses the old API however. This version is better because it includes the message body and adds columns for GovernanceResponse and Notes to the CSV. You still have to open it in Excel and format it as a table.
$cred = get-credential $jsonPayload = (@{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 $jsonPayload).RegistrationCookie $jsonPayload = (@{lastCookie=$cookie;locale="en-US";preferredEventTypes=@(2)} | convertto-json).tostring() $events = (invoke-restmethod -contenttype "application/json" -method Post -uri "https://api.admin.microsoftonline.com/shdtenantcommunications.svc/GetEvents" -body $jsonPayload) $outfileName = "C:\temp\Message Center\" + $(get-date).ToString("yyyy-MM-dd") + "-Messages.csv" $FormatedEvents = $events.Events | foreach { $row = new-object PSObject add-member -InputObject $row -MemberType NoteProperty -Name MessageID -Value $_.Id add-member -InputObject $row -MemberType NoteProperty -Name LastUpdatedTime -Value $_.LastUpdatedTime.toString("MM/dd/yyyy h:mm tt") add-member -InputObject $row -MemberType NoteProperty -Name Urgency -Value $_.UrgencyLevel add-member -InputObject $row -MemberType NoteProperty -Name ActionType -Value $_.ActionType add-member -InputObject $row -MemberType NoteProperty -Name Category -Value $_.Category add-member -InputObject $row -MemberType NoteProperty -Name Status -Value $_.Status #is this always null? if ($_.ActionRequiredByDate -eq $null){ add-member -InputObject $row -MemberType NoteProperty -Name ActionRequiredBy -Value "" } else { add-member -InputObject $row -MemberType NoteProperty -Name ActionRequiredBy -Value $_.ActionRequiredByDate.toString("MM/dd/yyyy h:mm tt") } add-member -InputObject $row -MemberType NoteProperty -Name Title -Value $_.Title add-member -InputObject $row -MemberType NoteProperty -Name Message -Value $_.Messages[0].MessageText add-member -InputObject $row -MemberType NoteProperty -Name AdditionalInfo -Value $_.ExternalLink add-member -InputObject $row -MemberType NoteProperty -Name GovernanceResponse -Value "" add-member -InputObject $row -MemberType NoteProperty -Name Notes -Value "" Write-Output $row } $FormatedEvents| export-csv -LiteralPath $outfileName -NoTypeInformation- Dean_GrossJun 01, 2018Silver Contributor
Michael Blumenthalhave you ever figured out how to use the new API to get the Message Center messages? this has me stumped.