brs
brs

Reputation: 299

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#.

Upvotes: 4

Views: 2814

Answers (2)

9cents
9cents

Reputation: 173

You haven't pasted the powershell you're using initially to get the properties from the Get-MailboxPermission cmdlet so I can't say for certain but I think that's the easiest place to edit to fix this. Try selecting the AccessRights property as an expression, I think it should solve your issue.

EG To return all mailbox permissions:

Get-Mailbox `
| Get-MailboxPermission `
| Select @{l="AccessRights";e={$_.AccessRights}}, `
          Deny, InheritanceType, User, Identity, IsInherited, IsValid

Upvotes: 0

JasonMArcher
JasonMArcher

Reputation: 14991

You need to cast the value.

foreach (Object foo in (Microsoft.Exchange.Management.RecipientTasks.MailboxRights[])obj.Properties["AccessRights"].Value)

Upvotes: 2

Related Questions