Tests/TestHelpers/WMF5Dot1Installation.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
Set-StrictMode -Version 'latest'
$errorActionPreference = 'Stop'

<#
    .SYNOPSIS
        Retrieves the URL for downloading the WMF 5.1 installation file for Windows 8.1 and Windows Server 2012 R2(amd64).
#>

function Get-Wmf5Dot1InstallFileUrl
{
    [OutputType([String])]
    [CmdletBinding()]
    param ()
    
    return 'https://download.microsoft.com/download/6/F/5/6F5FF66C-6775-42B0-86C4-47D41F2DA187/Win8.1AndW2K12R2-KB3191564-x64.msu'
}

<#
    .SYNOPSIS
        Downloads the WMF 5.1 installation file to the given location.
 
    .PARAMETER DownloadLocation
        The file path to download the WMF 5.1 install file to.
#>

function Download-Wmf5Dot1InstallFile
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [String]
        $DownloadLocation
    )

    $wmf5Dot1InstallFileUrl = Get-Wmf5Dot1InstallFileUrl
    $null = Invoke-WebRequest -Uri $wmf5Dot1InstallFileUrl -OutFile $DownloadLocation
}

<#
    .SYNOPSIS
        Invokes WUSA to install the given file.
        Outputs the exit code from WUSA.
     
    .PARAMETER InstallFile
        The file to install using WUSA.
 
    .NOTES
        WUSA: Windows Update Service Application
#>

function Invoke-Wusa
{
    [OutputType([Int])]
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [String]
        $InstallFile
    )

    Write-Verbose -Message "Installing $InstallFile..."

    $wusaProcess = [System.Diagnostics.Process]::Start($InstallFile, "/quiet /norestart ")

    Write-Verbose -Message 'Waiting for WUSA install to complete...'

    # Wait for 60 minutes for the process to exit
    if (-not $wusaProcess.WaitForExit(60 * 60 * 1000))
    { 
        throw "Installing $InstallFile timed out after 60 minutes. Exiting..."
    }
   
    return $wusaProcess.ExitCode
}

<#
    .SYNOPSIS
        Installs WMF 5.1.
#>

function Install-Wmf5Dot1
{
    [CmdletBinding()]
    param ()

    $downloadLocation = "$env:SystemDrive\WMF5Dot1.msu"

    $null = Download-Wmf5Dot1InstallFile -DownloadLocation $downloadLocation
    
    Write-Verbose -Message 'Restarting the Windows Update service (wuauserv)...'

    $null = Set-Service -Name 'wuauserv' -StartupType 'Manual'
    $null = Start-Service -Name 'wuauserv'

    Write-Verbose -Message 'Installing WMF 5.1...'

    $wusaExitCode = Invoke-Wusa -InstallFile $downloadLocation

    Write-Verbose -Message "Completed WMF 5.1 installation with exit code $wusaExitCode"
}

Export-ModuleMember -Function @( 'Install-Wmf5Dot1' )