Retrieving the latest PowerShell version

A good example to start with is to retrieve, download, and install the latest PowerShell Core version. The code shows how to work with directories and JSON. It is written as a simple function, and retrieves the latest PowerShell versions from GitHub and validates the currently used PowerShell version on its GitCommitId. Make sure you are executing the code with PowerShell Core 6. In the bottom-right corner of Visual Studio Code, you will be able to check which PowerShell version is currently in use, as follows:

As you can see from this example, PowerShell version 6 is currently being used. After you have clicked on it, the settings will show up and you will be able to choose between the different PowerShell versions:

#Retrieves the latest PowerShell Core version - mainly built for Windows OS.
Function Get-LatestPowerShellVersion {

#Using TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

#Retrieving the latest PowerShell versions as JSON
$JSON = Invoke-WebRequest "https://api.github.com/repos/powershell/powershell/releases/latest"| ConvertFrom-Json

#Validating if PowerShell Core 6 is being used
If ($PSVersionTable.GitCommitId) {
If ($JSON.tag_name -ne $PSVersionTable.GitCommitId) {
Write-Host "New version of PowerShell available: $($JSON.Name)"

$osarch = "x64"

#Validating if architecture of OS is 64 bits
if (-not [System.Environment]::Is64BitOperatingSystem)
{
$osarch = "x86"
}
Write-Host "The architecture of Windows is $($osarch)."

#Download string for MSI
$urlToMSI = ($JSON.assets.browser_download_url).Where{$_ -like "*-win-$osarch.msi"} | Select-Object -First 1

#Download to desktop - creating download path
$downloadPath = Join-Path -Path $([System.Environment]::GetFolderPath("DesktopDirectory")) -ChildPath ([System.IO.Path]::GetFileName($urlToMSI))

if ($downloadPath) {
Write-Host "File already download."
}
else
{
Write-Host "Downloading file $urlToMSI to Desktop."

#Downloading file
$client = New-Object System.Net.WebClient
$client.DownloadFile($urlToMSI, $downloadPath)
Write-Host "Download completed."
}
return $downloadPath
}
Else {
"PowerShell is currently up to date!"
}
}
Else {
Write-Host "No GitCommitId could be found, because you are using PowerShell Version $($PSVersionTable.PSVersion)"
}
}

#validating, if a new version exist and download it
#the downloaded file path will be returned
$downloadPath = Get-LatestPowerShellVersion

#Execute MSI
Start-Process $downloadPath

The Get-LatestPowerShellVersion function will search for a newer version and download it automatically for the correct architecture of your operating system. The path for the download is stored in $downloadPath. It will automatically download the MSI file, which can easily be executed with Start-Process $downloadPath. You could extend this example with a function header, as discussed in the best practice section in Chapter 4, and make it more dynamic. Also try to understand the cmdlets used in this example. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.111.183