Wednesday, February 3, 2010

Doing Things Remotely

Have you ever needed to move data from one remote server to another?  There's no reason to move the data to my machine as a broker to the other server, but that's exactly what happens when you execute a copy from one network share to another -- or from one UNC patch to another.  This problem is exasperated if the link between your machine and the servers is much slower.

There are a number of tools available from Sys Internals that are fantastic.  I'll focus on "psexec" today.  All of the tools can be downloaded in 1 archive or executed live from

What is psexec?  It's an application that allows me to remotely connect to another machine and execute a command or run a script.  Given the scenario above we can actually tell Server1 to initiate a copy to Server2 without putting our machine in the middle.  Since those 2 servers are connected via gigabit Ethernet it makes no sense to drag the files, in both directions, across a 10 mbit/sec WAN connection.

Why not just RDP to one of the servers and execute the command locally?  Maybe the job is going to take longer than a couple of minutes due to size or quantity of data pieces that must be moved.  If you log out of the RDP session your copy is stopped.  Even if you close the RDP window that doesn't stop another admin from kicking you off.  Executing the remote action from my PC gives me more control of the process.

Let's get started ...

Create a folder to hold the files and add that folder to your system's PATH environment variable.  Do this for the server on which you wish to execute this tool as well.

I'm a huge fan of robocopy.  We'll use examples that utilize it, but you could as easily do the same for xcopy.  If you're going to use robocopy make sure you put the .exe in a folder that is in the PATH environment variable as well.

The syntax can be seen by executing psexec /? at a command prompt.  Here's an example of a copy from Server1 to Server 2.  We'll copy .bak files (for no particular reason):

psexec \\Server1 robocopy c:\somepath \\Server2\c$\somepath *.bak /s /e /w:0 /r:0

You can specify different user permissions if necessary for the psexec command.  You do so by inserting -u {domain\}username between \\Server1 and robocopy.  

Since this isn't a robocopy how-to lesson I won't go in to all the options available.  Suffice it to say that it's a very robust tool and worth your effort to download it and use it.

What else can be done?  Of course you can execute programs you drop on the system.  We've already demonstrated that.  You can also execute anything pre-existing on the system.  For instance, you could stop & start a service (or restart for that matter):

psexec \\Server2 net stop SomeServiceName
psexec \\Server2 net start SomeServiceName

If you've thought of a different use please share!

No comments:

Post a Comment