Install or update Microsoft Azure Powershell Module with Powershell

DEPRECATED POST – NEW AND UPDATED POST CAN BE FOUND HERE

After “manually” updating/installing my Azure Powershell Module from GitHub a couple of times to often, just in the last 4 months, I decided it’s time make a script for this! Or rather a function which I can reuse. 🙂

Install by simply executing:

Get-AzurePowershellModule -Install

You can also download the installer by omitting the -Install parameter. By omitting the -Install parameter the function will the path to the downloaded installer.
If you want more output you can use the parameter -ShowVersion to output the installed version (if any) and the latest version on GitHub.
You can also use -Verbose if you want even more output.

You’ll find the code below or can download directly from GetAzurePowershellModule.

function Get-AzurePowershellModule
{
    [cmdletbinding()]
    Param(
        [switch]$ShowVersion,
        [switch]$Install,
        [string]$Path=$null
    )
    BEGIN
    {
        If ($ShowVersion.IsPresent)
        {
            $Modules = Get-Module -ListAvailable
            $InstalledVersion = ($Modules | Where-Object {$_.Name -eq "Azure"}).Version.ToString()
            Write-Output "Installed: $InstalledVersion"
        }

    }
    PROCESS {
        Try
        {
            $WebSite = Invoke-WebRequest -Uri "https://github.com/Azure/azure-sdk-tools/releases/latest" -Method Get -TimeoutSec 30
        }
        Catch
        {
            Throw "Failed to get Web Content for Azure Powershell Module: $($_.Exception.Message)"
        }

        if ($ShowVersion.IsPresent)
        {
            $LatestVersion = ($WebSite.Links | Where-Object {$_.href -eq $WebSite.BaseResponse.ResponseUri.AbsolutePath}).innerText
            Write-Verbose "LatestVersion = $LatestVersion"
            Write-Output "Latest: $LatestVersion"
        }

        $DownloadURL = ($WebSite.Links | Where-Object {$_.InnerHTML -eq "Windows Standalone"}).href
        Write-Verbose "DownloadURL = $DownloadURL"

        If ($Path -ne $null)
        {
            if ((Test-Path $Path) -eq $false)
            {
                $OutFilePathRoot = (Get-Item $Path).Directory
            }
        }
        if ($OutFilePathRoot -eq $null)
        {
            $OutFilePathRoot = $env:TEMP
        }
        $OutFilePath = "$($OutFilePathRoot)\$($DownloadURL.Split("/")[-1])"
        Write-Verbose "OutFilePath = $($OutFilePath)"

        Invoke-WebRequest -Uri $DownloadURL -OutFile $OutFilePath -Method Get -TimeoutSec 60

        $LogPath = $OutFilePath.Split(".")
        $LogPath[-1] = "log"
        $LogPath = $LogPath -join "."
        Write-Verbose "LogPath = $($LogPath)"

        If ($Install.IsPresent)
        {
            Start-Process $OutFilePath -ArgumentList "/quiet /norestart /log $($LogPath)" -Wait -PassThru
        }
        else
        {
            Write-Output "Azure Powershell Module downloaded to: $($OutFilePath)"
        }
    }
    END
    {
        If ($Install.IsPresent)
        {
            Remove-Item -Path $OutFilePath -Force -Verbose
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s