Running Exchange Management Shell Remotely from Powershell

Hi,

I am having an issue when I try to remotely run Exchange Management Shell (EMS) using the invoke-command from my powershell script. What I am trying to do is connect remotely to another server to run a powershell script that uses EMS to create a mailbox based on the argument I pass over. When I run the remote powershell script without EMS I see that the argument is passed over but it cant create the mailbox as it needs the EMS cmdlet's...however, when I connect to EMS it just hangs and does nothing. Does anybody know what could be causing this to hang? If this helps with investigation I do have the Microsoft Exchange management PowerShell snap-ins installed on my machine.  Please see my script below:


$mailbox = "andreatest77"

#Modify new-user-list.txt with login name
set-content -path "\\servername\path" -value "$mailbox"

#argument in invoke-command to pass to mailbox creation script on remote server
$arg = "c:\ts\scripts\new-user-list.txt"

#Create a remote Powershell connection to Exchange 2010 and run the new-mailbox script with argument
$ScriptBlockContent = { param ($param) C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command ". 'E:\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto" -command "C:\ts\scripts\New-O365Mailbox.ps1 c:\ts\scripts\new-user-list.txt"}
Invoke-Command -ComputerName "servername" -ScriptBlock $scriptblockcontent -argumentlist $arg

July 21st, 2015 3:17pm

Remotely you must use the remote shell. You cannot excute the local shell easily.

See: https://technet.microsoft.com/en-us/library/Dd335083(v=EXCHG.150).aspx

$UserCredential = Get-Credential
$uri='http://<FQDN of Exchange 2013 Client Access server>/PowerShell/' $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $uri -Authentication Kerberos -Credential $UserCredential Import-PSSession $Session C:\ts\scripts\New-O365Mailbox.ps1 c:\ts\scripts\new-user-list.txt


Free Windows Admin Tool Kit Click here and download it now
July 21st, 2015 3:32pm

Thanks for your answer. I do have the Exchange Management Tools installed on my server I am running the script from as well as the server I am trying to remotely run EMS in...shouldn't that allow me to run an EMS session in Exchange? 
July 22nd, 2015 1:01pm

No.

The local shell is designed to work against the local copy of Exchange.  The remote shell is designed too connect remotely and import the commands from that exchange.

What you are trying can be done but you wouldn't use Invoke if the commands are loaded locally.

Free Windows Admin Tool Kit Click here and download it now
July 22nd, 2015 1:16pm

Thanks, I decided to move my script to the local machine and run it from there since I have the EMS installed as well as Exchange Management Tools. I then used your suggestion and established a connection with our Exchange server and I was able to create a mailbox, thanks for your help!


Here is the script below if anyone wants this for their troubleshooting. Please take in mind the AD account must already exist for that user.

$samaccountname = "andreatest55"

#Modify new-user-list.txt with login name
set-content -path "e:\scripts\new-user-list.txt" -value "$samaccountname"

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri 'http://<FQDN of Exchange 2013 Client Access server>/PowerShell/' -Authentication Kerberos -Credential $UserCredential
Import-PSSession $Session
Invoke-expression -command "e:\scripts\New-O365Mailbox.ps1 e:\scripts\new-user-list.txt"
remove-pssession $session

July 23rd, 2015 4:42pm

Thanks, I decided to move my script to the local machine and run it from there since I have the EMS installed as well as Exchange Management Tools. I then used your suggestion and established a connection with our Exchange server and I was able to create a mailbox, thanks for your help!


Here is the script below if anyone wants this for their troubleshooting. Please take in mind the AD account must already exist for that user.

$samaccountname = "andreatest55"

#Modify new-user-list.txt with login name
set-content -path "e:\scripts\new-user-list.txt" -value "$samaccountname"

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri 'http://<FQDN of Exchange 2013 Client Access server>/PowerShell/' -Authentication Kerberos -Credential $UserCredential
Import-PSSession $Session
Invoke-expression -command "e:\scripts\New-O365Mailbox.ps1 e:\scripts\new-user-list.txt"
remove-pssession $session

  • Proposed as answer by Satyajit321 19 hours 50 minutes ago
Free Windows Admin Tool Kit Click here and download it now
July 23rd, 2015 8:41pm

Thanks, I decided to move my script to the local machine and run it from there since I have the EMS installed as well as Exchange Management Tools. I then used your suggestion and established a connection with our Exchange server and I was able to create a mailbox, thanks for your help!


Here is the script below if anyone wants this for their troubleshooting. Please take in mind the AD account must already exist for that user.

$samaccountname = "andreatest55"

#Modify new-user-list.txt with login name
set-content -path "e:\scripts\new-user-list.txt" -value "$samaccountname"

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri 'http://<FQDN of Exchange 2013 Client Access server>/PowerShell/' -Authentication Kerberos -Credential $UserCredential
Import-PSSession $Session
Invoke-expression -command "e:\scripts\New-O365Mailbox.ps1 e:\scripts\new-user-list.txt"
remove-pssession $session

  • Proposed as answer by Satyajit321 Monday, July 27, 2015 11:20 AM
July 23rd, 2015 8:41pm

Hi Techn0mama,

Try adding this to the mix.

PS C:\Scripts> get-help Invoke-Command -Parameter FilePath

-FilePath <String>
    Runs the specified local script on one or more remote computers. Enter the path and file name of the  script, or pipe a script path to Invoke-Command. The script must reside on the local computer or in a  directory that the local computer can access. Use the ArgumentList parameter to specify the values of  parameters in the script.

    When you use this parameter, Windows PowerShell converts the contents of the specified script file to a script block, transmits the script block to the remote computer, and runs it on the remote com

Free Windows Admin Tool Kit Click here and download it now
July 27th, 2015 8:45am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics