I’ve been known to patch a SharePoint server here and there. Once in a while, when I’m on a customer server its Internet Explorer security settings will prevent me from being able to download a patch. I’ll go to my favorite patch list, toddklindt.com/sp2016builds or toddklindt.com/sp2013builds, and pick the patch I want. But when it comes down to getting the patch the IE security settings will prevent me from actually downloading said patch. Sadness ensues. I’ve had to do various dances to get the patches and recently I’ve started leveraging PowerShell more and more to do so. After a conversation with Jason Himmelstein I agreed to publish the PowerShell I use. Jason promised me you wouldn’t laugh. So you can’t laugh.
I also made a video of how to use it.
The Usage
I packaged this as a function, Get-TKMSKBDownload, and while it’s in a module, you can easily paste it into any existing module file you might already be using.
To use this function download TKDownloadFile.psm1.txt. Remove the “.txt” at the end to rename it to TKDownloadFile.psm1, Rename-Item .\TKDownloadfile.psm1.txt .\TKDownloadFile.psm1. Then Import it into your PowerShell session with Import-Module TKDownloadFile to let it know it’s there.
I have lovingly provided some Help and Examples to help you use it. In trying to address all of the situations where I’ve done this, I made sure the function worked with either details.aspx and confirmation.aspx URLs for the download link.
To use it, go to my patches page and click the Download link for the patch you want. Then copy that URL out of your browser and paste it after the function, like in the example:
Get-TKMSKBDownload -url https://www.microsoft.com/en-us/download/confirmation.aspx?id=56230
It will download the patch into your current directory. It should look like this when it runs:
After the file is downloaded you can run it and patch your SharePoint server.
I won’t paste all the code in this blog post, (download it here) but I do want to highlight a couple of pieces of it. First, this is the line I use to get the direct link to the patch download, where $url is the link from the patches page:
$downloadurl = ((Invoke-WebRequest -UseBasicParsing -Uri $url).links | Where-Object -Property data-bi-cN -Like -Value "click here to download manually" | select -First 1).href
Before I wrote this PowerShell I had a couple of instances where I needed the direct link to the patch. I had had to use the Developer Tools in Chrome to find the URL in the Microsoft Download web page. I was able to use that information to craft the line above. If you’re curious, you can start with the part inside of the parenthesis (Invoke-WebRequest -UseBasicParsing -Uri $url) and see how the page is loaded as an object by PowerShell. There is some interesting stuff in there.
The other part I needed was the name of the file to save. That’s part of the $downloadurl object, so I used this PowerShell to put off the file name, which is everything after the final / character, plus 1 so we don’t get the / itself:
$file = $downloadurl.Substring($downloadurl.LastIndexOf("/") + 1)
With those two pieces of information I’m able to pull the patch down and save it.
Invoke-WebRequest -UseBasicParsing -Uri $downloadurl -OutFile $file
As always, I had a couple of people look this code. Thanks to Shane Young and Jeff Hicks for all of that. Jeff had some great ideas for a v2 of this, like being able to specify a download directory, or backgrounding the download job. All good ideas, but I didn’t want to delay the publication of this blog post to implement them. If I keep using this module I may add them later. If I do, I’ll update this blog post.
Once again, if you’d like to see all of this magic in action, you can watch my HowTo video on YouTube.
tk
ShortURL: http://toddklindt.com/PSDownloadMSPatch