如何使用PowerShell从ExchangeOnline命令执行中获取JSON输出

oknwwptz  于 2023-11-20  发布在  Shell
关注(0)|答案(1)|浏览(101)

我想执行ExchangeOnline命令并通过选择某些属性将其转换为JSON,所有这些都在单个命令中!
例如,我的命令为每个SharedMailbox获取RecipientPermissions的输出是:

// command

Get-EXOMailbox -RecipientTypeDetails "SharedMailbox" -ResultSize unlimited | foreach { Get-EXORecipientPermission -Identity $_.Identity }

字符串
以上命令的示例输出为

Identity          : testexosv
 Trustee           : NT AUTHORITY\SELF
 AccessControlType : Allow
 AccessRights      : {SendAs}
 IsInherited       : False
 InheritanceType   : All

 Identity          : SMBP
 Trustee           : 2b33e774737b4da26c2e551c00f
 AccessControlType : Allow
 AccessRights      : {SendAs}
 IsInherited       : False
 InheritanceType   : None


首先,我想将上面的输出转换为JSON。所以,如果我简单地将其传递给ConvertTo-JsonAccessRights的值将丢失如下:

[
     {
       "Identity": "SMBP",
       "Trustee": "2b33e774737b4da26c2e551c00f",
       "AccessControlType": "Allow",
       "AccessRights": [
              1
        ],
       "IsInherited": false,
       "InheritanceType": "None"
     },
     {
       "Identity": "testexosv",
       "Trustee": "NT AUTHORITY\\SELF",
       "AccessControlType": "Allow",
       "AccessRights": [
          1
        ],
       "IsInherited": false,
       "InheritanceType": "All"
     }
  ]


其次,我想在结果中只选择特定的属性,例如:Identity, Trustee, AccessRights,将它们重命名为任意键,并仍然保持JSON结构。所以我实际上想要的o/p是:

[
     {
       "ShareMailbox": "SMBP",
       "UPN": "2b33e774737b4da26c2e551c00f",  
       "Roles": [
              "SendAs"
        ]
     },
     {
       "SharedMailbox": "testexosv",
       "UPN": "NT AUTHORITY\\SELF",
       "Roles": [
          "SendAs"
        ]
     }
  ]


我试过以下命令:

Get-EXOMailbox -RecipientTypeDetails "SharedMailbox" -ResultSize unlimited 
 | foreach { Get-EXORecipientPermission -Identity $_.Identity 
 | foreach {$_.Identity, $_.Trustee, [String]$_.AccessRights } | ConvertTo-Json}


但它只是提供JSON数组,而不是JSON对象。

[
   "SMBP",
   "2b33e774737b4da26c2e551c00f",
   "SendAs"
 ]
 [
   "testexosv",
   "NT AUTHORITY\\SELF",
   "SendAs"
  ]

kxkpmulp

kxkpmulp1#

Get-EXOMailbox -RecipientTypeDetails "SharedMailbox" -ResultSize unlimited |
 ForEach-Object Identity |
 Get-EXORecipientPermission |
 Select-Object Identity, Trustee, AccessRights |
 ConvertTo-Json -EnumAsStrings

字符串
注意事项:

  • Get-EXORecipientPermission可以接受Get-EXOMailbox输出 * 直接 * 作为管道输入(我个人无法验证),在这种情况下,您可以删除ForEach-Object Identity管道段。
  • Select-Object用于只提取感兴趣的属性(这些属性被添加到[pscustomobject]示例中,这些示例具有包含输入对象属性值的静态副本的同名属性)。
  • ConvertTo-Json-EnumAsStrings开关(仅在PowerShell (Core) 7+中可用)确保[enum]派生值的示例(默认情况下由其 * 底层数值 * 序列化)改为由其 * 符号名称 * 序列化,即作为 * 字符串 *。

相关问题