Skip to content

Instantly share code, notes, and snippets.

@rchaganti
Last active August 29, 2015 14:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rchaganti/047435aa1b1094188396 to your computer and use it in GitHub Desktop.
Save rchaganti/047435aa1b1094188396 to your computer and use it in GitHub Desktop.
Download latest Python MSI from python.org/downloads
Function Test-Url {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[String] $Url,
[Parameter()]
[Switch] $ReturnUri
)
Process {
if ([system.uri]::IsWellFormedUriString($Url,[System.UriKind]::Absolute)) {
$true
} else {
$false
}
}
}
Function Get-PythonMSI {
[CmdletBinding()]
param (
[Parameter(ParameterSetName='Download')]
[Switch]$DownloadLatest,
[Parameter(ParameterSetName='Download',Mandatory=$false)]
[String]$DownloadPath = "$env:USERPROFILE\Downloads"
)
Process {
$Msi = @()
$doc = Invoke-WebRequest -Uri 'https://www.python.org/downloads'
foreach ($href in ($doc.links.href -ne '')) {
if ((Test-Url -Url $href) -and $href.EndsWith('.msi')) {
$uri = [System.uri]$href
$Msi += New-Object -TypeName PSObject -Property @{
"Url" = $href
"Version" = $Uri.Segments[-2].Replace('/','')
}
}
}
if ($DownloadLatest) {
$MsiToInstall = $msi | Sort -Property Version -Descending | Select -First 1
Start-BitsTransfer -Source $MsiToInstall.Url -Destination $DownloadPath
} else {
$Msi
}
}
}
#Usage
#get a list of all MSIs
Get-PythonMSI
#Download the latest MSI
Get-PythonMSI -DownloadLatest
#Download the latest MSI to a specific destination
Get-PythonMSI -DownloadLatest -DownloadPath C:\Downloads
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment