我想执行 ExchangeOnline 命令并通过选择某些属性将其转换为 JSON,所有这些都在单个命令中!
例如,我的命令为每个
RecipientPermissions
获取 SharedMailbox
的输出是:
// 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-Json
,则 AccessRights
的值会丢失,如下所示:
[
{
"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"
]
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
管道段。
ConvertTo-Json
的 -EnumAsStrings
开关 - 仅在 PowerShell (Core) 7+ 中可用 - 确保 [enum]
派生的值 - 默认情况下按其 基础数值进行序列化 - 而是通过其符号名称进行序列化,即作为字符串。