Recipe5.2.Running a Task on a Remote Server


Recipe 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" ' ------ END CONFIGURATION --------- set objController = WScript.CreateObject("WSHController") set objRemoteScript = objController.CreateScript(strCommand, strComputer) WScript.ConnectObject objRemoteScript, "remote_" objRemoteScript.Execute do While objRemoteScript.Status <> 2      WScript.Sleep 100 loop WScript.DisconnectObject objRemoteScript

Discussion

If you want to use psexec to run a CMD command (e.g., dir, date, set, etc.), you can use either the /c or /k option available with cmd.exe. The following command simply lists the contents of the C: drive on fs01:

> psexec \\fs01 cmd /c dir c:\

Occasionally I have seen where executing a command such as dir will not display any results when run with psexec. If you encounter this, you can create an interactive CMD session on the remote server using the /k option. Here is an example:

> psexec \\fs01 cmd /k dir c:\

This will print the results of dir, but also leave you at a command prompt on the remote server thereby allowing you to run additional command-line utilities without calling psexec again.



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net