UpdateAz.ps1
<#PSScriptInfo .VERSION 3.0 .GUID 9eb5ce4f-04cf-4fec-a2b8-f424812bb556 .AUTHOR rustedwizard@outlook.com .COMPANYNAME .COPYRIGHT .TAGS .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES .PRIVATEDATA #> <# .DESCRIPTION Uninstall and install latest version of Azure Powershell and Azure Cli #> param( [Parameter(Mandatory=$false)] [bool]$AllUsers=$true, [Parameter(Mandatory=$false)] [bool]$SkipAzureCli=$false ) Write-Host -ForegroundColor Yellow "*********************************************************************" Write-Host -ForegroundColor Yellow "** Update Az **" Write-Host -ForegroundColor Yellow "** This script will perform following operation: **" Write-Host -ForegroundColor Yellow "** 1. Uninstall all versions of Az PowerShell. **" Write-Host -ForegroundColor Yellow "** 2. Install the latest version of Az PowerShell available. **" Write-Host -ForegroundColor Yellow "** 3. Upgrade/install the latest version of Azure Cli. **" Write-Host -ForegroundColor Yellow "*********************************************************************" Write-Host "" Write-Host "" Write-Host "Checking for elevated permissions..." if (($AllUsers -OR !$SkipAzureCli) -AND (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))) { Write-Warning "Insufficient permissions to run this script. Open the PowerShell console as an administrator and run this script again." Write-Host -ForegroundColor Red "By default, this script install Az PowerShell in scope of AllUsers, and install Azure Cli require Admin permission." Write-Host -ForegroundColor Red 'You may specify prameter -AllUsers $false -SkipAzureCli=$true in order to run this script in regular user.' Return } Write-Host -ForegroundColor Yellow "Remove all versions of Az PowerShell currently installed" $AzVersions = Get-InstalledModule -Name Az -AllVersions -ErrorAction SilentlyContinue if ($null -ne $AzVersions) { Foreach ($v in $AzVersions) { $version = $v.Version Write-Host -ForegroundColor Yellow "Removing Az PowerShell version $version" $temp += (Import-Clixml -Path (Join-Path -Path $v.InstalledLocation -ChildPath PSGetModuleInfo.xml)).Dependencies.Name $total = $temp.Count $count = 0 Foreach ($e in $temp) { $count += 1 if ($null -ne $e) { if (Get-Module -ListAvailable -Name $e) { $prog = $count / $total * 100 Write-Progress -Activity "Removing Depedency Module..." -Status "Dependency module found: $e" -PercentComplete $prog Remove-Module -Name $e -ErrorAction SilentlyContinue Write-Progress -Activity "Removing Depedency Module..." -Status "Attempting to uninstall module: $e" -PercentComplete $prog Uninstall-Module -Name $e -AllVersions } else { Write-Host "Dependency Module $e not installed, skipping..." -ForegroundColor Yellow } } } Write-Progress -Activity "Removing Depedency Module..." -Status "Ready" -Completed Write-Host -ForegroundColor Green "Successfully removed Az PowerShell version $version" } Write-Host "" Write-Host "Remove Az PowerShell..." -ForegroundColor Yellow Remove-Module -Name Az -ErrorAction SilentlyContinue Uninstall-Module -Name Az -AllVersions }else{ Write-Host "No Az PowerShell install has been found, skip uninstallation..." -ForegroundColor Yellow } Write-Host "Install latest Az PowerShell..." -ForegroundColor Yellow if($AllUsers){ Install-Module -Name Az -Scope AllUsers -Repository PSGallery -Force }else{ Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force } Write-Host "Upgrade operation has completed successfully." -ForegroundColor Green if(!$SkipAzureCli){ Write-Host "" Write-Host "Upgrade/Install Azure Cli" -ForegroundColor Yellow Write-Host "" Write-Host "Downloading latest version of Azure Cli..." -ForegroundColor Blue Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi Write-Host "`Installer downloaded, start unattended install..." -ForegroundColor Yellow Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet' Write-Host "`rInstallation process end, clean up..." -ForegroundColor Green Write-Host "Installation of Azure Cli completed successfully." -ForegroundColor Green Remove-Item .\AzureCLI.msi Write-Host "`Clean up completed, Upgrade installed Azure Cli Extensions." -ForegroundColor Yellow $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") Az Upgrade } Write-Host "Script Execution Finished successfully." -ForegroundColor Green |