Solved

execute bat file on multiple machines

Posted on 2014-04-12
7
4,838 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…

863 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

22 Experts available now in Live!

Get 1:1 Help Now