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 http://live.sysinternals.com.

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!

Tuesday, February 2, 2010

Stupid Ping Tricks

Is there a way to tell if a server is up with a DOS bat file? Yes! I've used this to wait for a network resource to become available before continuing a script.

:PING
ping -n 1 -w 50 192.168.11.1 > nul
if errorlevel 1 goto PING


Is there a way to tell if a server is up with a .vbs file? Yes! I'm using a modified version of this to return a 1 or a 0 for MRTG trending.


On Error Resume Next
strTarget = "10.123.30.12" 'IP address or hostname
Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("ping -n 1 -w 50 " & strTarget)
strPingResults = LCase(objExec.StdOut.ReadAll)

'WScript.Echo strPingResults

if err.number > 0 then
WScript.Echo "ERROR"
End If

If InStr(strPingResults, "reply from " & strTarget) Then

WScript.Echo strTarget & " responded to ping."

Else

WScript.Echo strTarget & " did not respond to ping."

End If

Saturday, January 30, 2010

Windows 7 Profiles

Have you ever tried to move a profile in Windows 7? What a cranky process ...

My friend John built a brand new PC based on the Intel Core i7 920 chip. That included a 64 GB blazing fast SSD for the OS and a separate 1 TB disk for data.

To keep things as simple as possible we wanted to move his, and in theory all, user profiles to the D drive such that he would have to remember to store things in a different place. I remember the first versions of Slackware I downloaded on floppies had Symbolic Links. Thank you Windows 7 ... it's about time.

For those unfamiliar; a symbolic link allows you to point a file or directory to another place. In this case our hope was to make a symbolic link that would re-map c:\users to d:\users.

Long story short you can't do this in 1 step, but I do have it figured out. During the build of his PC he made an account for "John." Once he was logged in he created a dummy account for "Joe."

Logoff and logon as Joe. Is this step required? I have no idea, but it seemed prudent.

Reboot the PC in "Safe mode with Command Prompt." When presented with a login screen login as Joe. Now you need to copy your files ...

mkdir d:\users\john

xcopy c:\users\john d:\users\john /s/e/k/n/o/c

You should see a lot of files/folders being copied. When the copy completes do the following:

rmdir /s c:\users\john

mklink /d c:\users\john d:\users\john

Now you can reboot and login as John, but your user profile will be on D.

Extending this concept ... I believe it completely possible to repeat the steps above, but this time login as John since that profile is already moved. Copy all the other profiles, delete the c:\users folder on C and create a symbolic link for c:\users to point to d:\users.

Wednesday, January 20, 2010

I forgot to send my nephew a Happy Birthday SMS this morning!

This isn't the first time that it's happened. I could look at my calendar in the morning. Of course that would be helpful. I have found another option though ... have an Android based phone? Go to the Android Market and grab SMS Wishes. It allows you to schedule SMS messages. I promptly entered an SMS message to my nephew that will be delivered right after he gets out of school!

I believe this will also be useful for friends/family that refuse to use a calendar and often need gentle reminders ;)