Powershell: Load Exchange Tools

I am creating an universal Powershell script for Exchange 2007/2010 installations.  

I want to have a function which checks if the Exchange SnapIn is already loaded.  Depending on the installation, it only should try to load the Exchange 2007 or Exchange 2010 SnapIn.

That's what I have at the moment which is creating a Warning as it either only finds one or the other Snapin.


function LoadExchangeTools {
	if (-not (Get-pssnapin | ? {$_.name -like 'Microsoft.Exchange'})) {
		Add-PSSnapin Microsoft.Exchange.Management.PowerShell.e2010
		Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
	}
}

Open in new window



I want a function which checks which Exchange version is installed and then only tries to load the appropriate Snapin.

Thanks!!
whenzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SubsunCommented:
Either you can just add the ErrorAction parameter to Add-PSSnapin command
function LoadExchangeTools {
	if (-not (Get-pssnapin | ? {$_.name -like 'Microsoft.Exchange*'})) {
		Add-PSSnapin Microsoft.Exchange.Management.PowerShell.e2010 -ErrorAction SilentlyContinue
		Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin -ErrorAction SilentlyContinue
	}
}

Open in new window

Or you can use the following function and see if it irks for you..
function LoadExchangeTools {

if (!(Get-pssnapin | ? {$_.name -like 'Microsoft.Exchange*'})) {
		$Reg = Get-PSSnapin -Registered | ? {$_.name -like 'Microsoft.Exchange*'}
		IF ($Reg | ? {$_.name -like '*e2010'}){
			Add-PSSnapin Microsoft.Exchange.Management.PowerShell.e2010
			}
		ElseIf($Reg | ? {$_.name -like '*Admin'}){
			Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
		}
	}
Else{
		"Exchange modules not found"
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.