Search code examples
c#powershellexchange-server

How to get values from enumeration created from ExchangeManagement Shell


How to get the AccessRights List for the identity in C Sharp.
where I tried to cast the obj.Properties["AccessRights"].Value to PSObject,PSObject[],object etc..\

But everything resulted in Microsoft.Exchange.Management.RecipientTasks.MailboxRights[] cannot be casted. and I'm specifically using Microsoft.NET\Framework64\v2.0.50727

String execCommand = "Get-MailboxPermission -Identity \"{OBJECT_GUID}\"";
results = executeCommand(execCommand,ref errString);

foreach (PSObject obj in results) {
\\ get the list of accessrights here
\\ obj.Properties["AccessRights"].Value;
}

Solution

  • You can cast the AccessRights Properties to Array and so you will get the result from the 1st element of the array.

    Array accessRights = (obj.Properties["AccessRights"].Value as Array);
    String result = accessRights.GetValue(0).ToString();
    

    by using this way no need to cast the AccessRights Properties to Microsoft.Exchange.Management.RecipientTasks.MailboxRights[].