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:
However,
When I create a function with the same command, running the function does not load Exchange commands:
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.
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
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
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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...
- https://technet.microsoft.com/en-us/library/dd335083(v=exchg.160).aspx
- https://www.codetwo.com/kb/how-to-connect-to-exchange-server-via-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
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Thanks for your help.
Provided sufficient solution to user question. Closing question and assigning points.
ASKER
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.