How to load Exchange 2013 Management Shell into regular PowerShell using a function?

Hello,

I can't seem to figure out how to launch Exchange Management Shell using a function in regular PowerShell window.

If I run the following command in regular powershell, everything works:

. 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto -ClientApplication:ManagementShell

Open in new window


However,
When I create a function with the same command, running the function does not load Exchange commands:

Function Connect-Exchange {
    Write-Host Loading Exchange...
    . 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto -ClientApplication:ManagementShell
}

Open in new window


I suspect that this is a scoping issue but I can't figure out how to get this function to load exchange shell.

Any help would be greatly appreciated.

Thanks.
RECO_ISAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

timgreen7077Exchange EngineerCommented:
To load Exchange in normal powershell run the below cmdlets:

$usercredential = Get-Credential
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ExchangeFQDN/powershell -Credential $usercredential
Import-PSSession $session
0
RECO_ISAuthor Commented:
I have tried this method already and it does work but it takes a lot longer to load the commands.

My question is how do I make it run by using a function. Because I can run it by typing it in to the console, but if I wrap it in a function it does not.

Thanks.
0
timgreen7077Exchange EngineerCommented:
Oh gotcha. You may have to get assistance from another expert on that.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Todd NelsonSystems EngineerCommented:
Have you reviewed these references?  It's the only supported method I know to connect to Exchange using remote PowerShell.


You will need to do something more like this...

$UserCredential = Get-Credential
$SessionOpt = New-PSSessionOption -SkipCACheck:$true -SkipCNCheck:$true -SkipRevocationCheck:$true
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ExchangeFQDN/powershell/ -Credential $UserCredential -SessionOption $SessionOpt
Import-PSSession $Session

Open in new window

0
RECO_ISAuthor Commented:
Hi Todd,

This does work but it takes too long to connect this way.

In general how can I run a command using a function but as if it was typed directly into the console?
0
Todd NelsonSystems EngineerCommented:
Create a script with the commands that were provided and name it something like "ConnectToExchange.ps1".  That's what I do and it works perfectly fine.  Yes, a bit delayed, but it does have to import the Exchange modules into the PS session.   That's normal.

You want it to run faster?  Try installing the admin tools whatever domain-joined workstation you want ... https://technet.microsoft.com/en-us/library/bb691354(v=exchg.150).aspx#Anchor_4
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
RECO_ISAuthor Commented:
I ended up declaring the function as part of a profile and not part of a Module. It was the only way to get everything working correctly.

Thanks for your help.
0
timgreen7077Exchange EngineerCommented:
Provided sufficient solution to user question. Closing question and assigning points.
0
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.