As you know, Sitecore 9.1 is already released, and there is a lot of new packages to download. In this article, I will explain to you how to download all packages in an automated way with PowerShell. I will cover the following tasks:
- How to login to Sitecore download page
- How to get all files to download
- How to download all files
Before we start our journey to downloading files in an automated way, we have to log in.
You must be logged in.
To download packages from the Sitecore download page, you have to be a certified Sitecore developer, and you have to log in before you be able to download files from the Sitecore page.
With PowerShell, we can implement logging functionality in the following way:
If $status -eq $true we can go to the next step, otherwise please display kindly message that ‘password’ or ‘username’ is incorrect. Please notice that you must reuse the session variable in the next interactions with Sitecore download site.
How to list files to download?
Get all links in the HTML page is very easy Invoke-WebRequest response contains links property. With this property we can iterate thru all links available on the page, but as you can imagine HTML page contains not only links to files that we want to download. You have to analyze an HTML markup to find a way to get all proper links from HTML page. I give you favor, and I did it for you.
As you can see on the image, each valuable link has attribute ‘downloads-file’ and I use this attribute to filter files. Below you can find a snippet that returns all links to files that we want to download.
In variable $filesToDownload we have all links ready to download. But wait a minute, all of them has ugly name like a hash – /~/media/F53E9734518E47EF892AD40A333B9426.ashx
How to get a filename?
Now you have to be familiar with an HTTP requests methods like getting, POST, etc. As you know each keyword (GET, POST, …) means what action we would like to perform on a given resource. With GET we want to get the whole response body, in case of the file, it means that we want to download a file, but before we download the file, we would like to know the filename.
There is a request method HEAD, which is similar to GET but return only response header, not a body. For us, it means that we are so close to getting a file name. Here is the desired code snippet.
This code will generate the following output, where hash names are translated to more human readable.
The big finale.
The download file is the easy part when we have a source and destination.
- We have a source from the step “How to list files to download?”
- We have a destination from the step “How to get a filename ?”
Now I need to put bricks together. We have to add one line shown in the picture below.
The voice from the crowd.
– Hey @RobsonAutomator, but I don’t want to download everything, I want to choose what I want to download.
– No worries my friend. I will do this for you in the next post – @RobsonAutomator says.
Are you looking for similar, great solutions? Follow @RobsonAutomator