December 21st, 2009
While working on my Uber-script to automate the steps I take when bringing a Windows Server 2003 system online, I started to think about the ability to verify that steps were completed successfully. This can be done fairly easily using %errorlevel% by checking the exit status of each step:
xcopy file1 C:\Path\To\Copy\To\
IF "%errorlevel%" == "0" (
echo "xcopy file1 succeeded" >> C:\Temp\build.log
) ELSE (
echo "xcopy file1 failed" >> C:\Temp\build.log
)
If the copy succeeds, xcopy exits with an errorcode of 0 and we write a success message to the log. If it fails, a different errorlevel is produced and we write a failure.
But what if you have thousands of things to check on? Like checking each of the 100 patches in the previous post’s Update? A log file might work great for some people, but knowing my target audience I wanted something a little more obvious, and the Event Log is what I turned to.
Right off the bat I decided we wanted to use our own custom Event Log rather than one of the existing logs, so that we can keep everything separated and easy to audit. Creating a new event log can be done easily enough in VBScript:
Const NO_VALUE = Empty
Set WshReg = WScript.CreateObject("WScript.Shell")
WshReg.RegWrite "HKLM\System\CurrentControlSet\Services\EventLog\NewLogName\", NO_VALUE
Which gets called from the Uber-Batch-Script by:
cscript CreateEvtLog.vbs
So now that we have our new event log, what can we do with it? Let’s build off the previous xcopy example:
xcopy file1 C:\Path\To\Copy\To\
IF "%errorlevel%" == "0" (
EVENTCREATE /ID 1 /L NewLogName /T INFORMATION /SO "Server Build" /D "file1 was copied successfully" > NUL
) ELSE (
EVENTCREATE /ID 1 /L NewLogName /T INFORMATION /SO "Server Build" /D "file1 was not copied successfully" > NUL
)
So now, rather than echoing to a log that the copy succeeded or failed, we create an entry in our new Event Log. And if the copy fails, we get a nice red X. Quickly scanning for failed steps just got a whole lot quicker.
Tags: Batch, Script, Windows
Posted in Uncategorized | 1 Comment »
December 21st, 2009
Lately I have been working on automating a huge amount of work that gets done manually to each Windows Server 2003 machine before it is allowed to touch the network. This includes Windows Updates, Registry Edits, and installing a lot of software. This can take hours depending on who is doing the work. So I started investigating how to automate this, one piece at a time.
Starting off with Windows Updates, I came to the realization that if I were to write a script with all 100 or so updates, I’d end up with a script that was annoying to maintain. Manually removing Superceded updates from the folder, then removing the reference to it from the script, and then adding the new updates in. Surely, there must be a better way!
And along comes the idea of using a simple FOR Loop.
PUSHD X86\UPDATES
FOR /F %%i IN ('"dir /b /o:n *.exe"') DO CALL :install %%i
QCHAIN.EXE
POPD
GOTO :EOF
:install
%1 /Z /Q /U
Pretty straightforward, the script goes into the X86\UPDATES directory, and get a directory listing of all of the files sorted alphabetically. Then, for each of the files in the directory, it calls :install, which says to run the file with the arguments /Z /Q /U.
Once all of the files have been run, it escapes the for loop to run QCHAIN.EXE, return to the original directory, and then exit the script.
Now when I need to remove an update, I simply delete the .exe from the UPDATES directory, and adding new updates is just as simple – just copy it in!
Tags: Batch, Script, Windows
Posted in Uncategorized | No Comments »
September 8th, 2009
Broadband internet connections keep getting faster and faster, but as soon as you flood your pipe by uploading too quickly, everything grinds to a halt. At least that’s the case with Comcast. Maybe you FiOS people are luckier than I am.
My home router (a D-Link DIR 655) is supposed to do some fancy QoS and automatically determine how fast is too fast for my upload speed, but it never seems to work right, flooding my pipe and making browsing the internet feel worse than it did back in my 14400baud AOL days.
In any case, this afternoon I was working on uploading some 2GB of resized photos to my webserver and came across a handy setting in scp that allows you to control the speed at which your upload takes place.
Not a whole lot to explain here, so here’s all you need to know:
scp -l 512 filename user@host:
The -l flag limits the transfer speed, in Kbit/s.
Tags: Linux
Posted in Uncategorized | No Comments »
September 7th, 2009
I like to have photos I take available to family members online, but with modern cameras taking pictures at 10 Megapixels and up, these photos quickly consume lots of the limited space I have on the webserver. Since most of these photos are only going to be viewed on the screen, it makes sense to resize them before publishing them to the Gallery2 photo album I use. This helps cut the amount of time it takes to upload them, as well as save space on the server. The best method for accomplishing this in my setup, is to batch resize them using ImageMagick.
There are a few different methods by which you can use ImageMagick to resize photos. Here are a few quick examples:
mogrify -resize 800 *
Simply enough, this will resize all files to a width of 800 pixels, maintaining aspect ratio
mogrify -resize 800x800! *
This will break the aspect ratio, and force all images to exactly 800×800 pixels.
And finally, a method by which you can recursively resize all of your photos:
find ./ -name "*" -exec echo mogrify -resize 800 {} \;
Tags: Linux
Posted in Uncategorized | No Comments »