Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exchange Powershell - How to invoke Exchange 2010 module from inside script?

I'm writing a script that does a number of things with AD and Exchange and just got to the part of the GUI where I need to start working with Exchange but don't see where I can manually specify to include the Exchange module. The normal process I'm familiar with is import-module activedirectory but import-module exchange doesn't work.

I performed a Get-Module -ListAvailable | Select Name, and don't see anything indicating Exchange. However, the Exchange Management Shell IS loaded on the Exchange server I'm working on.

Does anyone know how to include the Exchange 2010 module in my script so I can use the exchange-specific cmdlets internally? TIA...

like image 291
thepip3r Avatar asked May 17 '11 19:05

thepip3r


3 Answers

You can do this:

add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010

and most of it will work (although MS support will tell you that doing this is not supported because it bypasses RBAC).

I've seen issues with some cmdlets (specifically enable/disable UMmailbox) not working with just the snapin loaded.

In Exchange 2010, they basically don't support using Powershell outside of the the implicit remoting environment of an actual EMS shell.

like image 145
mjolinor Avatar answered Nov 09 '22 21:11

mjolinor


I know this is an old question, but rather than adding the snapin which is apparently unsupported, I just looked at the EMS shortcut properties and copied those commands.

The full shortcut target is:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto"

So I put the following at the start of my script and it seemed to function as expected:

. 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'
Connect-ExchangeServer -auto

Notes:

  • Has to be run in 64bit PS
  • This was tested on a server with just the Management Tools installed. It automatically connected to our existing Exchange infrastructure.
  • No extensive testing has been done, so I do not know if this method is viable. I will edit this post if I run into any issues.
like image 23
Huon Imberger Avatar answered Nov 09 '22 20:11

Huon Imberger


import-module Microsoft.Exchange.Management.PowerShell.E2010aTry with some implementation like:

$exchangeser = "MTLServer01"
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI http://${exchangeserver}/powershell/ -Authentication kerberos
import-PSSession $session 

or

add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
like image 2
Yan Gauthier Avatar answered Nov 09 '22 19:11

Yan Gauthier