I would like to execute ExchangeOnline command and convert it to JSON by selecting certain properties, all in single command!
For example, the output for my command to get RecipientPermissions
for each SharedMailbox
is:
// command
Get-EXOMailbox -RecipientTypeDetails "SharedMailbox" -ResultSize unlimited | foreach { Get-EXORecipientPermission -Identity $_.Identity }
The sample output for above command is
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
Firstly, I would like to convert the above output to JSON. So, if I simply pipe it to ConvertTo-Json
, the value for AccessRights
is lost as follows:
[
{
"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"
}
]
Secondly, I want to select only specific properties in the result, for example: Identity, Trustee, AccessRights
, rename them to arbitrary keys and still maintain the Json structure. So the o/p I actually want is:
[
{
"ShareMailbox": "SMBP",
"UPN": "2b33e774737b4da26c2e551c00f",
"Roles": [
"SendAs"
]
},
{
"SharedMailbox": "testexosv",
"UPN": "NT AUTHORITY\\SELF",
"Roles": [
"SendAs"
]
}
]
I have tried following command:
Get-EXOMailbox -RecipientTypeDetails "SharedMailbox" -ResultSize unlimited
| foreach { Get-EXORecipientPermission -Identity $_.Identity
| foreach {$_.Identity, $_.Trustee, [String]$_.AccessRights } | ConvertTo-Json}
But it is just providing the JSON array, and not JSON object. Output for above command is
[
"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
Note:
Get-EXORecipientPermission
may accept Get-EXOMailbox
output directly as pipeline input (I cannot personally verify that), in which case you can remove the ForEach-Object Identity
pipeline segment.
Select-Object
is used to extract only the properties of interest (which are added to [pscustomobject]
instances with properties of the same name containing static copies of the input objects' property values).
ConvertTo-Json
's -EnumAsStrings
switch - available in PowerShell (Core) 7+ only - ensures that instances of [enum]
-derived values - which are by default serialized by their underlying numeric values - are instead serialized by their symbolic names, i.e. as strings.