Using the Exchange Management Shell from C#

I’m wanting to build on my previous post titled “Using PowerShell from managed code” to show how to invoke the Exchange 2010 Management Shell from managed code.

Since Exchange 2007, the primary management tool for Exchange is the Exchange Management Shell (EMS). The EMS is really just a PowerShell session with Exchange specific extensions loaded. So in addition to the normal commands like Get-ChildItem and Get-WmiObject we now also get things like Get-Mailbox and Get-ExchangeServer when the EMS is loaded.

So we’ve seen from my previous post that PowerShell can be invoked from within managed code (C#). Question is: Can we invoke the Exchange Management Shell from managed code? Affirmative! We do as follows:

Initialize a new C# project as described in: “Using PowerShell from managed code”

Create an empty PowerShell pipeline:
[csharp]
PowerShell powershell = PowerShell.Create();
[/csharp]

Load the Exchange Management Shell snap-in into the session:
[csharp]
PSSnapInException ex;
powershell
.Runspace
.RunspaceConfiguration
.AddPSSnapIn(
"Microsoft.Exchange.Management.PowerShell.E2010",
out ex);
if (ex != null)
throw ex;
[/csharp]

Execute the RemoteExchange.ps1 script in the Exchange installation’s bin directory through a dot source:
[csharp]
powershell.AddScript(
". $env:ExchangeInstallPath\\bin\\RemoteExchange.ps1");
[/csharp]

Create a remote powershell session to the local Exchange server through the Connection-ExchangeServer cmdlet:
[csharp]
powershell.AddScript("Connect-ExchangeServer -auto");
[/csharp]

Invoke the added commands and prepare the PowerShell session for further use:
[csharp]
powershell.Invoke();
powershell.Streams.ClearStreams();
powershell.Commands.Clear();
[/csharp]

At this point we are ready to execute Exchange Management Shell commands:
[csharp]
powershell.AddScript("Get-Mailbox");
[/csharp]

Now before we start writing lots of code lets first build a small wrapper class again:
[csharp]
public class EmsSession
{
protected readonly PowerShell powershell;

public EmsSession()
{
powershell = PowerShell.Create();
PSSnapInException ex;
PSSnapInException ex;
powershell
.Runspace
.RunspaceConfiguration
.AddPSSnapIn(
"Microsoft.Exchange.Management.PowerShell.E2010",
out ex);
if (ex != null)
throw ex;
powershell.AddScript(
". $env:ExchangeInstallPath\\bin\\RemoteExchange.ps1");
powershell.AddScript("Connect-ExchangeServer -auto");
powershell.Invoke();
powershell.Streams.ClearStreams();
powershell.Commands.Clear();
}

public void RunScript(string scriptText,
Action<Collection<PSObject>> handler)
{
powershell.Streams.ClearStreams();
powershell.Commands.Clear();
powershell.AddScript(scriptText, true);
var results = powershell.Invoke();
ThrowIfError();
if (handler != null)
{
handler(results);
}
}

public string RunScript(string scriptText)
{
var result = new StringBuilder();
RunScript(scriptText, results =>
{
foreach (var line in results)
{
if (line != null)
{
result.AppendLine(line.ToString());
}
}
});
return result.ToString().TrimEnd();
}

private void ThrowIfError()
{
var errors = powershell.Streams.Error;
if (errors.Count > 0)
{
var e = errors[0].Exception;
powershell.Streams.ClearStreams();
throw e;
}
}
}
[/csharp]

Add some syntactic sugar through extension methods:

[csharp]
public static class EmsExtentionMethods
{
private static readonly EmsSession emsSession
= new EmsSession();

public static string EmsExec(this string commands)
{
return emsSession.RunScript(commands);
}

public static void EmsExec(this string commands,
Action<Collection<PSObject>> handler)
{
emsSession.RunScript(commands, handler);
}
}
[/csharp]

… and enjoy playing with the Exchange Management Shell like so:
[csharp]
var mailBoxGuid =
@"$guid = Get-Mailbox hannes | Select Guid
$guid.Guid"
.EmsExec();
Console.WriteLine("GUID for Tom’s mailbox: {0}", mailBoxGuid);
[/csharp]

Hannes de Jager

A Senior Software Developer aspiring to be a Prototype Developer and/or a Software Architect. Living in Stellenbosch South Africa. I love ideas and complexities that can be combined in unexpected ways to make our life a little simpler. According to Keirsey I'm a Rational Inventor: http://www.keirsey.com/4temps/inventor.asp, which probably means I seem a bit crazy ;-)

hannesdejager has 2 posts and counting.See all posts by hannesdejager

Pin It on Pinterest