PowerShell: QuickAddress Pro data expiry checker

QAS Pro (formerly QuickAddress Pro) is a database of addresses that allows you to do postcode lookups etc. Its database is updated every few months, and the databases have expiry dates built in – if you don’t keep the database reasonably up to date then it stops working.

It comes with a .jsp file that allows you to query various things about the product via the web server that QAS is running on. Go to

http://<your-qas-server>:<qasport>/proweb/test.jsp

to view the page (port is 8080 on my server, not sure if that’s the default, was ages ago when this was set up). One of the things listed on that page is the data expiry, shown as days remaining.

Below is a powershell script that pulls the test page, gets the days remaining via a regular expression (regex) and displays just that value to screen. I’ve also used a switch statement to break down the result into a traffic light representation, you could do other stuff in this too like send emails, update a separate monitoring system etc.

# variables for the script 
$YellowLevel = 14 
$RedLevel = 7 
$SleepHours = 1
# change window title 
$Host.UI.RawUI.WindowTitle = "QAS DB Expiry Checker"
# loop indefinitely 
while($true){
     #get web page
     $Page = (Invoke-WebRequest "http://qaswebserver.rcmtech.co.uk:8080/proweb/test.jsp").Content
     # look for text on web page using regular expression
     if($Page -match "[0-9]+ days"){
         # get matching text string from full web page text
         $FullText = (Select-String -InputObject $Page -Pattern "[0-9]+ days").Matches.Value
         Write-Host (get-date),"QAS remaining: $FullText - " -NoNewline
         # get just the number of days
         $DaysString = (Select-String -InputObject $FullText -Pattern "[0-9]+").Matches.Value
         # convert number of days to integer to allow numeric matching operations, e.g. "greater than"
         $DaysInteger = [convert]::ToInt16($DaysString)
         # interrogate the number of days and set status based on what its value is
         switch($DaysInteger){
             {$_ -gt $YellowLevel} {
                 Write-Host "Green" -ForegroundColor Green
             }
             {($_ -le $YellowLevel) -and ($_ -gt $RedLevel)} {
                 Write-Host "Yellow" -ForegroundColor Yellow
             }
             {$_ -le $RedLevel} {
                 Write-Host "Red" -ForegroundColor Red
             }
         }
     } else {
         # page did not contain expected text
         Write-Host "Error with page" -ForegroundColor Yellow
     }
     # wait for specified time
     Start-Sleep -Seconds ($SleepHours * 60 * 60)
 }

The script shows how to change the title of the PowerShell window.
It demonstrates how to use Invoke-WebRequest to get the contents of a web page into a variable.
It also demonstrates how to extract text from a string using a regular expression.
You can also see how to use integers with “greater than” and “less than or equal to” with the switch statement.

This entry was posted in Applications, PowerShell and tagged , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s