Powershell team will not work in C #

I am writing a C # Windows Form application to transfer Exchange 2010 mailboxes to a file location on a server in PST format. I used the example from the Powershell SDK (Runspace05) to access the Exchange cmdlets (Get-Mailbox) and filled the combo box with user mailboxes without any problems.

I am having problems getting the New-MailboxExportRequest cmdlet (the export cmdlet) and the ability to return its objects and display them in a list control. What am I missing? Thanks in advance for your help.

The code:

InitialSessionState iss = InitialSessionState.CreateDefault(); PSSnapInException warning; iss.ImportPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out warning); using (Runspace myrunspace = RunspaceFactory.CreateRunspace(iss)) { myrunspace.Open(); using (PowerShell powershell = PowerShell.Create()) { var mailbox = cmbEmailUserName.Text; var pstFile = txtFileSaveLocation.Text; const int badLimit = 100; //can be increased in necessary powershell.AddCommand("Microsoft.Exchange.Management.PowerShell.E2010\\New-MailboxExportRequest"); powershell.AddParameter("Mailbox", mailbox); powershell.AddParameter("FilePath", pstFile); powershell.Runspace = myrunspace; Collection<PSObject> results = powershell.Invoke(); foreach (PSObject thisResult in results) { lstBoxStatus.Items.Add(thisResult); } myrunspace.Close(); } } 
+4
source share
1 answer

You want to access the properties of the PSObject , not the object itself.

Try the following:

 foreach (PSObject thisResult in results) { foreach (PSPropertyInfo thisResultInfo in thisResult.Properties) { lstBoxStatus.Items.Add("Name: {0} Value: {1} Type: {2}", thisResultInfo.Name, thisResultInfo.Value, thisResultInfo.MemberType); } } 
+1
source

Source: https://habr.com/ru/post/1499203/


All Articles