Import EMS Commands from an Exchange 2010 Server in the Local AD Site

by Mike Pfeiffer on April 13, 2010

Let's say you want to deploy a PowerShell script to your help desk group that imports the Exchange Management Shell cmdlets using implicit remoting. The benefit to this is it gives your help desk users access to the Exchange cmdlets without having to install the Exchange tools on their machine. Normally, you would have to hard code a server name into this type of script, but in this post we'll look at how you can import the commands automatically from an Exchange sever in the local AD site.

The following code uses the Get-ExchangeServersInSite function that I posted about here – this will search the local AD site and return a list of Exchange servers we can use. We'll choose a random Exchange server to connect to and import the EMS cmdlets into the local session:

#add all servers in the local site to an array
$servers = New-Object System.Collections.ArrayList
Get-ExchangeServerInSite | %{ [void]$servers.Add(($_.fqdn)) }

#select a random server from the current site
if($servers.count -gt 1) {
    $random = Get-Random -Minimum 0 -Maximum $servers.count
    $fqdn = $servers[$random]
}
else {
    $fqdn = $servers[0]
}

#create the session
$session = New-PSSession -ConfigurationName Microsoft.Exchange `
-ConnectionUri "http://$fqdn/PowerShell/" -Authentication Kerberos

#import the session
Import-PSSession $session

We'll save the Get-ExchangeServersInSite function and the above code to a script called Import-EMSCommands.ps1. When executed, the script will connect to a random Exchange server in the local Active Directory site and import the EMS cmdlets, as shown here:

You can download the entire script used in this example here.

Related Posts

{ 6 comments… read them below or add one }

Pat Richard April 28, 2010 at 5:42 am

That’s awesome. I was looking for something like this. Not only does it work well for the Help Desk folks, but also me as a scripter.

Well done!

Reply

Mike Pfeiffer April 28, 2010 at 6:45 am

Thanks Pat, glad it helps.

Reply

Ken Mabus February 1, 2011 at 5:43 am

Getting the error below while attempting the script. What gives?

PS C:\Users\kdmabus\desktop> .\importems.ps1
Missing expression after unary operator ‘-’.
At C:\Users\kdmabus\desktop\importems.ps1:15 char:2
+ – <<<< ConnectionUri "http://$fqdn/PowerShell/&quot; -Authenticat
+ CategoryInfo : ParserError: (-:String) [], Pars
+ FullyQualifiedErrorId : MissingExpressionAfterOperator

Reply

Mike Pfeiffer February 1, 2011 at 6:08 am

It seems like you might be missing the back tick (`) on line 38 in the script…I get the same error when I remove it. I used line continuation there for the sake of readability, but you could modify it so that it’s done on one line:

$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “http://$fqdn/PowerShell/” -Authentication Kerberos

Reply

XANDER October 14, 2011 at 12:49 am

Hi, I try to run script, but get error:

Import-PSSession : No command proxies have been created, because all of the requested remote commands would shadow existing local commands. Use the AllowClobber parameter if you want to shadow existing local commands.
At D:\Temp\Import-EMSCommands.ps1:42 char:17
+ Import-PSSession <<<< $session
+ CategoryInfo : InvalidResult: (:) [Import-PSSession], ArgumentException
+ FullyQualifiedErrorId : ErrorNoCommandsImportedBecauseOfSkipping,Microsoft.PowerShell.Commands.ImportPSSessionCommand

Please, help me fix this problem. Thanks in advance :)

Reply

Mike Pfeiffer October 14, 2011 at 1:06 pm

Hi,

That message means the cmdlets are already loaded. You’d want to run that script in a shell instance where the Exchange snapin/implicit remoting module has not been loaded.

Reply

Leave a Comment

Previous post:

Next post: