5.2. Running a Task on a Remote Server

Problem

You want to run a task on a remote server. By this, I don't mean running a task against a remote server, but the command or script actually runs on the remote server. This is useful if the tool you want to use can only be run locally on a system, or if it generates a lot of network traffic and could work more efficiently if run locally.

Solution

Using a graphical user interface

To run a command or utility on a remote computer via a graphical interface, you'll need to use a remote desktop application like Remote Desktop or VNC.

Using a command-line interface

The following command executes the diruse utility on the host fs01 to find directories that contain more than 100 MB of data:

> psexec \\fs01 c:\tools\diruse.exe /s /m /d /q:100 c:\

This assumes that c:\tools\diruse.exe exists on fs01. You can also have psexec copy the command you want to run from the local system to the target system by specifying the -c option. The following command executes the same one as before, except the diruse utility is copied to the target system:

> psexec \\fs01 -c diruse.exe /s /m /d /q:100 c:\

As with other Sysinternals tools, you can specify alternate credentials with psexec using the -u option. Here is an example:

> psexec \\fs01 -u CORP\rallen -c c:\diruse.exe /s /m /d /q:100 c:\

Using VBScript

' This code shows how to run a task on a remote server.
' ------ SCRIPT CONFIGURATION ------
strComputer = "<ServerName>" strCommand = "cscript.exe c:\scripts\dircheck.vbs" ...

Get Windows Server Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.