0

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
Daniel
  • 56
  • 2

1 Answers1

1

My approach would be to make "services" a calculated column that I convert to a string. That will output a CSV just as expected without "System.String[]".

$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, @{ N = 'services'; E = { $PSItem.services | Out-String }}, lastModifiedDateTime, category, title 
  • That does kind of work, but it also puts in a "new line" when there is multiple services impacted in the column. Do you know of a way to remove it? i added in "-nonewline", but it doesn't seem to like it – Daniel May 18 '23 at 00:05
  • 1
    Then you can put in the "E = {" scriptblock the following "$PSItem.services -join ';' | Out-String". – Josua Doering May 19 '23 at 06:34