Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

execute cmd script on multiple computers

Posted on 2013-10-30
14
Medium Priority
?
2,105 Views
Last Modified: 2014-03-11
I have a script script.cmd which I want to execute on half of the computers in our organization.
Could you tell me how to do it?
Most of workstations are XP SP3, and a couple win7
0
Comment
Question by:dedri
[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
  • 6
  • 4
  • 4
14 Comments
 
LVL 18

Expert Comment

by:Steven Harris
ID: 39612706
Try using a PowerShell .ps1 file with this script from Mohamed Garrana:

# ============================================================================================== 
#   
# Script Name : Run Remote cmd.exe Commands 
#  
# AUTHOR: Mohamed Garrana  
# DATE  : 4/12/2010 
#  
# COMMENT:  
# this script invokes whatever command you can use in cmd.exe on one or more computers 
#you input the command name as a screen input when you run the script 
#you can use all cmd.exe command like [del,ipconfig /flushdns,ipconfig /registerdns,gpupdate /force ,notepad.exe,defrag c:, ... 
#..wuauclt /detectnow,powercfg,net start ,net stop,copy,arp,wscript.exe ....] 
#if you can do it from cmd.exe you can do it here on multiple computers at the same time 
# ============================================================================================== 
 
 
function Run-RemoteCMD { 
 
    param( 
    [Parameter(Mandatory=$true,valuefrompipeline=$true)] 
    [string]$compname) 
    begin { 
        $command = Read-Host " Enter command to run" 
        [string]$cmd = "CMD.EXE /C " +$command 
                        } 
    process { 
        $newproc = Invoke-WmiMethod -class Win32_process -name Create -ArgumentList ($cmd) -ComputerName $compname 
        if ($newproc.ReturnValue -eq 0 ) 
                { Write-Output " Command $($command) invoked Sucessfully on $($compname)" } 
                # if command is sucessfully invoked it doesn't mean that it did what its supposed to do 
                #it means that the command only sucessfully ran on the cmd.exe of the server 
                #syntax errors can occur due to user input  
     
     
     
     
    } 
    End{Write-Output "Script ...END"} 
                 } 
     
 
#---------------- 
#you can use this script to run any command that can be run on CMD.EXE 
#the following is only to give you an idea how can you use it 
#----------------- 
#for copying files from many remote computers to a single 
# get-content c:\servers.txt | Run-Remotecommand 
#Enter command to run: copy c:\log\log.txt d:\ 
#you only input "copy c:\log\log.txt d:\" 
#--------------------------------------- 
#for forcing group policy update on multiple computers 
# get-content c:\servers.txt | Run-Remotecommand 
#Enter command to run: gpupdate /force 
#-------------------------------------- 
#for stopping the Bits service on multiple computers 
# get-content c:\servers.txt | Run-Remotecommand 
#Enter command to run: Net stop bits 
#--------- 
#you can always run it against a single server using  
#Run-RemoteCommand server1 
#Enter command to run: enter whatever you'd normally enter in cmd.exe shell 
 
 
 

Open in new window

1
 
LVL 18

Expert Comment

by:Steven Harris
ID: 39612733
If you want to stay away from PowerShell, you can use PsExec from the Sysinternals toolkit:

Make a list of servers/machines - Servers.txt  and then run -

psexec @Servers.txt -c Script.cmd

This will copy over the script and then execute on each machine.
0
 
LVL 56

Expert Comment

by:McKnife
ID: 39612971
3rd possible way: simply wrap that script into an msi using any msi wrapper like exetomsi or wiww. That way, you don't even need to power on those machines, they will do "install" that setting whenever they power on and decide to accept that policy.
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:dedri
ID: 39613549
ThinkSpaceSolutions, could you explain how to work with first script.
I found a script in this link which uses psexec, but I have a problem running it.
http://community.spiceworks.com/scripts/show/446-remote-execution-on-multiple-computers

If I run it manually here is the result
C:\temp\ExecMC>psexec \\computer1 -c \\server\share\scripts\Unreg.cmd

Here is the content of the cmd file
########

@Echo off

Echo *
Echo * SIGNME AS COM SERVER
Echo *

signme.exe /unregserver

Echo *
Echo * REGISTER ASSEMBLY
Echo *

%WINDIR%\Microsoft.NET\Framework\v1.1.4322\regasm /unregister signme.base.dll /tlb /v

rem fin:

##########

------------Here is the result from execution of the command---------

PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com


*
'signme.exe' is not recognized as an internal or external command,
operable program or batch file.
* SIGNME AS COM SERVER
*
*
* REGISTER ASSEMBLY
*
Microsoft (R) .NET Framework Assembly Registration Utility 1.1.4322.573
Copyright (C) Microsoft Corporation 1998-2002.  All rights reserved.

RegAsm error: Unable to locate input assembly: 'signme.base.dll'
Unreg.cmd exited on computer1 with error code 0.

--------------------------------

If I psexec the computer, browse the script on local folder and run it , it's executed successful.
Any idea how to fix it?
0
 
LVL 18

Expert Comment

by:Steven Harris
ID: 39613881
This is how I use it:

First, you should navigate to the location of the folder that file is located on your local machine.  For example, my scripts are stored on my file server 'TSShare' (mapped as T:\) under the Scripts folder.

T:\Scripts>

Then I call my list of servers, add -c to copy to the servers, then the script:

T:\Scripts> @T:\Servers\ServerSet1.txt -c Script1.bat

Is this the same format you are using?
0
 

Author Comment

by:dedri
ID: 39614286
sorry, I didn't understand your answer.

What I am doing is the following:
psexec \\computer1 -c \\server\share\scripts\Unreg.cmd
or
psexec \\computer1 Unreg.cmd
or anything similar

And the output is

------------Here is the result from execution of the command---------

PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com


*
'signme.exe' is not recognized as an internal or external command,
operable program or batch file.
* SIGNME AS COM SERVER
*
*
* REGISTER ASSEMBLY
*
Microsoft (R) .NET Framework Assembly Registration Utility 1.1.4322.573
Copyright (C) Microsoft Corporation 1998-2002.  All rights reserved.

RegAsm error: Unable to locate input assembly: 'signme.base.dll'
Unreg.cmd exited on computer1 with error code 0.

--------------------------------

If I do this:

psexec \\computer1 cmd
and on the prompt I run Unreg.cmd it is executed successful.

Anything different on both cases and idea how to make it working.
0
 
LVL 18

Expert Comment

by:Steven Harris
ID: 39614356
Try creating a copy of your .cmd and changing the association to .bat.
0
 

Author Comment

by:dedri
ID: 39616072
I tried changing the script file from .cmd to .bat , but the problem still exist, the same error message as above.
0
 
LVL 56

Expert Comment

by:McKnife
ID: 39621979
Why not use a wrapper like I recommended? As I described: it holds even more advantaged compared to psexec.
0
 

Author Comment

by:dedri
ID: 39624094
McKnife, could you explain in more details your solution.
I cannot understand what to do when I  create a msi package for this script.
0
 
LVL 56

Accepted Solution

by:
McKnife earned 1000 total points
ID: 39624505
An MSI-Wrapper creates MSI-packages of a special kind: those packages execute commands or as a second option, start setup.exe files using parameters for silent installation. In your case, we want to execute a custom script.
So download wiww from vinsvision and install it. Create your custom MSI package from your script, assign it to computers and there you go: next time the computers start, the MSI gets installed and with it, your script gets executed using the installation account (=system account) once on every system.
0
 

Author Comment

by:dedri
ID: 39695481
sorry for late response. I haven't got any time recently. I'll try your solution and give you reply.
0
 
LVL 56

Expert Comment

by:McKnife
ID: 39877719
Please respond or finalize it, this question is growing old :)
0
 

Author Closing Comment

by:dedri
ID: 39920678
sorry, I didn't have time to test your solution. I was appointed to another project
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
By default Outlook 2016 displays only one time zone in the Calendar. The following article explains how to display two time zones in one calendar view.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

670 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