Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get values from enumeration created from PowerShell

I'm working on an asp.net site that our security area will use to manage mailboxes and such in our Exchange environment. Within the site I am calling various PowerShell commands and then processing those results via C#. In one particular instance I want to display all of the mailbox permissions that are not inherited.

Here's the section of my code where I do this:

Collection<PSObject> results;
results = thisPipeline.Invoke();
myRunSpace.Close();

foreach (PSObject obj in results)
{
    //Don't include inherited rights
    if (obj.Properties["IsInherited"].Value.ToString().ToLower() != "true")
    {
        //Don't include permissions that are explicitly denied
        if (obj.Properties["Deny"].Value.ToString().ToLower() != "true")
        {
            if (obj.Properties["User"].Value.ToString().ToLower() != "nt authority\\self")
            {
                TableRow permissionRow = new TableRow();
                TableCell permissionUserCell = new TableCell();
                TableCell permissionRightsCell = new TableCell();
                Label permissionUserLabel = new Label();
                Label permissionRightsLabel = new Label();

                permissionUserLabel.Text = obj.Properties["User"].Value.ToString();
                                //This is my problem
                permissionRightsLabel.Text = obj.Properties["AccessRights"].Value.ToString();

                permissionUserCell.Controls.Add(permissionUserLabel);
                permissionRightsCell.Controls.Add(permissionRightsLabel);

                permissionRow.Controls.Add(permissionUserCell);
                permissionRow.Controls.Add(permissionRightsCell);

                table.Controls.Add(permissionRow);
            }
        }
    }
}

This results in the Access Rights column of the table displaying Microsoft.Exchange.Management.RecipientTasks.MailboxRights[].

If I try to do a foreach I receive a compiler error.

CS1579: foreach statement cannot operate on variables of type 'System.Management.Automation.PSPropertyInfo' because 'System.Management.Automation.PSPropertyInfo' does not contain a public definition for 'GetEnumerator'

I find lots of examples of how to access these values via straight PowerShell but I cannot figure out how to access them via C#.

like image 655
brs Avatar asked Dec 03 '25 18:12

brs


1 Answers

You need to cast the value.

foreach (Object foo in (Microsoft.Exchange.Management.RecipientTasks.MailboxRights[])obj.Properties["AccessRights"].Value)
like image 128
JasonMArcher Avatar answered Dec 06 '25 08:12

JasonMArcher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!