none
Run Batch file on Remote Server

    Question

  • I am attempting to write a script to run a batch file on a remote server.  I have other scripts successfully running remote commands, so I know the capability is setup properly.  Right now I am testing with a simple batch file that runs a dos command outputs the results to a text file.  I am not sure how to run it though.

    Currently my script looks like this:

    Invoke-Command -ComputerName <IP> -ScriptBlock { Start-Process -FilePath 'cmd.exe' -ArgumentList "c:\scripts\test-batch.bat" -Wait -PassThru }

    With this setup the script is continuously running until I go to the remote server and kill the 'cmd.exe' process.  If I use 'runas.exe' the script runs quickly and finishes.  In either case, I can't find the text file that should be created by the batch.  Any help on initializing that remote batch file would be greatly appreciated.

    Thursday, March 16, 2017 6:20 PM

All replies

  • You cannot "runas" remotely.  The remote session is already elevated.

    Installers cannot be easily managed remotely and must be designed for a silent install.  Check with the vendor of the installer.


    \_(ツ)_/

    Thursday, March 16, 2017 6:42 PM
  • You cannot "runas" remotely.  The remote session is already elevated.

    Installers cannot be easily managed remotely and must be designed for a silent install.  Check with the vendor of the installer.


    \_(ツ)_/

    I am attempting to run a custom batch file, not an installer.
    Friday, March 17, 2017 12:27 PM
  • What is in the batch file?

    RunAs is still not allowed.


    \_(ツ)_/

    Friday, March 17, 2017 3:30 PM
  • What is in the batch file?

    RunAs is still not allowed.


    \_(ツ)_/

    As I mentioned in the original question, the current batch file simply runs a dos command and sends the result to a text file.  I am trying to work on a proof of concept before I start writing the batch file.
    Friday, March 17, 2017 3:51 PM
  • You still cannot use "RunAs".

    To run a batch file just run it.

    Invoke-Command -ComputerName <IP> -ScriptBlock { c:\scripts\test-batch.bat }


    \_(ツ)_/

    Friday, March 17, 2017 4:15 PM
  • I'd just put the DOS commands into a powershell script instead, and then do a:

    Invoke-Command -comutername $computer -Filepath c:\path\to\script.ps1

    Keep in mind if you are using the -FilePath flag, it has to be a powershell script.

    Friday, March 17, 2017 4:24 PM