PowerShell Invoke-WebRequest

After (taking some time) to discover the Invoke-WebRequest cmdlet in PowerShell 3, I hoped it might at least be able to warn me when the bane of IT- Adobe and Java updates- were updated, so I knocked up the following script. Hopefully you’ll find it sufficiently well commented, but essentially it extracts a URI from the main Adobe Reader download site, checks it against an initially non-existent file with an existing URI inside it, then continually updates that file and re-checks to find out when a new version is available. It also keeps a legacy list of versions on file.

One word of warning- I can’t get PowerShell to authenticate through our proxy, so I’ve only used it with straight-through connections to the internet.

Also, I haven’t tested it for real- there are a couple of dummy lines in the script that I’ve used to mimic the updating process but I guess I’ll have to wait for Adobe to actually release a new version. However it all works functionally, including the email bit. I haven’t scheduled it either, which is the main point of writing it.

I do have modified version that run against Air (easy), Java (harder, x86 only) and Shockwave (just weird- it works but will only allow me to install the update, not save it). I haven’t done Flash yet because I was working on a Windows 8 machine, which has its Flash updates sorted through Windows Updates.

I know- working within an IT department- that Adobe do allow you to register for distribution rights which would have probably made the whole thing easier, but it contravenes the license terms to publish those URIs here…

————————————————————————————
#This script will monitor the primary Adobe Reader download site and send an email when new version are available
#This script will indicate that Adobe reader is out-of-date the first time it is run;
#this is because the relevant text files aren’t populated; ignore and re-run, then it should be correct

#Oh; and it doesn’t work through our proxy, despite trying New-PSSessionOption -ProxyAccessType with all combinations of -ProxyAuthentication

#Start by clearing the window
Clear-Host

#Explicitly defines legacy Adobe version variable as string
[string]$savedReaderDownloadSite

#Explicitly defines current Adobe download page variable as string, sets to value of Windows x86 download link
[String]$readerDownloadNowSite=(Invoke-WebRequest -Uri http://get.adobe.com/uk/reader).Links|Select-String -Pattern “Download Now”

#Explicitly defines actual (direct) download link as string, sets to single value
[string]$actualReaderDownloadSite=$readerDownloadNowSite.Split(‘; ‘)|Select-String -Pattern “href=/”

#These are dummy lines. The URI is real, but if you comment out the 2 [string] declarations above and un-comment these,
#you can test the script with dummy values to make sure it works. Start with 9.5, then replace that number with 10.1.4,
#then eventually comment these 2 lines back out and un-comment the 2 [string] declarations above.
#[string]$readerDownloadNowSite=”href=/uk/reader/completion/?installer=Reader_9.5_English_UK_for_Windows}”
#[string]$actualReaderDownloadSite=$readerDownloadNowSite.Split(‘; ‘)|Select-String -Pattern “href=/”

#Next 2 lines trim unwanted characters to leave “clean” http URI
$actualReaderDownloadSite=$actualReaderDownloadSite.TrimStart(“href=”)
$actualReaderDownloadSite=$actualReaderDownloadSite.TrimEnd(“}”)

#This line prefixes a URI to the $actualReaderDownloadSite
$actualReaderDownloadSite=$actualReaderDownloadSite.Insert(0,”http://get.adobe.com”)

#Reads the existing version of Adobe Reader into a variable
$savedReaderDownloadSite=Get-Content C:\Support\Scripts\Web\Adobe\currentAdobeReaderVersion.txt

#Checks to see if the existing version of Adobe Reader is the same as the existing- stored- version
#If it is, write a message to the PowerShell window
If($savedReaderDownloadSite -eq $actualReaderDownloadSite)
{
Clear-Host
Write-Host “Adobe Reader is up-to-date.”
}

#If the two versions don’t match, send an email!
#It also logs the existing version to a “legacy version” text file, so you have a record of all Adobe Reader versions
ElseIf($savedReaderDownloadSite -ne $actualReaderDownloadSite)
{
Clear-Host
$savedReaderDownloadSite|Add-Content C:\Support\Scripts\Web\Adobe\legacyAdobeReaderVersion.txt

#Define hub transport server, sender and recipient variables

$smtp_server = “mailserver.emaildomain.com”
$sender = “FirstName Surname <firstname.surname@emaildomain.com>”
$recipient = “FirstName Surname <firstname.surname@emaildomain.com>”

#Define date and time variables

$dateNow = get-date -displayhint date

#Define email subject and body

$msg_subject = “Adobe Reader is out-of-date”
$msg_body_text = “Adobe reader is out-of-date on the $dateNow `n You can download it from $actualReaderDownloadSite”

#Send it

Send-MailMessage -to $recipient -from $sender -subject $msg_subject -body $msg_body_text -smtpserver $smtp_server

}

#Clears the contents of the existing version file.
#This will always fail first time, as the file does not yet exist
Clear-Content C:\Support\Scripts\Web\Adobe\currentAdobeReaderVersion.txt

#Writes the new version of Adobe Reader to the above file (or creates it first time around).
$actualReaderDownloadSite|Add-Content C:\Support\Scripts\Web\Adobe\currentAdobeReaderVersion.txt

Advertisements

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