Solved

execute bat file on multiple machines

Posted on 2014-04-12
7
4,976 Views
Last Modified: 2014-04-12
Hi all, i think im almost there but need some help, im trying to run the following batch command but am getting some parameters errors. Any help appreciated.

$servers =Get-Content "C:\Users\computers.txt"

foreach ( $servers1 in $servers ) {
@"

Displaying for $servers1

"@
 $execute = invoke-command -Path C:\wsusreset.bat
0
Comment
Question by:cwstad2
  • 3
  • 3
7 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 39996558
$servers =Get-Content "C:\Users\computers.txt" | % {
invoke-command -computername $_ -scriptblock { invoke-command C:\wsusreset.bat }
}

Open in new window

0
 
LVL 15

Author Comment

by:cwstad2
ID: 39996567
Thanks, looks like winrm isnt enabled. Damn. I take it i have to log on to each server and enable it manually. I dont suppose you know of an equivalent script to exexute it with psexec?

thanks
0
 
LVL 29

Expert Comment

by:becraig
ID: 39996573
for /f %a  in (C:\Users\computers.txt) do psexec \\%a cmd /c C:\wsusreset.bat

That is from the command line

If you make it a .bat .cmd

for /f %%a  in (C:\Users\computers.txt) do psexec \\%%a cmd /c C:\wsusreset.bat
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 15

Author Comment

by:cwstad2
ID: 39996589
Thanks for the reply. I created a .bat but when i run i get "cmd exited with error code 1."
0
 
LVL 29

Accepted Solution

by:
becraig earned 500 total points
ID: 39996592
I am assuming there is something happening with the .bat you are running on the server itself or maybe an issue of permissions.


you can try this:

for /f %%a  in (C:\Users\computers.txt) do psexec -u domain\username -p password \\%%a cmd /c C:\wsusreset.bat

Can you also try running the .bat file you want to execute on the servers to see if it run without errors:
psexec \\server cmd /c c:\wsusreset.bat

What are you trying to do ?
Maybe we can find an easier way ?
0
 
LVL 15

Author Comment

by:cwstad2
ID: 39996621
worked a treat thanks
0
 
LVL 39

Expert Comment

by:footech
ID: 39996951
@cwstad2 - Just an FYI, you can enable PS Remoting via Group Policy.
http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/24/an-introduction-to-powershell-remoting-part-two-configuring-powershell-remoting.aspx

When needing to run .bat files or other external programs, here's a good resource for your options in PS.
http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

813 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

12 Experts available now in Live!

Get 1:1 Help Now