Functions/Update-WindowsImageWMF.ps1

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<#
        .Synopsis
        Updates WMF to 4.0, 5.0 Production Preview or 5.0 (and .NET to 4.6) in a Windows Update Image
        .DESCRIPTION
        This Command downloads WMF 4.0, 5.0PP or 5.0 (Production Preview) and .NET 4.6 offline installer
        Creates a temp VM and updates .NET if needed and WMF
        .EXAMPLE
        Update-UpdateImageWMF -Path C:\WITExample
        Updates every Image in c:\WITExample\BaseImages
        .EXAMPLE
        Update-UpdateImageWMF -Path C:\WitExample -Name Server2012R2Core
        Updates only C:\WitExample\BaseImages\Server2012R2Core_Base.vhdx
#>

function Update-WindowsImageWMF
{
    [CmdletBinding(SupportsShouldProcess = $true)]
    #[OutputType([String])]
    Param
    (
        # Path to the Windows Image Tools Update Folders (created via New-WindowsImageToolsExample)
        [Parameter(Mandatory = $true, 
        ValueFromPipelineByPropertyName = $true)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [ValidateScript({
                    if (Test-Path $_) 
                    {
                        $true
                    }
                    else 
                    {
                        throw "Path $_ does not exist"
                    }
        })]
        [Alias('FullName')] 
        $Path,
 
        # Name of the Image to update
        [Parameter(Mandatory = $true)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Alias('FriendlyName')]
        [string[]]
        $ImageName,

        # Use WMF 4 instead of the default WMF 5
        [switch]
        $Wmf4,

        # Use WMF5 Production Preview instead of the default WMF 5 (overrides -vmf4)
        [switch]
        $Wmf5pp

    )

    foreach ($image in $ImageName) 
    {
        $parentVHD = "$Path\BaseImage\$($image)_Base.vhdx"
        $target = "$Path\BaseImage\$($image)_Update.vhdx"
    
        if ($pscmdlet.ShouldProcess("$parentVHD", 'Update WMF in Windows Image Tools Update Image'))
        {
            $ParametersToPass = @{}
            foreach ($key in ('Whatif', 'Verbose', 'Debug'))
            {
                if ($PSBoundParameters.ContainsKey($key)) 
                {
                    $ParametersToPass[$key] = $PSBoundParameters[$key]
                }
            }
        
            Write-Verbose -Message "[$($MyInvocation.MyCommand)] : Creating $target from $parentVHD"
            $null = New-VHD -Path $target -ParentPath $parentVHD
                
            #region Validate Input
            try 
            {
                $null = Test-Path -Path "$Path\BaseImage" -ErrorAction Stop
                $null = Test-Path -Path "$Path\Resource" -ErrorAction Stop
            }
            catch
            {
                Throw "$Path missing required folder structure use New-WindowsImagetoolsExample to create example"
            }
        
            if (-not(Test-Path -Path "$Path\BaseImage\$($ImageName)_Base.vhdx"))
            {
                Throw "BaseImage for $ImageName does not exists. Use Add-UpdateImage first"
            }
            #endregion

            #region Update Resource Folder
            ## download WMF
            $wmfPath = "$Path\Resource\WMF\5"
            $wmfDownloadUrl = 'http://aka.ms/wmf5latest'
        
            if ($Wmf4)
            {
                $wmfPath = "$Path\Resource\WMF\4"
                $wmfDownloadUrl = 'http://www.microsoft.com/en-us/download/details.aspx?id=40855'
            }
            if ($Wmf5pp)
            {
                $wmfPath = "$Path\Resource\WMF\5pp"
                $wmfDownloadUrl = 'https://www.microsoft.com/en-us/download/details.aspx?id=48729'
            }
            try
            { 
                if (-not (Test-Path -Path $wmfPath)) 
                {
                    $nul = mkdir -Path $wmfPath
                } 
                Write-Verbose -Message "[$($MyInvocation.MyCommand)] : Checking for the latest WMF in $wmfPath"
                $confirmationPage = 'http://www.microsoft.com/en-us/download/' +  $((Invoke-WebRequest -Uri $wmfDownloadUrl -UseBasicParsing).links | 
                    Where-Object -Property Class -EQ -Value 'mscom-link download-button dl' |
                ForEach-Object -MemberName href) 
                $directURLs = (Invoke-WebRequest -Uri $confirmationPage -UseBasicParsing).Links | 
                Where-Object -Property Class -EQ -Value 'mscom-link' |
                Where-Object -Property href -Like -Value '*.msu' |
                ForEach-Object -MemberName href
                foreach ($directURL in $directURLs)
                {
                    $filename = $directURL -split '/' | Select-Object -Last 1
                    if (-not (Test-Path -Path "$wmfPath\$filename" ))
                    { 
                        Write-Warning -Message "[$($MyInvocation.MyCommand)] : Checking for the latest WMF : $filename Missing, Downloading"
                        $download = Invoke-WebRequest -Uri $directURL -OutFile "$wmfPath\$filename" 
                    }
                    else
                    {
                        Write-Verbose -Message "[$($MyInvocation.MyCommand)] : Checking for the latest WMF : $wmfPath\$filename : Found"
                    }
                }
            }
            catch 
            {
                if (-not (Test-Path -Path "$wmfPath\*.msu"))
                {
                    throw "Unable to downlaod WMF to $wmfPath. please download WMF manualy and place in $wmfPath "
                }
            }
        

            ## download .NET 4.6
            try
            {
                if (-not (Test-Path -Path $Path\Resource\dotNET)) 
                {
                    mkdir -Path $Path\Resource\dotNET
                } 
                Write-Verbose -Message "[$($MyInvocation.MyCommand)] : Checking for .NET 4.6"
                $directURL = 'https://download.microsoft.com/download/C/3/A/C3A5200B-D33C-47E9-9D70-2F7C65DAAD94/NDP46-KB3045557-x86-x64-AllOS-ENU.exe'
                $filename = 'dotNet4-6.exe'
                if (-not (Test-Path -Path "$Path\Resource\dotNET\$filename" ))
                { 
                    Write-Warning -Message "[$($MyInvocation.MyCommand)] : Checking for .NET 4.6 : Missing : Downloading"
                    $download = Invoke-WebRequest -Uri $directURL -OutFile "$Path\Resource\dotNET\$filename" 
                }    
            }
            catch 
            {
                if (-not (Test-Path -Path "$Path\Resource\dotNET\$filename"))
                {
                    throw "Unable to downlaod .net 4.6 to $Path\Resource\dotNET\$filename. please download .net 4.6 manualy "
                }
            }
            #endregion
       
            #region Install .NET
            $dotNetInstallAtStartup = {
                Start-Transcript -Path $PSScriptRoot\AtStartup.log -Append
                $currentDotNetVersionv = (Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse |
                    Get-ItemProperty -Name Version, Release -EA 0 |
                    Where-Object -FilterScript {
                        $_.PSChildName -match '^(?!S)\p{L}'
                    }  | 
                    Sort-Object -Property version -Descending |
                Select-Object -First 1 ).version 
                if ($currentDotNetVersionv -lt 4.6)
                {
                    if (-not (Test-Path -Path c:\PsTemp\dotNET\attempt.txt))
                    {  
                        Get-Date | Out-File -FilePath c:\PsTemp\dotNET\attempt.txt
                        Write-Verbose -Message '.Net 4.6 : Installing' -Verbose
                        Start-Process  -Verb runas -Wait -FilePath 'C:\PsTemp\dotNET\dotNet4-6.exe' -ArgumentList '/q', '/norestart', '/log c:\PsTemp\dotNet\dotNetLog.htm'
                    }
                
                    else 
                    {
                        Write-Error -Message '.Net 4.6 : install attempted but failed!'
                        Start-Sleep -Seconds 30
                        # Stop-Computer does not have -force in 2008/win7 WMF2
                        if ((Get-Command Stop-Computer -Syntax) -like '*[force]*') 
                        {
                            Stop-Computer -Verbose -Force
                        }
                        else
                        {
                            shutdown.exe /s /t 0 /f
                        }
                        Stop-Transcript
                    }
                }
                else 
                {
                    Get-Date | Out-File -FilePath c:\PsTemp\dotNET\Verified.txt
                    Write-Verbose -Message '.Net 4.6 : detected shuting down' -Verbose
                    # Stop-Computer does not have -force in 2008/win7 WMF2
                    if ((Get-Command Stop-Computer -Syntax) -like '*[force]*') 
                    {
                        Stop-Computer -Verbose -Force
                    }
                    else
                    {
                        shutdown.exe /s /t 0 /f
                    }
                    Stop-Transcript
                }
                Start-Sleep -Seconds 30
                Write-Verbose -Message 'Rebooting computer' -Verbose
                # Restart-Computer does not have -force in 2008/win7 WMF2
                if ((Get-Command Restart-Computer -Syntax) -like '*[force]*') 
                {
                    Restart-Computer -Verbose -Force
                }
                else
                {
                    shutdown.exe /r /t 0 /f
                }
                Stop-Transcript
            }

            $AddDotNetFilesBlock = {
                if (-not (Test-Path -Path "$($driveLetter):\PsTemp"))
                {
                    $null = mkdir -Path "$($driveLetter):\PsTemp"
                }
                if (-not (Test-Path -Path "$($driveLetter):\PsTemp\dotNET"))
                {
                    $null = mkdir -Path "$($driveLetter):\PsTemp\dotNET"
                }
                $null = New-Item -Path "$($driveLetter):\PsTemp" -Name AtStartup.ps1 -ItemType 'file' -Value $dotNetInstallAtStartup -Force
                $null = Copy-Item -Path "$Path\Resource\dotNET\$filename" -Destination "$($driveLetter):\PsTemp\dotNET\$filname"
            }


            Write-Verbose -Message "[$($MyInvocation.MyCommand)] : .NET : Adding installer to $target"
            Write-Verbose -Message "[$($MyInvocation.MyCommand)] : .NET : updateting AtStartup script"
            MountVHDandRunBlock -vhd $target -block $AddDotNetFilesBlock
            $vmGeneration = 1
            if ((GetVHDPartitionStyle -vhd $target) -eq 'GPT') 
            {
                $vmGeneration = 2
            }
            $ConfigData = Get-UpdateConfig -Path $Path
            
            Write-Verbose -Message "[$($MyInvocation.MyCommand)] : .NET : Creating temp vm and waiting "
            createRunAndWaitVM -vhdPath $target -vmGeneration $vmGeneration -configData $ConfigData @ParametersToPass
            #endregion

            #region Install WMF
            $verifyWmfVersion4 = {
                Start-Transcript -Path $PSScriptRoot\AtStartup.log -Append
                if ($PSVersionTable.PSVersion.Major -ge 4)
                {
                    Write-Verbose -Message 'WMF : version varified'
                    Get-Date | Out-File -FilePath c:\PsTemp\ChangesMade.txt
                }
                else 
                {
                    Write-Warning -Message "WMF : Excpected version 4, found $($PSVersionTable.PSVersion.Major)"
                }
                Stop-Transcript
                Stop-Computer -Force
            }
            $verifyWmfVersion5 = {
                Start-Transcript -Path $PSScriptRoot\AtStartup.log -Append
                if ($PSVersionTable.PSVersion.Major -ge 5)
                {
                    Write-Verbose -Message 'WMF : version varified'
                    Get-Date | Out-File -FilePath c:\PsTemp\ChangesMade.txt
                }
                else 
                {
                    Write-Warning -Message "WMF : Excpected version 4, found $($PSVersionTable.PSVersion.Major)"
                }
                Stop-Transcript
                Stop-Computer -Force
            }
        
            if ($Wmf4)
            {
                $VeirfyWmfAtStartup = $verifyWmfVersion4
            }
            else 
            {
                $VeirfyWmfAtStartup = $verifyWmfVersion5
            }

            $addWmfFilesBlock = {
                foreach ($update in (Get-ChildItem -Path $wmfPath\*.msu ).FullName )
                {
                    Write-Verbose -Message "checking if $update applies to $target"
                    $null = Add-WindowsPackage -PackagePath $update -Path "$($driveLetter):" 
                }
                $null = New-Item -Path "$($driveLetter):\PsTemp" -Name AtStartup.ps1 -ItemType 'file' -Value $VeirfyWmfAtStartup -Force
            }

            Write-Verbose -Message "[$($MyInvocation.MyCommand)] : WMF : Applying WMF to $target and Updating AtStartup script"
            MountVHDandRunBlock -vhd $target -block $addWmfFilesBlock
            Write-Verbose -Message "[$($MyInvocation.MyCommand)] : WMF : creating temp VM to finalize install on $target"
            createRunAndWaitVM -vhdPath $target -vmGeneration $vmGeneration -configData $ConfigData @ParametersToPass
            #endregion

            #region check for changes and merge or delete
            Write-Verbose -Message "[$($MyInvocation.MyCommand)] : WMF : Checking if changes made"
            $checkresultsBlock = {
                Test-Path -Path "$($driveLetter):\PsTemp\ChangesMade.txt"
                if (Test-Path -Path "$($driveLetter):\PsTemp\ChangesMade.txt")
                {
                    Remove-Item -Path "$($driveLetter):\PsTemp\AtStartup.ps1" -ErrorAction SilentlyContinue
                }
            }
            $ChangesMade = MountVHDandRunBlock -vhd $target -block $checkresultsBlock
            if ($ChangesMade)
            {
                Write-Verbose -Message "[$($MyInvocation.MyCommand)] : WMF : Changes found : Merging $target into $parentVHD"
                Merge-VHD -Path $target -DestinationPath $parentVHD
            }
            else 
            {
                Write-Verbose -Message "[$($MyInvocation.MyCommand)] : WMF : No Changes : Discarding $target"
                Remove-Item $target
            }
            #endregion
        }
    }
}