Graph API – 从PowerShell中获取嵌套值并输出

huangapple go评论80阅读模式
英文:

Graph API - Get nested values from Powershell into the output

问题

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.

我正在尝试将所有Microsoft 365管理中心消息的列表放入表格中,但输出格式存在问题,因为“Services”字段是嵌套的。

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):

我有一个Azure应用程序注册和一个令牌,具有正确的权限,并通过PowerShell运行以下命令(请注意,“Get-Token”是一个使用我的应用程序注册和密钥获取令牌的函数 - 如果您已经获取了令牌,请跳过它):

$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:

如果我使用“$messages[11]”来检查一个值,例如结果如下:

 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.

如果我尝试将值“$messages”导出到CSV文件,"services"列对于每个项目都显示“System.Object[]”。

Results for "$messages[11].services | get-member" says "TypeName: System.String"

"$messages[11].services" shows just "Microsoft Teams" without the braces.

对于“$messages[11].services | get-member”的结果显示“TypeName: System.String”

“$messages[11].services”仅显示“Microsoft Teams”,不带括号。

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?

我找到了一个将该值导入CSV文件的解决方法(如下所示),但我想知道是否有更好(或更优雅)的方法来获取正确的值?

$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

你的问题已经被翻译成中文。

英文:

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

答案1

得分: 1

我的方法是将"services"列计算为字符串形式。这样输出的CSV就会符合预期,不会包含"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
英文:

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 

huangapple
  • 本文由 发表于 2023年5月14日 20:03:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定