Link to home
Start Free TrialLog in
Avatar of greetrufus

asked on

powershell run batch file on multiple computers

In enterprise, many still use batch files instead of PS so I have to find a way to continue to use batch files for now.
We currently use PSEXEC but it is being shelved for security reasons.

Issue:  I need to run a batch file on 15 servers.  The batch file is local.
I would like to use powershell.
Is this possible?
The only way I can figure out how to do this is  using Invoke-Command
1. copy the .bat file to a tmp folder on the target server
2.  run the bat file
3. remove it.

Is there a way to run a local batch file on a remote server using powershell?

Avatar of Gary Dewrell
Gary Dewrell
Flag of United States of America image

We place all of our batchiles/PS scripts on a network share that all servers by default have a mapped drive to. That way it appears as they on the local server.
I have not tried it but maybe you could do something like:

Use invoke command to map a drive from the remote server to your local server/pc.
Then invoke that batch file from the mapped drive.
Avatar of becraig
You can do two things I know of:

1. Use net use and specify the username and password as a part of your invoke-command script

2. Pass in the path as a variable in your argument list and map it then call from that drive.
One way is to paste the content of the batch file, and redirect it to invoke-command:
get-content c:\Batch.cmd | invoke-command -computer $PC -script { cmd.exe /D /Q } | out-null

Open in new window

Avatar of greetrufus


Qlemo, I think I have the idea your going at.
Let me tell you my test setup.

c:\scripts\Test.bat for testing which contains:
         @echo TEST IPCONFIG

get-content C:\Scripts\test.bat | invoke-command -computer SERVER01 -script { cmd.exe /D /Q } | out-null

Open in new window

So i should get back ipconfig info as a result, correct?

Script runs with no errors but nothing returns.
Is there any logic built into your batch file besides the ipconfig command  ?
Yes.  I was using the above for testing.
I am building a scripting app that can run PS and batch on multiple remote servers.
So I am sure there will be some advanced cmd scipting at some point.

At this point, it is more proof of concept on the best way to use PS to do this.
We have been using PSEEXEC but am being forced to remove it.
So I got this to work based off Qlemo's approach and it works for simple batch.
I have not tested any advanced files yet.
The key was using the /c && to seperate the commands sent to cmd.exe
Here is what worked:

#Read the content of the batch file.  && is the command line separator for strings in cmd.exe
$Getbatch = (get-content C:\Scripts\test.bat) -join "&&" 

#Pass the content to Invoke-command
Invoke-Command -computername SERVER01 {PARAM($myArg) cmd.exe /c "$myArg"} -ArgumentList "$Getbatch"

Open in new window

Hope that helps someone else.  If i have different findings for more advanced batches, i'll post an update.

Thanks everyone!!!
I've requested that this question be closed as follows:

Accepted answer: 0 points for greetrufus's comment #a38741951
Assisted answer: 500 points for Qlemo's comment #a38740587

for the following reason:

Qlemo's logic led to my conclusion.  Great job!!!!!
I've redirected the output of the "batch" to the NULL device with    | out-null  , as I thought you were not interested in any output.

&& is not the command separator. It is the "if success then execute" operator, that is the prior command has to execute without any "error". It is convenient to use in combination with "conditional" commands like
  echo 1| find "2" >nul && echo Yes || echo No
   echo 2| find "2" >nul && echo Yes || echo No

You should not have to use any special command separator (and then not use /C), or provide the single ampersand   &   as separator.
After some research I see what you are saying.
using /C and && would alow me to execute multiple cmd commands but will not handle batch scripting.
I went back to your original post and instead of a commands in the batch file, i substituted a For Each statement:
     for /L %%I in (1,1,3) do echo loop %%I
I also removed the Out-Null to see what the return was.
When i run the PS script, the return is the string?

How is the piped content getting feed to the -scriptbock?

Moderator, can this question be reopened? or should I start another?
Sorry for the hassle!!!
To stop the process of closing this question, just post a comment and press Object instead of Submit. I have done that now, so the question stays open.
Avatar of Qlemo
Flag of Germany image

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