Skip to content

Commit

Permalink
fix: Windows VHD pipeline tests (Azure#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
AbelHu authored Nov 11, 2020
1 parent fd8a533 commit 202fef7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
36 changes: 36 additions & 0 deletions vhdbuilder/packer/sysprep.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Stop and remove Azure Agents to enable use in Azure Stack
# If deploying an Azure VM the agents will be re-added to the VMs at deployment time
Stop-Service WindowsAzureGuestAgent
Stop-Service WindowsAzureNetAgentSvc
Stop-Service RdAgent
& sc.exe delete WindowsAzureGuestAgent
& sc.exe delete WindowsAzureNetAgentSvc
& sc.exe delete RdAgent

# Remove the WindowsAzureGuestAgent registry key for sysprep
# This removes AzureGuestAgent from participating in sysprep
# There was an update that is missing VMAgentDisabler.dll
$path = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\SysPrepExternal\Generalize"
$generalizeKey = Get-Item -Path $path
$generalizeProperties = $generalizeKey | Select-Object -ExpandProperty property
$values = $generalizeProperties | ForEach-Object {
New-Object psobject -Property @{"Name"=$_;
"Value" = (Get-ItemProperty -Path $path -Name $_).$_}
}

$values | ForEach-Object {
$item = $_;
if( $item.Value.Contains("VMAgentDisabler.dll")) {
Write-HOST "Removing " $item.Name - $item.Value;
Remove-ItemProperty -Path $path -Name $item.Name;
}
}

# run Sysprep
if( Test-Path $Env:SystemRoot\\system32\\Sysprep\\unattend.xml ) { Remove-Item $Env:SystemRoot\\system32\\Sysprep\\unattend.xml -Force }
& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /mode:vm /quiet /quit

# when done clean up
while($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10 } else { break } }
Get-ChildItem c:\\WindowsAzure -Force | Sort-Object -Property FullName -Descending | ForEach-Object { try { Remove-Item -Path $_.FullName -Force -Recurse -ErrorAction SilentlyContinue; } catch { } }
Remove-Item -Path WSMan:\\Localhost\\listener\\listener* -Recurse
22 changes: 5 additions & 17 deletions vhdbuilder/packer/windows-vhd-builder.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

{
"variables": {
"build_branch": "{{env `GIT_BRANCH`}}",
Expand Down Expand Up @@ -97,7 +96,7 @@
{
"elevated_user": "packer",
"elevated_password": "{{.WinRMPassword}}",
"environment_vars" : [
"environment_vars": [
"BUILD_BRANCH={{user `build_branch`}}",
"BUILD_COMMIT={{user `build_commit`}}",
"BUILD_ID={{user `build_id`}}",
Expand All @@ -114,21 +113,10 @@
"destination": "release-notes.txt"
},
{
"elevated_user": "packer",
"elevated_password": "{{.WinRMPassword}}",
"type": "powershell",
"inline": [
"& $env:SystemRoot\\System32\\Sysprep\\Sysprep.exe /oobe /generalize /mode:vm /quiet /quit",
"Stop-Service WindowsAzureGuestAgent",
"Stop-Service WindowsAzureNetAgentSvc",
"Stop-Service RdAgent",
"Stop-Service WindowsAzureTelemetryService",
"& sc.exe delete WindowsAzureGuestAgent",
"& sc.exe delete WindowsAzureNetAgentSvc",
"& sc.exe delete RdAgent",
"& sc.exe delete WindowsAzureTelemetryService",
"Get-ChildItem c:\\WindowsAzure -Force | Sort-Object -Property FullName -Descending | ForEach-Object { try { Remove-Item -Path $_.FullName -Force -Recurse -ErrorAction SilentlyContinue; } catch { } }",
"while($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10 } else { break } }",
"Remove-Item -Path WSMan:\\Localhost\\listener\\listener* -Recurse"
]
"script": "vhdbuilder/packer/sysprep.ps1"
}
]
}
}

0 comments on commit 202fef7

Please sign in to comment.