Created
June 9, 2015 14:29
-
-
Save ploegert/6c886e44e78c5e70ee9b to your computer and use it in GitHub Desktop.
Installs the given hotfix via powershell
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
| <# | |
| .Synopsis | |
| Installs the given hotfix | |
| #> | |
| function Install-Hotfix | |
| { | |
| param( | |
| [Parameter(Mandatory=$true)] | |
| [string] $Url, | |
| [Parameter(Mandatory=$true)] | |
| [string] $HotfixId | |
| ) | |
| $downloadedFile = Download -Url $Url | |
| $logFile = $downloadedFile + ".log" | |
| function InvokeWusa { | |
| Write-Log "Installing $downloadedFile..." | |
| $process = [System.Diagnostics.Process]::Start('wusa.exe', "/quiet /norestart /log:$logFile $downloadedFile") | |
| if (!$process.WaitForExit(30 * 60 * 1000)) { # wait for 30 min | |
| throw "Installing $HotfixId timed out; exiting..." | |
| } | |
| Write-Log "Install completed (wusa.exe exit code: $($process.ExitCode))" | |
| return $process | |
| } | |
| $process = InvokeWusa | |
| # | |
| # 0x8024800c ("The data store section could not be locked within the allotted time") may indicate a conflict with | |
| # Windows Update, so we stop the service and try one more time. | |
| # | |
| $windowsUpdate = 'Windows Update' | |
| if ($process.ExitCode -eq 0x8024800c -and ((Get-Service $windowsUpdate).Status -eq 'Running')) { | |
| Write-Log 'Install had a conflict with Windows Update; will retry.' | |
| Write-Log 'Stopping Windows Update service...' | |
| Stop-Service $windowsUpdate -Force -Verbose | |
| try { | |
| InvokeWusa > $null | |
| } | |
| finally { | |
| Write-Log 'Restarting Windows Update service...' | |
| Start-Service $windowsUpdate -ErrorAction Continue -Verbose | |
| } | |
| } | |
| } | |
| <# | |
| .Synopsis | |
| Tests whether the given hotfix is installed | |
| #> | |
| function Test-Hotfix | |
| { | |
| [CmdletBinding()] | |
| [OutputType([bool])] | |
| param( | |
| [Parameter(Mandatory=$true,Position = 0)] | |
| [string] $HotfixId | |
| ) | |
| [bool](Get-Hotfix -Id $HotfixId -ErrorAction SilentlyContinue) | |
| } | |
| <# | |
| .Synopsis | |
| Returns the download info for WMF, which is a hashtable with these items: | |
| HotfixId - Name of the WMF hotfix | |
| Url - Url of the WMF hotfix | |
| #> | |
| function Get-WmfDownloadInfo | |
| { | |
| param( | |
| [parameter(mandatory=$true)] | |
| [string] $WmfVersion | |
| ) | |
| $osVersion = GetOSVersion | |
| Write-Log 'Retrieving WMF download information...' | |
| Write-Log " WMF Version: $WmfVersion" | |
| Write-Log " OS Version : $($osVersion.Version)" | |
| Write-Log " Server OS : $($osVersion.IsServer)" | |
| Write-Log " 64-bit OS : $($osVersion.IsX64)" | |
| if ($osVersion.IsX64) { | |
| $arch = 'x64' | |
| } else { | |
| $arch = 'x86' | |
| } | |
| $key = 'WMF_{0}-Windows_{1}-{2}' -f $WmfVersion, $osVersion.Version, $arch | |
| $downloadInfo = $script:wmfDownloadInfo[$key] | |
| if (!$downloadInfo) | |
| { | |
| throw New-HandlerTerminatingError $DSC_Status.OsVersionNotSupported | |
| } | |
| $downloadInfo | |
| } | |
| # | |
| # Downloads the given URL to a local file and outputs the path to the file | |
| # | |
| function Download | |
| { | |
| param([Parameter(Mandatory=$true)][string] $Url) | |
| $webClient = $null | |
| try | |
| { | |
| $uri = [URI] $url | |
| $update = $uri.Segments[$uri.Segments.Count - 1] | |
| $localFile = '{0}\{1}' -f $DSC_MainWorkingFolder, $update | |
| if (Test-Path $localFile) { | |
| throw New-HandlerTerminatingError ` | |
| -Code $DSC_Status.WmfInstallError.Code ` | |
| -Message ($DSC_Status.WmfInstallError.Message -f $update, (GetLogDirectory)) | |
| } | |
| Write-Log "Downloading $Url ..." | |
| $webClient = New-Object System.Net.WebClient | |
| $webClient.DownloadFile($Url, $localFile) | |
| $localFile | |
| } | |
| finally | |
| { | |
| if ($webClient) { | |
| $webClient.Dispose() | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment