• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2048
  • Last Modified:

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?

Thanks!
0
greetrufus
Asked:
greetrufus
  • 5
  • 4
  • 2
  • +1
1 Solution
 
Gary DewrellSenior Network AdministratorCommented:
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.
0
 
Gary DewrellSenior Network AdministratorCommented:
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.
0
 
becraigCommented:
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.
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
QlemoC++ DeveloperCommented:
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

0
 
greetrufusAuthor Commented:
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
          ipconfig.exe

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.
0
 
becraigCommented:
Is there any logic built into your batch file besides the ipconfig command  ?
0
 
greetrufusAuthor Commented:
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.
0
 
greetrufusAuthor Commented:
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!!!
0
 
greetrufusAuthor Commented:
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!!!!!
0
 
QlemoC++ DeveloperCommented:
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.
0
 
greetrufusAuthor Commented:
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!!!
0
 
QlemoC++ DeveloperCommented:
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.
0
 
QlemoC++ DeveloperCommented:
I'm not clear about what you tried. Executing
"for /L %I in (1,1,3) do echo loop %I" | invoke-command -script { cmd.exe /D /Q }

Open in new window

will output the values 1 to 3 as expected. Remember that with this method we won't execute a batch, but more perform interactive commands outside of any batch. Hence FOR vars may have only one preceeding percent sign.

It is not the best of all methods, as certain restrictions apply, and the output contains the cmd banner and prompt strings. Running the script locally or from a network share is still the better way (usually). But if we want to do something similar to what you started with:
$bat = @"
@echo off
for /L %%I in (1,1,3) do echo loop %%I
"@

invoke-command -script {
   $f = "$env:temp\a.cmd"
   out-file -encoding ASCII -InputObject $bat $f
   cmd /D /Q /C $f
   remove-item $f
}

Open in new window

This creates the .cmd file locally, executes and then removes it.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now