Functions/Invoke-WindowsImageUpdate.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#requires -Version 2
function Invoke-WindowsImageUpdate
{
    <#
            .Synopsis
            Starts the process of applying updates to all (or selected) images in a Windows Image Tools BaseImages Folder
            .DESCRIPTION
            This Command updates all (or selected) the images created via Add-UpdateImage in a Windows Image Tools BaseImages folder
            New-WindowsImageToolsExample can be use to create the structrure
            .EXAMPLE
            Invoke-WindowsImageUpdate -Path C:\WITExample
            Update all the Images created with Add-UpdateImage located in C:\WITExample\BaseImages and place the resulting VHD and WIM in c:\WITExample\UpdatedImageShare
            .EXAMPLE
            Invoke-WindowsImageUpdate -Path C:\WITExample -Name 2012r2Wmf5
            Update Image named 2012r2Wmf5_Base.vhdx in C:\WITExample\BaseImages and place the resulting VHD and WIM in c:\WITExample\UpdatedImageShare
    #>

    [CmdletBinding(SupportsShouldProcess = $true)]
    [OutputType([bool])]
    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
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Alias('FriendlyName')]
        [string[]]
        $ImageName,
        
        # Reduce output file by removing feature sources
        [switch]
        $ReduceImageSize,

        # what files to export if upates are added : NONE, WIM, Both (wim and vhdx) default = both
        [ValidateSet('NONE', 'WIM', 'Both')]
        [string]
        $output = 'Both'

    )

    $ParametersToPass = @{}
    foreach ($key in ('Whatif', 'Verbose', 'Debug'))
    {
        if ($PSBoundParameters.ContainsKey($key)) 
        {
            $ParametersToPass[$key] = $PSBoundParameters[$key]
        }
    }

    #region validate input
    try
    {
        $null = Test-Path -Path "$Path\BaseImage" -ErrorAction Stop
        $null = Test-Path -Path "$Path\Resource" -ErrorAction Stop
        $null = Test-Path -Path "$Path\UpdatedImageShare" -ErrorAction Stop
        $null = Test-Path -Path "$Path\config.xml" -ErrorAction Stop
    }
    catch
    {
        throw "$Path folder structure incorrect, see New-WindowsImageToolsExample for an example"
    }
    
    if ($ImageName)
    {
        foreach ($testpath in $ImageName) 
        {
            Write-Verbose -Message "[$($MyInvocation.MyCommand)] : Validateing [$testpath]"
            if (-not (Test-Path -Path "$Path\BaseImage\$($testpath)_base.vhdx" ))
            
            {
                throw "$Path\BaseImage\$($testpath)_base.vhdx"
            }
        }
        $ImageList = $ImageName
    }
    else 
    {
        Write-Verbose -Message "[$($MyInvocation.MyCommand)] : Colecting List of Images"
        $ImageList = (Get-ChildItem -Path $Path\BaseImage\*_Base.vhdx).Name -replace '_Base.vhdx', ''
    }

    $configData = Import-Clixml -Path "$Path\config.xml"

    try
    {
        Write-Verbose -Message "[$($MyInvocation.MyCommand)] : Validateing VM switch config"
        $null = Get-VMSwitch -Name $configData.VmSwitch -ErrorAction Stop
    }
    catch
    {
        throw "VM Switch Configuration in $Path incorrect Set-UpdateConfig"
    }

    #endregion
    
    #region update resorces folder
    if ($pscmdlet.ShouldProcess('PowerShell Gallery', 'Download required Modules'))
    {
        if (-not (Test-Path -Path $Path\Resource\Modules)) 
        {
            $null = mkdir -Path $Path\Resource\Modules
        }
        if (-not (Get-Command Save-Module))
        {
            Write-Warning -Message 'PowerShellGet missing. you will need to download required modules from PowerShell Gallery manualy'
            Write-Warning -Message 'Required Modules : PSWindowsUpdate'
        }
        else 
        {
            Write-Verbose -Message "[$($MyInvocation.MyCommand)] : Geting latest PSWindowsUpdate"
            try 
            {
                # if nuget needs updating this prompts
                ### To-Do find a way to silenty update nuget ###
                $null = Save-Module -Name PSWindowsUpdate -Path $Path\Resource\Modules -Force -ErrorAction Stop @ParametersToPass
            }
            catch 
            {
                if (Test-Path -Path $Path\Resource\Modules\PSWindowsUpdate)
                {
                    Write-Warning -Message "[$($MyInvocation.MyCommand)] : PSwindowsUpdate present, but unable to download latest"
                }
                else 
                {
                    throw "unable to download PSWindowsUpdate from PowerShellGalary.com, download manualy and place in $Path\Resource\Modules "
                }
            }
        }
    }
    #endregion

    #region Process Images
    foreach ($TargetImage in $ImageList)
    { 
        if ($pscmdlet.ShouldProcess($TargetImage, 'Invoke Windows Updates on Image'))
        {
            #region setup enviroment
            $BaseImage = "$Path\BaseImage\$($TargetImage)_base.vhdx"
            $UpdateImage = "$Path\BaseImage\$($TargetImage)_Update.vhdx"
            $SysprepImage = "$Path\BaseImage\$($TargetImage)_Sysprep.vhdx"
            $OutputVhd = "$Path\UpdatedImageShare\$($TargetImage).vhdx"
            $OutputWim = "$Path\UpdatedImageShare\$($TargetImage).wim"

            $vmGeneration = 1
            $PartitionStyle = GetVHDPartitionStyle -vhd $BaseImage
            if ($PartitionStyle -eq 'GPT') 
            {
                $vmGeneration = 2
            }
            $configData = Get-UpdateConfig -Path $Path

            $vhdData = Get-VHD -Path $BaseImage
            #endregion

            #region create Diff disk
            try 
            { 
                Write-Verbose -Message "[$($MyInvocation.MyCommand)] : Windows Update : New Diff Disk : Creating $UpdateImage from $BaseImage"
                $null = New-VHD -Path $UpdateImage -ParentPath $BaseImage -ErrorAction Stop @ParametersToPass
            }
            catch 
            {
                throw "error creating differencing disk $UpdateImage from $BaseImage"
            }
            #endregion

            #region Inject files
            $RunWindowsUpdateAtStartup = {
                Start-Transcript -Path $PSScriptRoot\AtStartup.log -Append
                
                $IpType = 'IPTYPEPLACEHOLDER'
                $IPAddress = 'IPADDRESSPLACEHOLDER'
                $SubnetMask = 'SUBNETMASKPLACEHOLDER'
                $Gateway = 'GATEWAYPLACEHOLDER'
                $DnsServer = 'DNSPLACEHOLDER'
                
                if (-not ($IpType -eq 'DHCP'))
                {
                    Write-Verbose -Message 'Set Network : Getting network adaptor' -Verbose
                    $adapter = Get-NetAdapter | Where-Object -FilterScript {
                        $_.Status -eq 'up'
                    }
                    
                    Write-Verbose -Message "Set Network : removing existing config on $($adaptor.Name)" -Verbose
                    If (($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress) 
                    {
                        $adapter | Remove-NetIPAddress -AddressFamily $IpType -Confirm:$false
                    }
                    If (($adapter | Get-NetIPConfiguration).Ipv4DefaultGateway) 
                    {
                        $adapter | Remove-NetRoute -AddressFamily $IpType -Confirm:$false
                    }
                    
                    $params = {
                        AddressFamily = $IpType
                        IPAddress = $IPAddress
                        PrefixLength = $SubnetMask
                        DefaultGateway = $Gateway
                    }
                    Write-Verbose -Message 'Set Network : Adding settings to adaptor'
                    Write-Verbose -Message $params -Verbose
                    $adapter | New-NetIPAddress @params
                    
                    Write-Verbose "Set Network : Set DNS to $DnsServer" -Verbose
                    $adapter | Set-DnsClientServerAddress -ServerAddresses $DnsServer  
                }

                try 
                {
                    Import-Module "$env:SystemDrive\PsTemp\Modules\PSWindowsUpdate" -Force -ErrorAction Stop
                }
                catch
                {
                    Write-Error 'Unable to import update module'
                    Stop-Transcript
                    Stop-Computer -Force
                }
                
                # Run pre-update script if it exists
                if (Test-Path "$env:SystemDrive\PsTemp\PreUpdateScript.ps1") 
                {
                    Write-Verbose "Pre-Upate script : found $env:SystemDrive\PsTemp\PreUpdateScript.ps1"
                    & "$env:SystemDrive\PsTemp\PreUpdateScript.ps1"
                }

                if ((Get-WUList -verbose -NotCategory 'Language packs').Count -gt 0)
                {
                    Write-Verbose 'Windows updates : Updates needed, flaging drive as changed' -Verbose
                    Get-Date | Out-File $env:SystemDrive\PsTemp\changesMade.txt -Force
                }
                else 
                {
                    Write-Verbose 'Windows updates : No further updates' -Verbose
                
                    if(-not ($IpType -eq 'DHCP')) 
                    {
                        $adapter = Get-NetAdapter | Where-Object {
                            $_.Status -eq 'up'
                        }
                        $interface = $adapter | Get-NetIPInterface -AddressFamily $IpType

                        Write-Verbose 'Set Network : Removing static config' -Verbose
                        If ($interface.Dhcp -eq 'Disabled') 
                        {
                            If (($interface | Get-NetIPConfiguration).Ipv4DefaultGateway) 
                            {
                                $interface | Remove-NetRoute -Confirm:$false
                            }
                            $interface | Set-NetIPInterface -Dhcp Enabled
                            $interface | Set-DnsClientServerAddress -ResetServerAddresses
                        }
                    }
                    Write-Verbose 'Shuting down' -Verbose
                    ## remove self so as to not triger updates if manual mantinance required
                    Remove-Item "$env:SystemDrive\PsTemp\AtStartup.ps1"
                    Stop-Transcript
                    Stop-Computer 
                }
 
                # Apply all non-language updates
                Write-Verbose 'Windows updates : installing updates' -Verbose
                Get-WUInstall -AcceptAll -IgnoreReboot -IgnoreUserInput -NotCategory 'Language packs' -Verbose

                # Run post-update script if it exists
                if (Test-Path "$env:SystemDrive\PsTemp\PostUpdateScript.ps1") 
                {
                    Write-Verbose "Post-Update script : found $env:SystemDrive\PsTemp\PostUpdateScript.ps1"
                    & "$env:SystemDrive\PsTemp\PostUpdateScript.ps1"
                }

 
                if (Get-WURebootStatus -Silent) 
                {
                    Write-Verbose 'Windows updates : Reboot required to finish restarting' -Verbose
                } 
                else
                {
                    Write-Verbose 'Windows updates : Restarting to check for additional updates' -Verbose
                }
                Stop-Transcript
                Restart-Computer -Force
            }

            #region add configuration data into block
            $block = $RunWindowsUpdateAtStartup | Out-String -Width 400
    
            $block = $block.Replace('IPTYPEPLACEHOLDER', $configData.IpType)
            $block = $block.Replace('IPADDRESSPLACEHOLDER', $configData.IPAddress)
            $block = $block.Replace('SUBNETMASKPLACEHOLDER', $configData.SubnetMask)
            $block = $block.Replace('GATEWAYPLACEHOLDER', $configData.Gateway)
            $block = $block.Replace('DNSPLACEHOLDER', $configData.DnsServer)
            
            $RunWindowsUpdateAtStartup = [scriptblock]::Create($block)
            #endregion
            
            $CopyInUpdateFilesBlock = {
                if (-not (Test-Path -Path "$($driveLetter):\PsTemp"))
                {
                    $null = mkdir -Path "$($driveLetter):\PsTemp"
                }
                if (-not (Test-Path -Path "$($driveLetter):\PsTemp\Modules"))
                {
                    $null = mkdir -Path "$($driveLetter):\PsTemp\Modules"
                }
                $null = New-Item -Path "$($driveLetter):\PsTemp" -Name AtStartup.ps1 -ItemType 'file' -Value $RunWindowsUpdateAtStartup -Force
                cleanupFile "$($driveLetter):\PsTemp\Modules\*"
                $null = Copy-Item -Path "$Path\Resource\Modules\*" -Destination "$($driveLetter):\PsTemp\Modules\" -Recurse

                if ((Get-ChildItem "$($driveLetter):\PsTemp\Modules\PSWindowsUpdate" -File).count -eq 0)
                {
                    Write-Verbose -Message 'Sidebyside detected in PSWindowsUpdate : switching to v4 compatability'
                    $newest = (Get-ChildItem "$($driveLetter):\PsTemp\Modules\PSWindowsUpdate" -Directory | Sort-Object LastWriteTime)[0] 
                    Copy-Item -Path $newest.fullname -Destination "$($driveLetter):\PsTemp\Modules\PSWindowsUpdate_temp" -Recurse
                    cleanupFile "$($driveLetter):\PsTemp\Modules\PSWindowsUpdate"
                    Rename-Item -Path "$($driveLetter):\PsTemp\Modules\PSWindowsUpdate_temp" -NewName "$($driveLetter):\PsTemp\Modules\PSWindowsUpdate" 
                }
            }
            Write-Verbose -Message "[$($MyInvocation.MyCommand)] : Windows Update : Adding PSWindowsUpdate Module to $UpdateImage"
            Write-Verbose -Message "[$($MyInvocation.MyCommand)] : Windows Update : updateting AtStartup script"
            MountVHDandRunBlock -vhd $UpdateImage -block $CopyInUpdateFilesBlock 
            #endregion

            #region create vm and run updates
            createRunAndWaitVM -vhdPath $UpdateImage -vmGeneration $vmGeneration -configData $configData @ParametersToPass
            #endregion

            #region Detect results - Merge or discard.
            $checkresultsBlock = {
                Test-Path -Path "$($driveLetter):\PsTemp\ChangesMade.txt"
                Remove-Item "$($driveLetter):\PsTemp\ChangesMade.txt" -ErrorAction SilentlyContinue
            }
            $ChangesMade = MountVHDandRunBlock -vhd $UpdateImage -block $checkresultsBlock
            if ($ChangesMade)
            {
                Write-Verbose -Message "[$($MyInvocation.MyCommand)] : Windows Update : Changes detected : Merging $UpdateImage into $BaseImage"
                Merge-VHD -Path $UpdateImage -DestinationPath $BaseImage @ParametersToPass
            }
            else 
            {
                Write-Verbose -Message "[$($MyInvocation.MyCommand)] : Windows Update : No changes, discarding $UpdateImage" 
                cleanupFile $UpdateImage
            }
            #endregion
      
            if ($output -ne 'none')
            { 
                #region Sysprep if changes or missing output vhd
                if (($ChangesMade) -or (-not (Test-Path $OutputVhd)))
                {
                    try 
                    { 
                        Write-Verbose -Message "[$($MyInvocation.MyCommand)] : SysPrep : New Diff Disk : Creating $SysprepImage from $BaseImage"
                        cleanupFile $SysprepImage
                        $null = New-VHD -Path $SysprepImage -ParentPath $BaseImage -ErrorAction Stop @ParametersToPass
                    }
                    catch 
                    {
                        throw "error creating differencing disk $SysprepImage from $BaseImage"
                    }
                
      
                    $sysprepAtStartup = {
                        Start-Transcript -Path $PSScriptRoot\AtStartup.log -Append
                        # Run pre-sysprep script if it exists
                        if (Test-Path "$env:SystemDrive\PsTemp\PreSysprepScript.ps1") 
                        {
                            & "$env:SystemDrive\PsTemp\PreSysprepScript.ps1"
                        }
                    
      
                        # Remove Scedualed task
                        Write-Verbose -Message 'SysPrep : Removeing AtStartup task' -Verbose
                        if (Get-Command -Name Unregister-ScheduledTask -ErrorAction SilentlyContinue)
                        {
                            Unregister-ScheduledTask -TaskName AtStartup -Confirm:$false -Verbose
                        }
                        else 
                        {
                            schtasks.exe /delete /TN 'AtStartup' /f
                        }
                        $params = @{
                            'FilePath'             = "$ENV:SystemRoot\System32\Sysprep\Sysprep.exe"
                            'ArgumentList'         = '/generalize', '/oobe', '/shutdown'
                            'NoNewWindow'          = $true
                            'Wait'                 = $true
                            'RedirectStandardOutput' = "$($env:temp)\$($exeName)-StandardOutput.txt"
                            'RedirectStandardError' = "$($env:temp)\$($exeName)-StandardError.txt"
                            'PassThru'             = $true
                        }
      
                        Write-Verbose -Message 'SysPrep : starting Sysprep' -Verbose
                        $ret = Start-Process @params
                        Start-Sleep -Seconds 30
                        Get-Date | Out-File c:\sysprepfail.txt
                    }
      
                    $CopyInSysprepFilesBlock = {
                        $null = New-Item -Path "$($driveLetter):\PsTemp" -Name AtStartup.ps1 -ItemType 'file' -Value $sysprepAtStartup -Force
                    }
                    Write-Verbose -Message "[$($MyInvocation.MyCommand)] : SysPrep : updateting AtStartup script"
                    MountVHDandRunBlock -vhd $SysprepImage -block $CopyInSysprepFilesBlock 
                    Write-Verbose -Message "[$($MyInvocation.MyCommand)] : SysPrep : Creating temp vm and waiting"
                    createRunAndWaitVM -vhdPath $SysprepImage -vmGeneration $vmGeneration -configData $configData @ParametersToPass
             
                    MountVHDandRunBlock -vhd $SysprepImage -block {
                        if (Test-Path "$($driveLetter):\sysprepfail.txt")
                        {
                            throw 'Sysprep Failed!'
                        }
                    }
                
                    $CleanupVhdBlock = {
                        cleanupFile "$($driveLetter):\Unattend.xml"
                        cleanupFile "$($driveLetter):\PsTemp"
                        attrib.exe -s -h "$($driveLetter):\pagefile.sys"
                        cleanupFile "$($driveLetter):\pagefile.sys"
                        if ($ReduceImageSize)
                        { 
                            $null = Dism.exe /image:$($driveLetter):\ /Cleanup-Image /StartComponentCleanup /ResetBase
                            $null = Get-WindowsOptionalFeature -Path "$($driveLetter):\" |
                            Where-Object State -EQ -Value 'Disabled' |
                            Disable-WindowsOptionalFeature -Remove -Path "$($driveLetter):\" @ParametersToPass
                        }
                    }
                    Write-Verbose -Message "[$($MyInvocation.MyCommand)] : SysPrep : Removing PageFile and PsTemp"
                    Write-Verbose -Message "[$($MyInvocation.MyCommand)] : SysPrep : Cleaning SxS"
                    MountVHDandRunBlock -vhd $SysprepImage -block $CleanupVhdBlock
                }
                #endregion
      
                #region export WIM
                if (($ChangesMade) -or (-not (Test-Path $OutputWim)) -or (-not (Test-Path $OutputVhd)))
                { 
                    Write-Verbose -Message "[$($MyInvocation.MyCommand)] : WIM : Creating $OutputWim"
                    cleanupFile $OutputWim
                    MountVHDandRunBlock -ReadOnly $SysprepImage -block {
                        $nul = New-WindowsImage -CapturePath "$($driveLetter):" -ImagePath $OutputWim -Name "$TargetImage Updated $(Get-Date)" @ParametersToPass
                    }
                    Write-Verbose -Message "[$($MyInvocation.MyCommand)] : WIM : removing $SysprepImage"
                    cleanupFile $SysprepImage
                }
            
                #endregion
      
                #region create output VHD
                if ((($ChangesMade) -or (-not (Test-Path $OutputVhd))) -and $output -eq 'both')
                {
                    Write-Verbose -Message "[$($MyInvocation.MyCommand)] : VHD : Creating $OutputVhd from $OutputWim"
                    cleanupFile $OutputVhd
                    $layout = 'BIOS'
                    if ($PartitionStyle -eq 'GPT')
                    {
                        $layout = 'UEFI'
                    }
                    $dynamic = $false
                    if ($vhdData.VhdType -eq 'Dynamic')
                    {
                        $dynamic = $true
                    }
                    $param = @{
                        Path       = "$OutputVhd"
                        Size       = $vhdData.Size
                        dynamic    = $dynamic
                        DiskLayout = $layout
                        force      = $true
                        SourcePath = "$OutputWim"
                    }
                    $nul = Convert-Wim2VHD @param @ParametersToPass 
                }
                #endregion
            }
        }
    }
    #endregion
}