PowerShell: A Function to download and run an Installer
coding powershell
I’ve added a Setup menu for downloading and installing the Azure Cli and the Azure Sphere SDK as part of the djaus2/az-iothub-ps tool (available on GitHub). I’ve encapsulated that functionality into a function for reusablity.
Links
- djaus2/az-iothub-ps on GitHub.
- function install-download in that repository. It’s the first function in that file. There is my Setup menu function below it.
About az-iothub-ps
This tool is a menu driven PowerShell tool that can create an Azure Group, IoT Hub, Device and Device Provisioning Service (both linked to the hub). There is also an option to automatically create all those items from the command line. It automatically makes all of the Azure IoT Hub SDK queries.
Usage examples
$url="https://aka.ms/installazurecliwindows"
$output = "$global:ScriptDirectory\temp\azure-cli.msi"
install-download $url $output
Installation of Az Cli using the function
$url="https://aka.ms/AzureSphereSDKDownload"
$output="$global:ScriptDirectory\install-azsphere.exe"
install-download $url $output
Installation of Azure Sphere SDK using the function
Notes
The function does not remove the downloaded file. That action was originally in the function but was removed as I found it generated a thread issue. The test at the end if (Test-Path $output)
could support the removal.
Microsoft docs presents a PowerShell command for installing Az Cli:
Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; rm .\AzureCLI.msi
I found it would take a lot of time and not do anything when I had a previous installation. I found a discussion with respect to this suggesting it’s slow because it uses a lot of buffering. A suggestion was to use BITS, Background Intelligent Transfer Service to do downloads. I found this a better way to do the transfer:
Import-Module BitsTransfer
Start-BitsTransfer -Source $url -Destination $output
There is an option to do this synchronously, as above, or asynchronously.
Links on BITS
- 3 ways to download files with PowerShell … The discussion mentioned above.
- BITS discussion on MS Docs
Footnote: The function checks for the existence of a global PS variable
$global:usePreviousDownloads
. If that exists as a non-empty string then if the download file exists already, it just does the install. If not then it deletes it first.
Topic | Subtopic | |
< Prev: | This blog site construction | Site Calendar Page |
This Category Links | ||
Category: | Coding Index: | Coding |
Next: > | PowerShell | Functions for a quick user response |
< Prev: | PowerShell Code Signing of scripts |