Anda di halaman 1dari 5

Method 1: Use Sysinternals' PSExec ^

The most common way to invoke commands remotely is by using PSExec. This is
a classic command line tool by SysInternals, that can easily invoke a command on
a remote computer/s and redirect the output to your local command shell.

You will need to download PSExec to your computer.

The next step would be to prepare a simple TXT file with the names of the
computers on which you want to run the command, one name per line.

In this example, we will use a plain text file named 'CompList.txt' which looks like


1 Tssrv01
2 Tssrv02
3 Tssrv03
4 Tssrv04
5 Tssrv05
6 Tssrv06
7 Tssrv07
8 Tssrv09
9 Tssrv10

In this scenario well run the following short script, which cleans the Windows
spooler directory and restarts the Print Spooler service, which helps overcome
some common printing issues:


1 net stop spooler /y

2 del %windir%\system32\spool\PRINTERS\*.* /f /q
3 net start spooler
4 net start cpsvc
Now, assuming the commands above are saved to a batch file named
'CleanSpool.bat' which is located in C:\temp directory, enter the following
command on your computer:

1 Psexec -c -f @c:\temp\complist.txt c:\temp\cleanspool.bat

This is a sample output of the command:

1 C:\Windows\system32>net stop spooler /y

2 The Print Spooler service is stopping.
3 The Print Spooler service was stopped successfully.
5 C:\Windows\system32>del C:\Windows\system32\spool\PRINTERS\*.* /f /q
7 C:\Windows\system32>net start spooler
8 The Print Spooler service is starting.
9 The Print Spooler service was started successfully.
11 C:\Windows\system32>net start cpsvc
13 C:\WINDOWS\system32>net stop spooler /y
14 The following services are dependent on the Print Spooler service.
15 Stopping the Print Spooler service will also stop these services.
17 Citrix Print Manager Service
19 The Citrix Print Manager Service service is stopping..
20 The Citrix Print Manager Service service was stopped successfully.
22 The Print Spooler service is stopping.
23 The Print Spooler service was stopped successfully.
25 C:\WINDOWS\system32>del C:\WINDOWS\system32\spool\PRINTERS\*.* /f /q
27 C:\WINDOWS\system32>net start spooler
28 The Print Spooler service is starting.
29 The Print Spooler service was started successfully.
31 C:\WINDOWS\system32>net start cpsvc
32 The Citrix Print Manager Service service is starting.
33 The Citrix Print Manager Service service was started successfully.

As you can see, it's fairly easy to run the command, but analyzing the output can
be a pain. While your script may run successfully on some computers, you might
want to spot those machines on which the service could not be restarted, or on
which the script could not terminate successfully for any other reason. Especially
if the command you're running produces several output lines, it may take a while
to spot these errors as the output is spilled continuously on your command prompt.

Another thing that might prove time consuming, is preparing the list of servers.
Assuming your servers are named CTXSRV150 to CTXSRV274 (with several skips),
you would have to create a script that creates the list, rather than manually
copying and pasting it over and over. On the other hand, if your computer names
follow a continuous numeric pattern, you might prefer to leverage the good old
FOR command like this:

1 For /l %i in (150,1,274) do psexec \\CTXSRV%i c:\temp\cleanspool.bat

This way, your command will be executed one-by-one on all servers within the
above numeric range. (Tip: if you want to use the FOR statement within a batch
file, use %% instead of %).

Method 2: Use WMI to run remote commands ^

As you probably know, Microsoft has integrated WMI (Windows Management
Infrastructure) on all of its operating systems. In few words, WMI is a framework
that allows you to retrieve management data and invoke functions, while
abstracting the API level.

Creating a script which uses WMI is a bit more difficult, but once your script is
ready, you can re-use it over and over with minor changes. The major advantage
in using a WMI script is that your flexibility is almost infinite. You can analyze the
output within the script, notify upon failures and even run corrective actions.

The disadvantages, however, is that you have to work hard for each change, test
it thoroughly and only then run it on your production environment.

Here is a little script I made for generic execution of remote commands:

1 option explicit
2 on error resume next
4 dim sOriginalCompPrefix 'The prefix of the computers' names
5 dim sCompPrefix 'The calulated computers' names prefix
6 dim iStart 'Start from number
7 dim iEnd 'End at
8 dim i 'Used as enumerator
9 dim objWMIService 'Used to contain the WMI object
10 dim oError ' Used to contain the WMI return object
11 dim sCommandLine 'Used to hold the process name that should be invoked
12 dim iProcessID 'Used to contain the PID of the executable that was invoked on the
13 remote computer
14 dim sComputersToSkip
16 iStart = 1
17 iEnd = 8
18 sOriginalCompPrefix = "DevTS"
19 sComputersToSkip = "TSSRV011;TSSRV012;TSSRV087;TSSRV130"
21 if wscript.arguments.count = 0 then
23 wscript.echo "Please specify the name of the process you wish to run as a
24 command line argument"
25 wscript.quit
26 end if
28 for i = 0 to wscript.arguments.count -1
29 sCommandLine = scommandLine & wscript.arguments(i) & " "
30 next
31 scommandline = left(scommandline,len(scommandline)-1)
33 wscript.echo "Executing '" & sCommandLine & "' on remote computers..." & vbcrlf
35 for i = iStart to iEnd
37 if i<10 then
38 sCompPrefix = sOriginalCompPrefix & "00" 'Adds two leading zeros to the prefix
39 elseif i<100 then
40 sCompPrefix = sOriginalCompPrefix & "0" 'Adds a leading zero to the prefix
41 else
42 sCompPrefix = sOriginalCompPrefix
43 end if
45 if instr(1,sComputersToSkip,sCompPrefix & i,1) =0 then
47 wscript.echo "Running '" & sCommandLine & "' on " & sCompPrefix & i & "..."
49 err.clear
50 Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &
51 sCompPrefix & i &
52 "\root\cimv2:Win32_Process")
54 if err.number <> 0 then
55 Wscript.Echo "'" & sCommandLine & " failed to start on " & sCompPrefix & i &
56 " with the following error:" & vbcrlf & err.description & vbcrlf
57 else
59 oError = objWMIService.Create(sCommandLine, null, null, iProcessID)
60 If oError = 0 Then
61 Wscript.Echo "'" & sCommandLine & " succesfully executed on " &
62 sCompPrefix & i & " with PID " & iProcessID & vbcrlf
63 Else
65 Wscript.Echo "'" & sCommandLine & " failed to start on " & sCompPrefix &
66 i & " with the following error:" & crlf & oError
68 End If
69 end if
70 end if
71 Next

The money time of this script is in the following line:

1 oError = objWMIService.Create(sCommandLine, null, null, iProcessID)

It tells the remote WMI namespace (initialized using the Set objWMIService
statement) to execute the process.

I saved the best (and my favorite) for last:

Method 3: Use ControlUp to run remote commands ^