Apr 13

The proper way to run a remote process

This is the story of my journey to find a way to run a process (or a program on a remote pc)
This wasn't an easy thing at all...

Overall, I thought, this should be an easy thing to do.
I found this C# code on a Microsoft forums

object[] theProcessToRun = { "notepad.exe" };
ConnectionOptions theConnection = new ConnectionOptions();
theConnection.Username = "username";
theConnection.Password = "password";
ManagementScope theScope = new ManagementScope("<\\\\" + IP + "\\root\\cimv2", theConnection);
ManagementClass theClass = new ManagementClass(theScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
theClass.InvokeMethod("Create", theProcessToRun);

I tried this code, not after forgetting to disable the firewall on the remote computer - a big downside but I guess if I had gone with it I'd hunt a way to stable port to unblock in the firewall.


Then I found the big downside (which can be an upside to some of you):
The remote process this way will never have a GUI window opened (In this example, a process of notepad will be opened in the background).
This can be a big advantage to system admins which want to run scripts.


Ok, back to the quest.

A little more digging came up with a nice set of tools called PsTools
This is a very handy set of tools, that can be used to automate any local or remote tasks.
And the upside to this thing: You can make the GUI window appear !

Sidenote - of course you need the username and password of an administrator to do all those things 

Ok, back to the PsTools...
It has a utility inside called PsExec (guess what it does... )


The command line for this is:
PsExec \\<remote-pc> -u <username> -p <password> -d -i <process name>
-d = don't wait for the process to end
-i = show the GUI window


And that's where the Windows OS gave me hell:


It appears that PsTools use an administrative hidden share named ADMIN$

You can see if you have one by right-clicking on "My Computer", then selecting "Manage", then "Shared folders", and "Shares"

This all refers to Windows XP Proffesional

Unfortunately I deleted this share a while ago (Honestly? I don't remember why)
So my quest had turned to fix this issue...


To make a long story short:
With XP Pro, you can manually create this share with this command (in the remote machine)
net share admin$ 

And make it permanent by adding these registry keys in the remote machine (with the value of 1):
AutoShareWks (REG_DWORD)
AutoShareServer (REG_DWORD)

Also, if you still have connection problems you can clear out your local active connections with
net use * /delete 

Anyways, I hope this post will help you get by all the first difficulties I ran into...

All in all, this is a great thing to have as a network administrator