Recipe 10.7. Running a Task on a Remote System


Problem

You want to run a task on a remote system. By this we don't mean run a task against a remote system, but rather that the command or script will actually run on the remote system. 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 such as Remote Desktop.

Using downloadable software

The following Sysinternals utility executes the diruse command 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 system. ' ------ SCRIPT CONFIGURATION ------ strComputer = "<HostName>"  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, we have seen situations where 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 system 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 system, thereby allowing you to run additional command-line utilities without calling psexec again.



Windows XP Cookbook
Windows XP Cookbook (Cookbooks)
ISBN: 0596007256
EAN: 2147483647
Year: 2006
Pages: 408

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