Home > Exchange Server > Function: New-LocalExchangeConnection – Ensure Your PowerShell Script is Connected to Exchange 2010

Function: New-LocalExchangeConnection – Ensure Your PowerShell Script is Connected to Exchange 2010

When writing scripts that execute commands on an Exchange server, for Exchange 2010, it’s important to ensure that you’re running within a session connected to an Exchange server, and that all Exchange cmdlets are available. In Exchange 2007, we could load the Exchange snapins. But 2010 doesn’t use snapins. Some people would say that you can simply load the modules, but loading the modules bypasses RBAC, and is thus, not recommended. Mike Pfeiffer wrote a great article Managing Exchange 2010 with Remote PowerShell that sheds some light. It’s worth a read.

A solution around this is to run a PowerShell script that comes built into Exchange 2010. This makes available the Connect-ExchangeServer cmdlet, which will connect via remote PowerShell to Exchange. We can specify a server, or let the -auto option connect to the best server via autodiscover. New-LocalExchangeConnection is a function I wrote to connect:

function New-LocalExchangeConnection	{ 
	[cmdletBinding(SupportsShouldProcess = $true)]
	param(
	)
	Write-Verbose "Checking for Exchange Management Shell"
	$Sessions = Get-PSSession | Where-Object {$_.ConfigurationName -eq "Microsoft.Exchange"}
	if (!($Sessions)){
		if (Test-Path "$env:ExchangeInstallPath\bin\RemoteExchange.ps1"){
			Write-Verbose "Exchange Management Shell not found - Loading..."
			. "$env:ExchangeInstallPath\bin\RemoteExchange.ps1"
			Write-Verbose "Exchange Management Shell loaded"
			Write-Verbose "Connecting to Exchange server"
			Connect-ExchangeServer -auto
			if (Get-PSSession | Where-Object {$_.ConfigurationName -eq "Microsoft.Exchange"}){
				Write-Verbose "Connected to Exchange Server"
			}else{
				Write-Host "An error has occurred" -ForegroundColor red
			}
		}else{
			Write-Warning "Exchange Management Shell is not available on this computer"
		}
	}else{
		Write-Verbose "Exchange Management Shell already loaded" -ForegroundColor Yellow
	}
} # end function New-LocalExchangeConnection

Calling this within your script will make ensuring that your script is running with access to Exchange cmdlets much simpler.

  1. December 28, 2011 at 9:33 am | #1

    You’ve been posting some good stuff, which is why I keep offering suggestions on taking things to the next level or to make slight tweaks that I think make it even better. The only suggestion I would make here is to take advantage of foreground (or background) color with Write-Host. Colorized output makes it very clear that what is on the screen are information messages and not any sort of pipelined output.

    Keep up the good work.

    • Pat Richard
      December 28, 2011 at 9:45 am | #2

      Done. Thanks for the suggestions.

  2. December 28, 2011 at 9:52 am | #3

    There’s a possible bug in the code, the ELSE clause may execute if Get-PSSession returns a collection of session objects.

    • Pat Richard
      December 28, 2011 at 9:55 am | #4

      The else branch should only execute if there is already a session with the config name of “Microsoft.Exchange”. Are you seeing otherwise?

      • December 28, 2011 at 10:00 am | #5

        When you execute Get-PSSession it may return a collection of session objects. In that case the following is not valid (Get-PSSession).ConfigurationName , the collection doesn’t have a ConfigurationName property.

      • December 28, 2011 at 10:28 am | #6

        This would be safer to use:

        $s = Get-PSSession | Where-Object {$_.ConfigurationName -eq ‘Microsoft.Exchange’}

        if($s)
        {
        Write-Host “Exchange Management Shell already loaded”
        }
        else
        {
        Write-Host “Exchange Management Shell not found – Loading…”

        }

        • Pat Richard
          January 21, 2012 at 2:11 pm | #7

          Thanks. Finally got around to test the suggestion. Incorporated it into the post.

  3. Ed
    January 4, 2012 at 4:14 pm | #8

    Write-Log “Checking for Exchange Management Shell” , you have not posted the write-log function :)

    Replaced it with write-host.

    • Pat Richard
      January 5, 2012 at 6:37 am | #9

      Thanks. Looks like I missed an instance when posting the code. I’ve updated the code in the post.

  1. No trackbacks yet.

Leave a Reply