Link to home
Start Free TrialLog in
Avatar of RECO_IS
RECO_IS

asked on

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.
SOLUTION
Avatar of timgreen7077
timgreen7077

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RECO_IS
RECO_IS

ASKER

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.
Oh gotcha. You may have to get assistance from another expert on that.
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

Avatar of RECO_IS

ASKER

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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RECO_IS

ASKER

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.
Provided sufficient solution to user question. Closing question and assigning points.