I am trying to get a list of all Microsoft 365 Admin Centre Messages into a table, but I am having an issue with the output formatting, since the "Services" Field is nested.
I have an azure App registration and a token, with the correct permissions, and run the below via PowerShell (note that 'Get-Token' is a function that uses my app registration and secret to get a token - skip it if you got one already):
$headers = Get-Token
$messageuri = "https://graph.microsoft.com/v1.0/admin/serviceAnnouncement/messages?`$count=true&`$filter=lastModifiedDateTime ge 2023-05-10T23:29:23Z"
$messages = (Invoke-RestMethod -Uri $messageuri -headers $headers -Method Get).value
$messages | Select-Object id,services,lastModifiedDateTime,category,title
If I check a value using "$messages[11]", as an example result, it shows this:
id : MC554158
services : {Microsoft Teams}
lastModifiedDateTime : 12/05/2023 5:47:24 PM
category : stayInformed
title : A new experience to search within chat and channels.
If I then try to export the value "$messages" to a CSV, the "services" column shows "System.Object[]" for every item.
Results for
"$messages[11].services | get-member" says "TypeName: System.String"
"$messages[11].services" shows just "Microsoft Teams" without the braces.
I imagine that the field must be array formatted somehow, since each message within message center could relate to more than one service.
I found a workaround for getting the value into the csv (as below), but I want to know if there is a better (or more elegant) way of getting the correct value?
$finallist = @()
$total = $messages.Count
for ($i=0;$i -lt $total;$i++) {
$valuetoAdd = $messages[$i]
$valuetoAdd | Add-Member -Name "ServiceThatIadd" -MemberType NoteProperty -value $($messages[$i].services -join ',')
$finallist += $valuetoAdd
}
$finallist | export-csv messages2.csv