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?
 
Todd NelsonConnect With a Mentor Systems 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
 
timgreen7077Connect With a Mentor Exchange 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
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
timgreen7077Exchange EngineerCommented:
Oh gotcha. You may have to get assistance from another expert on that.
0
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.