Last active
August 29, 2015 14:03
-
-
Save rchaganti/68a2bb55ed015d19385e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Test-Url { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory=$true)] | |
[String] $Url | |
) | |
Process { | |
if ([system.uri]::IsWellFormedUriString($Url,[System.UriKind]::Absolute)) { | |
$true | |
} else { | |
$false | |
} | |
} | |
} | |
Function Get-AzurePowerShellMSI { | |
[CmdletBinding()] | |
param ( | |
[String]$Url = 'https://github.com/Azure/azure-powershell/releases', | |
[Switch]$DownloadLatest, | |
[String]$DownloadPath = "$env:USERPROFILE\Downloads", | |
[Switch]$Passthru | |
) | |
Process { | |
$Msi = @() | |
$doc = Invoke-WebRequest -Uri $Url | |
foreach ($href in ($doc.links.href -ne '')) { | |
if ((Test-Url -Url $href) -and $href.EndsWith('.msi')) { | |
$Msi += New-Object -TypeName PSObject -Property @{ | |
"Url" = $href | |
"Version" = ([Regex]::Match($href,'\bv?[0-9]+\.[0-9]+\.[0-9]+(?:\.[0-9]+)?\b')).Value | |
} | |
} | |
} | |
if ($DownloadLatest) { | |
if (-not (Test-Path $DownloadPath -PathType Container)) { | |
Write-Warning "${DownloadPath} does not exist. Creating it ..." | |
New-Item -Path $DownloadPath -ItemType Directory -ErrorAction Stop | Out-Null | |
} | |
$MsiFullPath = "${DownloadPath}\$(Split-Path -Path $Msi[0].Url -Leaf)" | |
if (-not (Test-Path -Path $MsiFullPath)) { | |
Write-Verbose "Starting MSI download from $($Msi[0].Url)" | |
Start-BitsTransfer -Source $Msi[0].Url -Destination $MsiFullPath -ErrorAction Stop | |
} else { | |
Write-Warning ("{0} already exists at {1}. No action needed." -f $MsiFullPath, $DownloadPath) | |
} | |
if ($Passthru) { | |
return $MsiFullPath | |
} | |
} else { | |
$Msi | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment