@@ -70,36 +70,6 @@ function New-Password {
7070 return $result
7171}
7272
73- <#
74- . SYNOPSIS
75- Waits for the shutdown of the specified resource.
76-
77- . DESCRIPTION
78- Wait-Shutdown takes a VM, and checks if there's a 'PowerState/stopped'
79- code; if there is, it returns. If there isn't, it waits 10 seconds and
80- tries again.
81-
82- . PARAMETER ResourceGroupName
83- The name of the resource group to look up the VM in.
84-
85- . PARAMETER Name
86- The name of the virtual machine to wait on.
87- #>
88- function Wait-Shutdown {
89- [CmdletBinding (PositionalBinding = $false )]
90- Param (
91- [Parameter (Mandatory )][string ]$ResourceGroupName ,
92- [Parameter (Mandatory )][string ]$Name
93- )
94-
95- Write-Host " Waiting for $Name to stop..."
96- $StoppedCode = ' PowerState/stopped'
97- while ($StoppedCode -notin (Get-AzVM - ResourceGroupName $ResourceGroupName - Name $Name - Status).Statuses.Code) {
98- Write-Host ' ... not stopped yet, sleeping for 10 seconds'
99- Start-Sleep - Seconds 10
100- }
101- }
102-
10373# ###################################################################################################
10474Display- ProgressBar - Status ' Silencing breaking change warnings'
10575
@@ -169,7 +139,7 @@ Display-ProgressBar -Status 'Creating prototype VM'
169139
170140# Previously: -Priority 'Spot'
171141$VM = New-AzVMConfig `
172- - Name $ProtoVMName `
142+ - VMName $ProtoVMName `
173143 - VMSize $VMSize `
174144 - Priority ' Regular'
175145
@@ -195,79 +165,79 @@ $VM = Set-AzVMBootDiagnostic `
195165 - VM $VM `
196166 - Disable
197167
198- $VM = Set-AzVMSecurityProfile `
199- - VM $VM `
200- - SecurityType ' TrustedLaunch'
201-
202- $VM = Set-AzVMUefi `
203- - VM $VM `
204- - EnableVtpm $true `
205- - EnableSecureBoot $true
206-
207168New-AzVm `
208169 - ResourceGroupName $ResourceGroupName `
209170 - Location $Location `
210171 - VM $VM | Out-Null
211172
173+ $VM = Get-AzVM `
174+ - ResourceGroupName $ResourceGroupName `
175+ - Name $ProtoVMName
176+
177+ $PrototypeOSDiskName = $VM.StorageProfile.OsDisk.Name
178+
212179# ###################################################################################################
213180Display- ProgressBar - Status ' Running provision-image.ps1 in VM'
214181
215182$ProvisionImageResult = Invoke-AzVMRunCommand `
216- - ResourceGroupName $ResourceGroupName `
217- - VMName $ProtoVMName `
183+ - ResourceId $VM.ID `
218184 - CommandId ' RunPowerShellScript' `
219185 - ScriptPath " $PSScriptRoot \provision-image.ps1"
220186
221187Write-Host $ProvisionImageResult.value.Message
222188
189+ if ($ProvisionImageResult.value.Message -cnotmatch ' PROVISION_IMAGE_SUCCEEDED' ) {
190+ Write-Host ' provision-image.ps1 failed, stopping VM...'
191+
192+ Stop-AzVM `
193+ - Id $VM.ID `
194+ - Force | Out-Null
195+
196+ Write-Error " VM stopped. Remember to delete unusable resource group: $ResourceGroupName "
197+ }
198+
223199# ###################################################################################################
224200Display- ProgressBar - Status ' Restarting VM'
225201
226- Restart-AzVM - ResourceGroupName $ResourceGroupName - Name $ProtoVMName | Out-Null
202+ Restart-AzVM `
203+ - Id $VM.ID | Out-Null
227204
228205# ###################################################################################################
229206Display- ProgressBar - Status ' Sleeping after restart'
230207
231208# The VM appears to be busy immediately after restarting.
232- # This workaround waits for a minute before attempting to run sysprep.ps1.
209+ # This workaround waits for a minute before attempting to run sysprep.
233210Start-Sleep - Seconds 60
234211
235212# ###################################################################################################
236- Display- ProgressBar - Status ' Running sysprep.ps1 in VM'
213+ Display- ProgressBar - Status ' Running sysprep in VM'
237214
238215Invoke-AzVMRunCommand `
239- - ResourceGroupName $ResourceGroupName `
240- - VMName $ProtoVMName `
216+ - ResourceId $VM.ID `
241217 - CommandId ' RunPowerShellScript' `
242- - ScriptPath " $PSScriptRoot \ sysprep.ps1 " | Out-Null
218+ - ScriptString ' C:\Windows\system32\ sysprep\sysprep.exe /oobe /generalize /mode:vm /shutdown ' | Out-Null
243219
244220# ###################################################################################################
245221Display- ProgressBar - Status ' Waiting for VM to shut down'
246222
247- Wait-Shutdown - ResourceGroupName $ResourceGroupName - Name $ProtoVMName
223+ while (' PowerState/stopped' -notin (Get-AzVM - ResourceId $VM.ID - Status).Statuses.Code) {
224+ Start-Sleep - Seconds 10
225+ }
248226
249227# ###################################################################################################
250228Display- ProgressBar - Status ' Stopping VM'
251229
252230Stop-AzVM `
253- - ResourceGroupName $ResourceGroupName `
254- - Name $ProtoVMName `
231+ - Id $VM.ID `
255232 - Force | Out-Null
256233
257234# ###################################################################################################
258235Display- ProgressBar - Status ' Generalizing VM'
259236
260237Set-AzVM `
261- - ResourceGroupName $ResourceGroupName `
262- - Name $ProtoVMName `
238+ - Id $VM.ID `
263239 - Generalized | Out-Null
264240
265- $VM = Get-AzVM `
266- - ResourceGroupName $ResourceGroupName `
267- - Name $ProtoVMName
268-
269- $PrototypeOSDiskName = $VM.StorageProfile.OsDisk.Name
270-
271241# ###################################################################################################
272242Display- ProgressBar - Status ' Creating gallery'
273243
0 commit comments