Last active
January 14, 2016 18:52
-
-
Save p0w3rsh3ll/df0fc784452c1a802b1b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#Requires -Version 3.0 | |
Function Get-LatestWMF5 { | |
[CmdletBinding()] | |
Param( | |
[parameter(Mandatory)] | |
[system.string]$TargetFolder | |
) | |
Begin { | |
$HT = @{ | |
ErrorAction = 'Stop' ; | |
Verbose = $false ; | |
} | |
try { | |
$null = Get-Item $TargetFolder @HT | |
} catch { | |
Write-Warning -Message "The target folder specified as parameter does not exist" | |
break | |
} | |
} | |
Process { | |
try { | |
$p = Invoke-WebRequest -URI 'http://aka.ms/wmf5latest' -UseBasicParsing @HT | |
} catch { | |
Write-Warning -Message "Failed to get webpage http://aka.ms/wmf5latest because $($_.Exception.Message)" | |
} | |
if ($p) { | |
try { | |
$d = Invoke-WebRequest -Uri $($p.BaseResponse.ResponseUri.OriginalString -replace 'details\.aspx','confirmation.aspx') -UseBasicParsing @HT | |
} catch { | |
Write-Warning -Message "Failed to get the content from webpage: $($p.BaseResponse.ResponseUri) because $($_.Exception.Message)" | |
} | |
Switch ([environment]::Is64BitOperatingSystem) { | |
$true {$bitness = '-x64' ; break} | |
$false {$bitness = '-x86' ; break} | |
} | |
Switch ([Version]$((Get-CimInstance Win32_OperatingSystem).Version)) { | |
{ $_ -gt [Version]'6.3' -and $_ -lt [Version]'6.4'} { $OS = 'WindowsBlue'; break } | |
{ $_ -gt [Version]'6.2' -and $_ -lt [Version]'6.3'} { $OS = 'Windows8' ; break } | |
{ $_ -gt [Version]'6.1' -and $_ -lt [Version]'6.2'} { $OS = 'Windows6.1' ; break } | |
} | |
if (-not($OS)) { | |
Write-Warning -Message "Theres's something wrong with the version of your Operating System. Aborting" | |
break | |
} | |
if ($d) { | |
$u = ($d.Links | Where { | |
$_.href -match ('{0}.*\{1}.msu'-f $OS,$bitness) | |
}).href | Select -First 1 | |
Write-Verbose -Message "About to download the latest WMF 5 from $u" | |
try { | |
Invoke-WebRequest -Uri $u -UseBasicParsing @HT -OutFile (Join-Path -Path $TargetFolder -ChildPath $(([uri]$u).Segments[-1])) | |
Write-Verbose -Message "Successfully downloaded the latest WMF 5 to $TargetFolder" | |
Get-Item -Path (Join-Path -Path $TargetFolder -ChildPath $(([uri]$u).Segments[-1])) | |
} catch { | |
Write-Warning -Message "Failed to download $u because $($_.Exception.Message)" | |
} | |
} | |
} | |
} | |
End {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment