Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After a lot of trial and error, I finally figured this out. The problem with the code above is it works great when run against Exchange 2007 but things have changed in Exchange 2010. Instead of the snapin called "Microsoft.Exchange.Management.PowerShell.Admin", use this snapin, "Microsoft.Exchange.Management.PowerShell.E2010".</p> <p>The complete code to run a Powershell command from C# looks like this. Hope this helps someone else trying to do this.</p> <p>You will need references to System.Management.Automation.Runspaces, System.Collections.ObjectModel and System.Management.Automation also.</p> <p>I found that the reference to System.Management.Automation had to be manually added to the csproj file itself in the ItemGroup section using notepad like this:</p> <pre><code> &lt;Reference Include="System.Management.Automation" /&gt; </code></pre> <p>code below:</p> <pre><code>private class z_test { //set up private RunspaceConfiguration rsConfig = RunspaceConfiguration.Create(); private PSSnapInException snapInException = null; private Runspace runSpace; private void RunPowerShell() { //create the runspace runSpace = RunspaceFactory.CreateRunspace(rsConfig); runSpace.Open(); rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapInException); //set up the pipeline to run the powershell command Pipeline pipeLine = runSpace.CreatePipeline(); //create the script to run String sScript = "get-mailbox -identity 'rj'"; //invoke the command pipeLine.Commands.AddScript(sScript); Collection&lt;PSObject&gt; commandResults = pipeLine.Invoke(); //loop through the results of the command and load the SamAccountName into the list foreach (PSObject results in commandResults) { Console.WriteLine(results.Properties["SamAccountName"].Value.ToString()); } pipeLine.Dispose(); runSpace.Close(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload