Solved

execute bat file on multiple machines

Posted on 2014-04-12
7
5,251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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 40

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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
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 …

726 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