Thursday, September 16, 2010

Windows Update log scripting

** CAUTION NERD ALERT **

I have been working on a problem at work to add a list of updates required to be installed on our servers to my daily Powershell check script I have which runs each day. All the examples I had found online featured the script running a query against the WSUS server. I found this to a bit cumbersome for what I needed and it generally only gave me part of the picture required.

So instead I discovered that the windowsupdate.log file is in fact a tab separated file and with Powershell v2 you can easily import this. Once imported I rearranged to sort the newest lines first and then parse it to find the lines showing which updates are waiting.

I then strip those lines to get only the GUID for the update and query this against a WSUS server. This method seems to run fairly swiftly and quite well across WAN links.

If you want to use it just change the editable section at the top. Of course the script could be incorporated into a bigger script checking more than one computer. This is what it does in my daily server check.

I have also uploaded it to the TechNet Script Center Repository.

###
### Script to check WSUS updates to be installed on a given computer.
###
### Author: NomadTales
### Email: nomadtales [at] gmail [dot] com
### Twitter: @NomadTales
### Date: 16 Sept 2010
###
### License: Creative Commons Attribution-NonCommercial 3.0 Unported License
### LicenseURL: http://creativecommons.org/licenses/by-nc/3.0/
###
### The script parses the WindowsUpdate.log file to get a list of update GUIDs,
### then checks those GUIDs against the WSUS server. This method seems more
### acurate and simpler than polling the WSUS server directly.
###
### Please note there are probably better ways at scripting the string handling section.
### Let me know if so.
###


### Start editable section ###

$computer = "computer"       # The computer to check updates on
$systemdrv = "C"             # The System Drive
$windowsdir = "windows"      # The System Folder
$WSUSserver = "wsusserver"   # Your WSUS server
$WSUSserverportnum = "80"    # WSUS server port number
$wsusSSL = $false            # WSUS using SSL, if yes use $true

### End editable section ###


### START SCRIPT ###

## Reset variables - handy if you run the script multiple times from the editor

$updatesrequired = $null
$updates = $null
$updatelist = $null
$updatecount = 0

## Connect to the WSUS server

[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | out-null
$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($WSUSserver,$wsusSSL,$WSUSserverportnum)

## Generate the path of the log file

$winupdatelog = "\\" + $computer + "\" + $systemdrv + "$\" + $windowsdir + "\windowsupdate.log"

## Import the log file and change the sorting to make newest first

$updates = import-csv $winupdatelog -delimiter `t -header "Date", "Time", "Code1", "Code2", "Action", "Description" `
    | sort @{Expression="Date";Descending=$true}, @{Expression="Time";Descending=$true}

## Parse the log file for the update GUID and query the WSUS server for the human friendly title

foreach ($update in $updates)
{
    if ($update.description -match "Added update {")
    {
        $updatecount++
        $updateidtemp = $update.description -replace ".*{"
        $updateid = $updateidtemp -replace "}.*"
        
        $updatesrequired = $wsus.GetUpdate([guid]$updateid)
        $updatelist += "{0}`n" -f $updatesrequired.title

    }
    
    if ($update.description -match "## START ## AU: Search for updates") {break}
   
}

## Add a total number of updates to the bottom

$updatelist += "`n"
$updatelist += "Total updates to install - {0}" -f $updatecount

## Display the updates required

$updatelist

### END SCRIPT ###

Saturday, July 31, 2010

TV-B-Gone inside an iPod

A couple of weeks ago I got myself a TV-B-Gone kit. This is after months of wanting one. Once received I pulled out the soldering iron that had laid unused for years and constructed the simple circuitry. Completed I just didn't like it being so .. exposed.

It needed a case. And not some ordinary case, it needed some sort of disguising case. This after all is a covert piece of equipment. So I pulled out my old iPod Photo, which after a great life had also sat around collecting dust.

Just a normal iPod Photo ..

Once apart and gutted I set about trying to see if the kit would fit. It was satisfying to see that the cable bay at the bottom was perfectly suited as a slot for the IR LEDs.

Something a miss here ..

After experimenting with the iPod Photo's battery connected to the kit I instead tested an older original iPod battery. This worked great.

The only thing to do now was to try and rig up some sort of button. I originally thought about using the main command button on the front of the unit but, after checking the circuitry, found it would be too hard to connect up to so instead changed my focus on using the hold button on the top.

Gutted ..

Wired up with the help of a mate who had a better soldering iron and soldering skills, we had our disguised TV-B-Gone working. All that is needed now is to wear your headphones and wander into any electronics store or mall filled with annoying and power hungry TVs.

Wired up to the hold switch ..

A demo of it in action ..