Solved

execute bat file on multiple machines

Posted on 2014-04-12
7
4,661 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 28

Expert Comment

by:becraig
Comment Utility
$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
Comment Utility
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 28

Expert Comment

by:becraig
Comment Utility
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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Author Comment

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

Accepted Solution

by:
becraig earned 500 total points
Comment Utility
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
Comment Utility
worked a treat thanks
0
 
LVL 39

Expert Comment

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

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Set OWA language and time zone in Exchange for individuals, all users or per database.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

772 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