« Posts under Scripts

Excluding Multiple Paths with ROBOCOPY

ROBOCOPY is an excellent command line utility that Microsoft began bundling with Windows Vista, and has since been included in Windows 7 and Server 2008 (R2 as well). It makes it incredibly simple to mirror a directory tree, and is great for batch file backups. There are a few quirks with it, however.

When using the /MIR switch, ROBOCOPY overwrites the destination path with everything in the source path. To exclude folders, you’re instructed to use the /XD switch. Below is a sample with the correct syntax:

robocopy d:\ n:\Data /MIR /Z /A-:R /R:1 /W:1 /LOG:n:\logs\data.log /XD d:\vms d:\wsus “d:\System Volume Information” d:\tmpbak d:\installs d:\RECYCLER

To break this down, we start with the ROBOCOPY command itself. Following that is the Source Path (D:) and then the Destination Path (N:Data). Next is the /MIR switch that tells ROBOCOPY to mirror the existing directory structure and copy everything. The /Z switch sets the job in Restartable Mode, and /A-:R removes any read-only attributes. /R:1 and /W:1 tell ROBOCOPY to Retry copying the file once if it encounters an error, and Wait one second between retries (I’ve set it this low as the backup runs late at night, as if someone has left a file open it doesn’t matter how many retries are attempted).

Following this, the /LOG switch dumps the full output of the copy job to n:\logs\data.log. This includes information on the number of files copied, directories excluded, etc….

Finally, we get to the /XD switch. Note that this is the last included switch – although you can technically have it where ever you want in the command, it’s cleaner, and less error-prone if you make it the last switch.

To exclude multiple directories, simply list them with spaces in between. If the path contains spaces (in the example above, I’ve excluded System Volume Information), wrap it in double-quotes (“). The most important thing, though, is DO NOT EVER, *EVER* include a trailing “\” in excluded paths!

For some reason, ROBOCOPY parses the trailing “\” in an odd way. If included in the command, the log will list all of the excluded paths, however it will still copy the data. It won’t provide any error messages or other output. For example, if you used the following command:

robocopy d: n:\Data /MIR /Z /A-:R /R:1 /W:1 /LOG:n:logsdata.log /XD d:\vms d:\wsus “d:\System Volume Information\” d:\tmpbak d:\installs d:\RECYCLER

Because there is a trailing \ on “d:\System Volume Information\”, the log will report that the excluded directories are d:\vms d:\wsus d:\System Volume Information d:\tmpbak d:\installs d:\RECYCLER, however it will still attempt to copy all of them.

However, if you simply omit the trailing \, ROBOCOPY will correctly parse the list of paths and will ignore them.

Howto: Google Desktop on Vista x64 via GPO

Edited to remove references to Enterprise edition when not appropriate.

Google Desktop has been in use at our organization long before I started here, however one of the things that’s irked me about it is the lack of official 64-bit support. Specifically, installation is possible on 64-bit editions of Windows, however a flag (specifically, the ‘/force’ flag) is required when running the installer to get it to actually install.

So although I can manually install the app on workstations, it’s a bit of a pain as a lot of the 64-bit machines were missed when they were originally setup. As such, I whipped up a nice little GPO that takes care of everything for me, and has some flexibility for installation detection. There are probably easier ways of doing this, but I was in a rush when I wrote the script, and it works very well.

Instructions

To start, download the Google Desktop Enterprise package. This contains a .ADM file that can be used in both Server 2003 and Server 2008 Group Policy Management consoles. Create a new GPO, and import the .adm in to the User ConfigurationPoliciesAdministrative Templates object (Server 2003 doesn’t have the Policies folder, it just goes right to Administrative Templates). It’s important to do it on the User side rather than the computer side, given the actual installation process.

After the import, it’s important to note that Server 2008 will place the Google object under Classic Administrative Templates (ADM), instead of just in the Administrative Templates folder. Now configure the settings you want for GDE, and you’re almost set.

Here’s the fun part now – the actual installation. Download the Google Desktop Setup file from Google. Because I’m too lazy to make a transform for the .MSI, I dug back in to my DOS knowledge and made a batch file to map a drive to a server with a publically-available share, run the Google Desktop installer from the mapped drive with the /force switch, and then disconnect the drive.

The main advantage to doing this if I want to update the installer with a newer version later, I just have to swap out the file – I don’t have to screw around with transforms again. The script goes as follows:

@ECHO OFF
Color 1F
Title Google Desktop Installer
c:
cd \windows\system32
if exist “gds_installed” GOTO End
ECHO Installing Google Desktop Search
ECHO.
net use x: \\public\installs >nul
start /wait x:\GDE\GoogleDesktopSetup.exe /force /silent
net use x: /delete >nul
echo Installed > gds_installed
exit

:End
ECHO Google Desktop Search already installed! Exiting!
exit

Alter the server name (public in the example) and path to the installer, and then save the file with the .BAT or .CMD extension in the domains scripts folder, or somewhere accessable to users that will be running the script. Remember, users have to have Read and Execute permission to the folder containing the setup executable and the script.

Once saved, add the batch file to the GPO’s logon script (again, under User Configuration). Once done, link the GPO to the Organizational Units (OU’s) that you want GDE installed. Presto! You’re done!

The Script Explained

The very first part makes the window a nice blue with white writing and gives it a title, just in case the user happens to see it (although typically it runs before Explorer loads, and even then it’s usually minimized). Immediately after, it makes sure it’s in the C Drive and changes to the Windows\System32 folder and uses an IF EXIST to see if a file named gde_installed is present. If it is, it uses a GOTO to skip the installation and end the batch job.

If gde_installed isn’t in the system32 folder, it maps \publicinstalls (the globally-available share) to the X Drive, which isn’t used in our organization. It then changes to X:, runs the GDS installer with the /force and /silent flags to install it unattendedly, then it disconnects the X: drive so the users won’t see it, and creates the gde_installed file in the system32 folder.

Details

In the batch file, you’ll note the use of >nul. This simply directs console output into nothingness so the command window, if seen, only shows what I have ECHO‘ed to it. You’ll also notice that the GoogleDesktopSetup.exe program is executed using start /wait. This forces the batch process to wait for the installation to finish before moving on. Without it, it will try to disconnect the X: drive too soon, which will it to prompt the user to forcefully disconnect the drive. As this is a fully automated process, and console output is sent to nul, we don’t want this to happen, hense the /wait flag.

Watchdog script for misbehaving DD-WRT Routers

I have two Linksys WRT600N 1.0 routers running an SVN build of the DD-WRT firmware (v24-sp1 (09/11/08) mega – build 10305) setup in a Client Bridge fashion. I’ve tried a few of the more recent builds, but so far the signal quality and throughput just isn’t quite as good. I have run in to an issue, however, where one of the routers (the client) will drop it’s WIFI connection. The access point stays up on with WIFI and LAN, and while I can reach the client via a LAN connection (and it shows as still having a connection to the AP), nothing gets through on the WIFI side.

As the link I have setup requires relatively high availability, I can’t afford to spend days mucking about with each SVN build released. At the same time, getting angry calls from users who can’t access a file server mid-day also isn’t exactly an option.

Interm solution? A simple CRON-job on the misbehaving router to ping the access point every five minutes (the 5 * * * * part of the below command) and check the output. If it doesn’t get any response after four pings, it simply reboots the router. Problem (temporarily) solved.

5 * * * * ping 192.168.100.251 -c 4 |grep “100% packet loss”; if["$?" != 1]; then reboot; fi

For more information on DD-WRT, check their forums.