I’m writing a an Azure Function App in PowerShell to manage Exchange Online User’s Mailbox settings. (no Not available vs MS Graph)
I’m looking to see what Members of a Group there are. When I issue the Command
$getDistListOutput = Get-DistributionGroupMember @getDistListparms | Where-Object {$_.alias -ne $SelfIdentity} | Select-Object Identity
I can get one of three types of results in $getDistListOutput
, Null, One user or Multiple users. When I take the output $getDistListOutput
and convert it to JSON and add to the Existing JSON structure I get one of the three situations:
//No GroupMember
{
"ForwardingAddressID": "{"Identity":"smtp:[email protected]"}",
"GroupMembers": null
}
//Single GroupMember
{
"ForwardingAddressID": "{"Identity":"smtp:[email protected]"}",
"GroupMembers": "{"Identity":"smtp:[email protected]"}",
}
//Multiple GroupMembers
{
"ForwardingAddressID": "{"Identity":"smtp:[email protected]"}",
"GroupMembers": ["{"Identity":"smtp:[email protected]"}","{"Identity":"smtp:[email protected]"}"]
}
No I’m trying to map the data returned into C# Classes. The following works for the ForwardingAddressID
though I can’t seem to come up with the proper way to get the GroupMembers decoded with its 3 variants.
ExchPS_GetUserMailData_Reply getUserMailDataReplyDeserialized = JsonConvert.DeserializeObject<ExchPS_GetUserMailData_Reply>(AzureResponseDeserialized.OutputData);
if (!(string.IsNullOrWhiteSpace(getUserMailDataReplyDeserialized.ForwardingAddressID)))
{
ExchPS_UserIdentity_Data forwardingAddressIDSerializedReply = JsonConvert.DeserializeObject<ExchPS_UserIdentity_Data>(getUserMailDataReplyDeserialized.ForwardingAddressID);
if ((forwardingAddressIDSerializedReply != null) )
{
if (forwardingAddressIDSerializedReply.Identity != null)
{
ForwardMbx = true;
ForwardMbxTo = forwardingAddressIDSerializedReply.Identity;
}
else
{
ForwardMbx = false;
ForwardMbxTo = "";
}
}
}
If there is nothing in the value set for GroupMembers, don’t do anything.
If there is a [ as the First, it’s an Array.
If there is a { as the First, it’s a single.
if (!(string.IsNullOrWhiteSpace(getUserMailDataReplyDeserialized.GroupMembers)))
{
if (getUserMailDataReplyDeserialized.GroupMembers[0] == '[')
{
ExchPS_GroupMembers_Reply[] getGroupMembersSerializedReply = JsonConvert.DeserializeObject<ExchPS_GroupMembers_Reply[]>(getUserMailDataReplyDeserialized.GroupMembers);
if ((getGroupMembersSerializedReply != null))
{
if (getGroupMembersSerializedReply.First != null)
{
ForwardMbx = true;
//ForwardMbxTo = getGroupMembersSerializedReply.Identity[0].Identity;
}
else
{
ForwardMbx = false;
}
}
} else if (getUserMailDataReplyDeserialized.GroupMembers[0] == '{')
{
ExchPS_UserIdentity_Data getGroupMembersSerializedReply = JsonConvert.DeserializeObject<ExchPS_UserIdentity_Data>(getUserMailDataReplyDeserialized.GroupMembers);
if (getGroupMembersSerializedReply != null)
{
if (getGroupMembersSerializedReply.Identity != null)
{
ForwardMbx = true;
ForwardMbxTo = getGroupMembersSerializedReply.Identity;
}
else
{
ForwardMbx = false;
}
}
}
}
Classes defined:
// Single User Identity
class ExchPS_UserIdentity_Data
{
[JsonProperty(nameof(Identity))]
public string Identity { get; set; } = string.Empty;
}
// Array of User Identities
[JsonArray]
class ExchPS_GroupMembers_Reply
{
[JsonProperty(nameof(Identity))]
public string Identity { get; set; } = string.Empty;
}
class ExchPS_GetUserMailData_Reply
{
[JsonProperty(nameof(GroupMembers))]
public string GroupMembers { get; set; } = string.Empty;
[JsonProperty(nameof(ForwardingAddressID))]
public string ForwardingAddressID { get; set; } = string.Empty;
}