I wanted to configure my Vista Home Premium machine to run a script at shutdown, to synchronise "My Documents" to my file server. On my old XP workstation this was done by editing the local group policy and specifying a shutdown script, and then configuring PathSync (a nice free app) to sync the files to a remote location. The challenge this time is there is no local group policy editor on Vista Home premium.

Options
1. Install some software that modifies the shutdown routine
I didn't really want to do this. I've only had this machine a few weeks and want to keep it free of software that could mess things up. I also don't want to pay for a solution, if possible.

2. Windows registry editing
Every local group policy has a corresponding registry entry, so I wondered if in the absence of the group policy editor, I would just be able to create the relevant registry keys and values to run a script at shutdown. Microsoft kindly provide a list of group policies and where they are configured in the registry, so I downloaded it to have a look. Sadly I couldn't find what I needed in here.

I also wondered if I would just be able to export the existing key from my XP machine into Vista, I tried this but that didn't work either - looks like this was because it references the name of a local group policy, which of course didn't exist on my new machine.

3. Windows task scheduler
The Vista task scheduler is actually very good. You can schedule tasks to run when specific events are written to the event log, such as Event 4634 in the security log (Log Off). I tested and found this would actually work if I used the log off feature, but when shutting down, the OS does not wait for scheduled tasks to finish so it only gets to run for a few seconds.

4. Use a VBScript to sync and shutdown
This is how I've implemented this solution - for now. I've created a VB Script which brings up an "Are you sure you want to shutdown" dialog, if you click yes, it syncs to my remote machine and then shuts down. Then I created a shortcut to the script and added it to the quick launch buttons, next to the start menu button. As a final touch I changed the icon to the same power icon that Vista uses for shutdown.

Shutdown icon
Shutdown button

The snag is of course you have to remember to click on this button to shutdown if you want your documents to sync, if you forget and use the usual shutdown option on the start menu, you get no backup for that session. This also means that it doesn't sync when you reboot, and you also don't get the option to install Windows updates before the computer shuts down.

Anyway here is the script:

Set objShell = CreateObject("Wscript.Shell")
Set wshShell = WScript.CreateObject ("WSCript.shell")
strShutdown = "shutdown -s -t 0 -f"

Continue = MsgBox ("Are you sure you want to shutdown?",vbYesNo + VBCritical, "Shutdown computer")

If Continue = vbNo then

' Do nothing

Else

' This box will disappear after 5 seconds.
intReturn = objShell.Popup("The computer will now sync and shutdown.", 5, "Shutdown computer", VBInformation)

' Run synctoy - just change "MyDocs" to the name of your folder pair.
wshshell.run """c:Program FilesSyncToy 2.0 BetaSyncToyCmd.exe"" -RMyDocs", 6, True

' Shutdown the computer
objShell.Run strShutdown

End If

How do I use this code?
To use the code right-click on your desktop and select New --> Text Document. Copy and paste the code above into the new document. Click on File --> 'Save As...'. Change the filename to shutdown.vbs and the type to 'All files (*.*)'. Once saved you can drop this file on your system anywhere to keep your desktop tidy.

shutdown_snip
Save As ... All Files

Next you can create a shortcut to the file in your quick launch area or Start Menu and give the shortcut a nice name like 'Sync and Shutdown', and a nicer icon that the generic VBScript icon.

One slight annoyance is the SyncToy command prompt window opens minimised, but I can live with that for the moment. I tried a number of sync tools before settling with MS SyncToy. I did try using rsync which I thought may be a bit faster due to it's compatibility with Linux, but the scanning of the folders took forever.

Technorati Tags: , ,