none
Running Exchange Management Shell Remotely from Powershell RRS feed

  • Question

  • 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

    Tuesday, July 21, 2015 7:14 PM

Answers

  • 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

    Thursday, July 23, 2015 8:41 PM

All replies

  • 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



    \_(ツ)_/


    • Edited by jrv Tuesday, July 21, 2015 7:30 PM
    Tuesday, July 21, 2015 7:29 PM
  • 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? 
    Wednesday, July 22, 2015 4:59 PM
  • 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.


    \_(ツ)_/

    Wednesday, July 22, 2015 5:14 PM
  • 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

    Thursday, July 23, 2015 8:41 PM
  • 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 computer.


    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.


    Monday, July 27, 2015 12:45 PM