Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
Disable Azure Agents and remove from sysprep
Browse files Browse the repository at this point in the history
  • Loading branch information
jsturtevant committed Oct 28, 2020
1 parent 18507b6 commit e72a629
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
36 changes: 36 additions & 0 deletions vhd/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
17 changes: 3 additions & 14 deletions vhd/packer/windows-vhd-builder.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,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": "vhd/packer/sysprep.ps1"
}
]
}

0 comments on commit e72a629

Please sign in to comment.