Solved

powershell run batch file on multiple computers

Posted on 2013-01-03
13
1,696 Views
Last Modified: 2013-01-16
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
Comment
Question by:greetrufus
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 12

Expert Comment

by:Gary Dewrell
Comment Utility
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
 
LVL 12

Expert Comment

by:Gary Dewrell
Comment Utility
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
 
LVL 28

Expert Comment

by:becraig
Comment Utility
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
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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
 

Author Comment

by:greetrufus
Comment Utility
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
 
LVL 28

Expert Comment

by:becraig
Comment Utility
Is there any logic built into your batch file besides the ipconfig command  ?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:greetrufus
Comment Utility
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
 

Author Comment

by:greetrufus
Comment Utility
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
 

Author Comment

by:greetrufus
Comment Utility
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
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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
 

Author Comment

by:greetrufus
Comment Utility
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
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now