|
10 | 10 | Invoke-Sysprep |
11 | 11 |
|
12 | 12 | .NOTES |
13 | | - Copyright 2024-2025 The MathWorks, Inc. |
| 13 | + Copyright 2024-2026 The MathWorks, Inc. |
14 | 14 | The $ErrorActionPreference variable is set to 'Stop' to ensure that any errors encountered during the function execution will cause the script to stop and throw an error. |
15 | 15 | #> |
16 | 16 | function Invoke-Sysprep { |
17 | | - Write-Output 'Starting Invoke-Sysprep...' |
| 17 | + try { |
| 18 | + Write-Output 'Starting Invoke-Sysprep...' |
18 | 19 |
|
19 | | - Write-Output 'Waiting for essential services to start...' |
| 20 | + Write-Output 'Waiting for essential services to start...' |
20 | 21 |
|
21 | | - # Boolean flag to check if required services are running |
22 | | - $ServicesRunning = $false |
| 22 | + # Boolean flag to check if required services are running |
| 23 | + $ServicesRunning = $false |
23 | 24 |
|
24 | | - # Start a stopwatch for timing |
25 | | - $ServicesTimeoutDuration = 300 |
26 | | - $Stopwatch = New-Object System.Diagnostics.Stopwatch |
27 | | - $Stopwatch.Start() |
| 25 | + # Start a stopwatch for timing |
| 26 | + $ServicesTimeoutDuration = 300 |
| 27 | + $Stopwatch = New-Object System.Diagnostics.Stopwatch |
| 28 | + $Stopwatch.Start() |
28 | 29 |
|
29 | | - while ($Stopwatch.Elapsed.TotalSeconds -le $ServicesTimeoutDuration) { |
30 | | - $RdAgent = Get-Service RdAgent -ErrorAction SilentlyContinue |
31 | | - $GuestAgent = Get-Service WindowsAzureGuestAgent -ErrorAction SilentlyContinue |
| 30 | + while ($Stopwatch.Elapsed.TotalSeconds -le $ServicesTimeoutDuration) { |
| 31 | + $RdAgent = Get-Service RdAgent -ErrorAction SilentlyContinue |
| 32 | + $GuestAgent = Get-Service WindowsAzureGuestAgent -ErrorAction SilentlyContinue |
32 | 33 |
|
33 | | - if (($RdAgent.Status -eq 'Running') -and ($GuestAgent.Status -eq 'Running')) { |
34 | | - Write-Output 'SUCCESS: Both RdAgent and WindowsAzureGuestAgent services are running.' |
35 | | - $ServicesRunning = $true |
36 | | - break |
| 34 | + if (($RdAgent.Status -eq 'Running') -and ($GuestAgent.Status -eq 'Running')) { |
| 35 | + Write-Output 'SUCCESS: Both RdAgent and WindowsAzureGuestAgent services are running.' |
| 36 | + $ServicesRunning = $true |
| 37 | + break |
| 38 | + } |
37 | 39 | } |
38 | | - } |
39 | 40 |
|
40 | | - $Stopwatch.Stop() |
| 41 | + $Stopwatch.Stop() |
41 | 42 |
|
42 | | - if (-not $ServicesRunning) { |
43 | | - Write-Output "Services RdAgent and WindowsAzureGuestAgent did not start within the specified timeout of $ServicesTimeoutDuration seconds." |
44 | | - throw |
45 | | - } |
| 43 | + if (-not $ServicesRunning) { |
| 44 | + Write-Output "Services RdAgent and WindowsAzureGuestAgent did not start within the specified timeout of $ServicesTimeoutDuration seconds." |
| 45 | + throw |
| 46 | + } |
46 | 47 |
|
47 | | - # Start a stopwatch for timing sysprep |
48 | | - $SysprepTimeoutDuration = 300 |
49 | | - $Stopwatch = New-Object System.Diagnostics.Stopwatch |
50 | | - $Stopwatch.Start() |
| 48 | + # Start a stopwatch for timing sysprep |
| 49 | + $SysprepTimeoutDuration = 300 |
| 50 | + $Stopwatch = New-Object System.Diagnostics.Stopwatch |
| 51 | + $Stopwatch.Start() |
| 52 | + |
| 53 | + # Start sysprep |
| 54 | + & "$Env:SystemRoot\System32\Sysprep\Sysprep.exe" /oobe /generalize /quiet /quit |
| 55 | + |
| 56 | + $ExpectedImageState = 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE' |
| 57 | + |
| 58 | + while ($Stopwatch.Elapsed.TotalSeconds -le $SysprepTimeoutDuration) { |
| 59 | + $ImageState = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\State' |
| 60 | + if ($ImageState.ImageState -ne $ExpectedImageState) { |
| 61 | + Write-Output $ImageState.ImageState |
| 62 | + Start-Sleep -Seconds 10 |
| 63 | + } |
| 64 | + else { |
| 65 | + Write-Output "Final image state: $($ImageState.ImageState)" |
| 66 | + break |
| 67 | + } |
| 68 | + } |
| 69 | + $Stopwatch.Stop() |
| 70 | + if ($ImageState.ImageState -ne $ExpectedImageState) { |
| 71 | + $FinalImageState = $ImageState.ImageState |
| 72 | + throw "Image stage is $FinalImageState after $SysprepTimeoutDuration but was expected to be $ExpectedImageState" |
| 73 | + } |
51 | 74 |
|
52 | | - # Start sysprep |
53 | | - & "$Env:SystemRoot\System32\Sysprep\Sysprep.exe" /oobe /generalize /quiet /quit |
54 | | - |
55 | | - $ExpectedImageState = 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE' |
| 75 | + Write-Output 'Done with Invoke-Sysprep.' |
56 | 76 |
|
57 | | - while ($Stopwatch.Elapsed.TotalSeconds -le $SysprepTimeoutDuration) { |
58 | | - $ImageState = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\State' |
59 | | - if ($ImageState.ImageState -ne $ExpectedImageState) { |
60 | | - Write-Output $ImageState.ImageState |
61 | | - Start-Sleep -Seconds 10 |
62 | | - } |
63 | | - else { |
64 | | - Write-Output "Final image state: $ImageState.ImageState" |
65 | | - break |
66 | | - } |
| 77 | + Write-Output 'Wait for 5 mins to let sysprep complete...' |
| 78 | + Start-Sleep -Seconds 300 |
| 79 | + Write-Output '5 mins wait is over.' |
67 | 80 | } |
68 | | - $Stopwatch.Stop() |
69 | | - if ($ImageState.ImageState -ne $ExpectedImageState) { |
70 | | - $FinalImageState = $ImageState.ImageState |
71 | | - throw "Image stage is $FinalImageState after $SysprepTimeoutDuration but was expected to be $ExpectedImageState" |
| 81 | + finally { |
| 82 | + Write-Output 'Printing last 10 lines of sysprep log for verification...' |
| 83 | + Get-FileContent -FilePath '%WINDIR%\System32\Sysprep\Panther\Setupact.log' -NLines 10 |
72 | 84 | } |
73 | | - |
74 | | - Write-Output 'Done with Invoke-Sysprep.' |
75 | | - |
76 | | - Write-Output 'Wait for 5 mins to let sysprep complete...' |
77 | | - Start-Sleep -Seconds 300 |
78 | | - Write-Output '5 mins wait is over.' |
79 | | - Write-Output 'Printing last 10 lines of sysprep log for verification...' |
80 | | - Get-FileContent -FilePath '%WINDIR%\System32\Sysprep\Panther\Setupact.log' -NLines 10 |
81 | 85 | } |
82 | 86 |
|
83 | 87 | function Get-FileContent { |
|
0 commit comments