Thursday, July 01, 2010

Different ways to execute processes remotely

During my stint at executing a given process remotely, I discovered few ways of doing it. For example, if you want to run a particular command or a program on a remote computer how will you do it? Here are few ways which I tried and were useful:

 

1.        Psexec : This is a tool from SysInternals from Microsoft. It comes as a part of PsTools. Using Psexec, one can run a process on any given remote computer.

Syntax:                    psexec \\remote-machine-name –i –u domainname\username –p password name-and-path-of-the-process-to-be-executed

psexec \\client1 –i –u testdomain\administrator –p P@ssword notepad.exe

Advantages:          Works even if the executable or the process to be executed is on the network location. It need not be present on the destination (remote) machine where you want to execute the process. This makes Psexec one of its kind.

Limitations:           Sometimes, psexec does not work with the –i switch which stands for interactive mode. In these cases try without using –i switch.

 

2.        Windows Service: Developing a Windows service which when started (OnStart() event) executes a process or an executable. Push this service on the remote computer and start it remotely.

Syntax:                    sc \\remote-machine-name create service-name binpath= path-of-the-service-executable start= service-start-type obj= domainname\username password= password

sc \\client1 create TestService1 binpath= C::\TestService\TestService.exe start= auto obj= testdomain\administrator password= P@ssword

Advantages:          Can be achieved with pure coding without the need of any third-party tool.

Limitations:           Requires .Net framework to be installed on the destination computer.

 

3.       WMI: Using WMI (Windows Management Instrumentation) class Win32_Process to execute a process on a remote computer.

Syntax:                    string command = "notepad.exe";

                object[] theProcessToRun = { cmd };

                ConnectionOptions theConnection = new ConnectionOptions();

                theConnection.Username = "administrator";

                theConnection.Password = "P@ssword";

                string machineName = "client1";

                ManagementScope theScope = new ManagementScope("\\\\" + machineName + "\\root\\cimv2", theConnection);

                ManagementClass theClass = new ManagementClass(theScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());

theClass.InvokeMethod("Create", theProcessToRun);

                Advantages:          WMI provides one of the best functionalities to monitor and administer remote computers.

                Limitations:           WMI exceptions should be provided in firewall settings of the remote computers.

                                                Win32_Process cannot execute a process which do not reside physically on that computer. For that matter even mapping network drive does not work with Win32_Process.

 

4.       Scheduled Tasks: Creating scheduled tasks on the remote computer and making those tasks to execute a required process or a program.

Syntax:                    For pre-Vista computers

schtasks /Create /S remote-machine-name /RU domainname\username /RP password /U domainname\username /P password /SC ONCE /TN scheduled-task-name /TR file-to-be-executed /ST start-time /V1

schtasks /Create /S client1 /RU testdomain\administrator /RP P@ssword /U testdomain\administrator /P P@ssword /SC ONCE /TN Task001 /TR notepad.exe /ST 15:36 /V1

 

For post-Vista computers

schtasks /Create /S remote-machine-name /U domainname\username /P password /SC ONCE /TN scheduled-task-name /TR file-to-be-executed /ST 16:55

schtasks /Create /S client1 /U testdomain\administrator /P P@ssword /SC ONCE /TN Task001 /TR notepad.exe /ST 16:52

Advantages:          Does not require any pre-requisites such as firewall exceptions, framework or any third-party tool installed.

You can schedule the task according to your timings.

                Limitations:           Nothing as I know of.

No comments:

Post a Comment