I recently had the need to update the Microsoft ODBC Driver for SQL Server on a collection of Windows servers. I found the download URL for the particular version I needed on Microsoft’s website and then threw together a quick script that I could run against the list of servers to download and update the ODBC driver on all the servers.
Later in the day I had the thought, “that’s nice to patch to a specific version, but what about just going to the latest version?” If I could devise a method to get the link for the latest version, the script wouldn’t need to be updated if it needed to be used again. Who doesn’t like making less work for themselves down the road?
Let’s take a look at one of several ways you can get download links from webpages using PowerShell, and how I decided to use that in the improved script.
What we really want to focus on here is the Links property of the object (I’ve truncated it for the sake of brevity in the post, since it’s quite lengthy):
This is better, since it is only returning the links for the page content, however we need to take it one step further. Looking at the webpage, I know that the displayed text for the download link is “Download Microsoft ODBC Driver 17 for SQL Server (x64)”. What I ended up doing was moving deeper into the Invoke-WebRequest’s response, specifically into the OuterHTML. From here, we can use Select-String to find the specific URL that we want:
1
$webpage.links.outerhtml|Select-String-SimpleMatch"Download Microsoft ODBC Driver 17 for SQL Server (x64)"|Out-String
This left me with a this line of the HTML, as a string:
1
<a href="https://go.microsoft.com/fwlink/?linkid=2249004" data-linktype="external">Download Microsoft ODBC Driver 17 for SQL Server (x64)</a>
We can then use basic string manipulation to get our our target URL. Given that the URL is wrapped in double quotation marks, we can split the string at the double quotation marks and select one of the splits:
1
$url=($webpage.links.outerhtml|Select-String-SimpleMatch"Download Microsoft ODBC Driver 17 for SQL Server (x64)"|Out-String).split('"')[1]
With my download URL obtained, I was able to write an improved version of the script which will install the latest update for the Microsoft ODBC Driver for SQL Server, rather than a specified version.
Is there a better way to accomplish this? Quite possibly. As the saying goes, there’s more than one way to cook an egg. This was just thrown together out of necessity.
If you’re interested, you can check out how I ended up implementing this in the script
in my public GitHub repository. I hope that you’ve found something in this post helpful in some way!