Friday, 10 April 2015

C# - Get PowerShell Output from MultiValuedProperty Value

In C#, you can execute powershell commands and get output in separate runspace. The following C# example returns the exchange server's mailbox audit report. The audit setting fields (AuditDelegate,AuditAdmin,AuditOwner) are multi-valued property field, so we can not read value directly from this field. The datatype of this field is "Deserialized.Microsoft.Exchange.Data.Directory.ADMultiValuedProperty`1[[Microsoft.Exchange.Data.Directory.MailboxAuditOperations, Microsoft.Exchange.Data.Directory, Version=14.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]". So, here, I have written the C# code to extract value from ADMultiValuedProperty field.
public static void GetMailBoxAuditReport()
{
    string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
    var secured = new SecureString();
    foreach (char letter in "MyPassword")
    {
        secured.AppendChar(letter);
    }
    secured.MakeReadOnly();
    var credential = new PSCredential(@"MyExchDomain\Administrator", secured);
    var connectionInfo = new WSManConnectionInfo(false, "MyExchServer", 5985, "/wsman", shellUri, credential);
    Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
    runspace.Open();
    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.AddScript("Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010");
    pipeline.Commands.AddScript("Get-Mailbox | Select Name, AuditEnabled, AuditLogAgeLimit,AuditAdmin,AuditDelegate,AuditOwner");
    Collection<PSObject> results = pipeline.Invoke();
    if (results.Count > 0)
    {
        foreach (PSObject psObject in results)
        {
            List<string> output = new List<string>();
            foreach (PSPropertyInfo psInfo in psObject.Properties)
            {
                if (psInfo.Value != null)
                {
                    string dataType = psInfo.TypeNameOfValue;
                    if (dataType.Contains("ADMultiValuedProperty"))
                    {
                        string multiValueOutput = string.Empty;
                        PSObject childObj = (PSObject)psInfo.Value;
                        foreach (PSObject info in (ArrayList)childObj.ImmediateBaseObject)
                        {
                            multiValueOutput += info.ToString() + ";";
                        }
                        output.Add(multiValueOutput);
                    }
                    else if (dataType.Equals("System.String[]"))
                    {
                        string[] childObj = (string[])psInfo.Value;
                        output.Add(string.Join(";", childObj));
                    }
                    else
                    {
                        output.Add(psInfo.Value.ToString());
                    }
                }
            }
            Console.WriteLine(string.Join(";", output.ToArray()));
            Console.WriteLine("----------------------");
        }
    }
}


Advertisements
Advertisements

No comments:

Post a Comment