Recover a Windows maintenance setup interrupted before policy activation

This commit is contained in:
2026-11-17 17:32:17 +01:00
parent fe55995154
commit 8c39e79a79
9 changed files with 303 additions and 63 deletions
@@ -88,6 +88,13 @@ $binding=[ordered]@{tenant_id=$clock.tenant_id;asset_id=$AssetId.ToString();agen
$inputDto=[ordered]@{binding=$binding;release_id=$ReleaseId.ToString();agent_sha256=(GetHash $agentExe);helper_sha256=(GetHash $helperExe);max_runtime_seconds=$Seconds}
$markerJson=([ordered]@{case_id=$CaseId.ToString();release_id=$ReleaseId.ToString();agent_id=$AgentId.ToString();asset_id=$AssetId.ToString()}|ConvertTo-Json -Compress)
$archive=$root+'.closed-'+$CaseId.ToString()
$staging=$root+'.preparing-'+$CaseId.ToString()
function PublishMarker($destination,$readers) {
if(Test-Path -LiteralPath $destination){VerifyMarker $destination;return}
NewPrivateDir $staging $readers
WriteFixed (Join-Path $staging 'windows-update-enrollment.json') $markerJson
[IO.Directory]::Move($staging,$destination)
}
$backup=Join-Path $root 'original-agent-config.json'
$written=Join-Path $root 'active-agent-config.json'
$gate=$null
@@ -111,7 +118,7 @@ function StartNormal {
function RemoveWindow {
if (!(Test-Path -LiteralPath $root)) {
Check (!(Service $helper) -and !(JsonFile $cfg).windows_update_helper) 'Helper SCM or configuration present without its protected case marker.'
if(Test-Path -LiteralPath $archive){VerifyMarker $archive}
PublishMarker $archive @()
if((Get-Service $actor).Status -ne 'Running'){Start-Service $actor}
return
}
@@ -142,7 +149,14 @@ if(Test-Path -LiteralPath $root){VerifyMarker $root}
if($Action -eq 'Plan'){[ordered]@{planned=$true;tenant_id=$binding.tenant_id;agent_id=$AgentId;asset_id=$AssetId;release_id=$ReleaseId;agent_sha256=$inputDto.agent_sha256;helper_sha256=$inputDto.helper_sha256;seconds=$Seconds;requires_helper_creation=(!$h);case_id=$CaseId}|ConvertTo-Json -Depth 6 -Compress;return}
try {
# The updater and agent hold the same file throughout a transition or execution.
$gate=[IO.File]::Open((Join-Path $data 'release-job.lock'),'Open','ReadWrite','None')
$until=[DateTime]::UtcNow.AddSeconds(60)
do {
try {$gate=[IO.File]::Open((Join-Path $data 'release-job.lock'),'Open','ReadWrite','None')}
catch [IO.IOException] {
if (($_.Exception.HResult -band 0xffff) -ne 32 -or $Action -ne 'Remove' -or [DateTime]::UtcNow -ge $until){throw}
Start-Sleep -Milliseconds 400
}
} while (!$gate)
Check ((JsonFile $active).release_id -ceq $ReleaseId.ToString()) 'Release switched during preparation.'
if($Action -eq 'Resume'){
Check (!(Service $helper) -and !(JsonFile $cfg).windows_update_helper -and !(Test-Path -LiteralPath $root)) 'Helper still active.'
@@ -160,7 +174,7 @@ try {
Check ((!$h) -or (Test-Path -LiteralPath $marker)) 'Existing helper service has no matching protected case.'
$actorSid=GetSid $actor
# The helper runs as SYSTEM; its service SID is optional on file DACLs.
NewPrivateDir $root @($actorSid);WriteFixed $marker $markerJson
PublishMarker $root @($actorSid)
Check (!(Test-Path -LiteralPath (Join-Path $data 'release-state\release-transition.json'))) 'Release transition must finish before maintenance.'
Set-Service 'RmmDiscoveryReleasePull' -StartupType Manual
if((Get-Service 'RmmDiscoveryReleasePull').Status -ne 'Stopped'){Stop-Service 'RmmDiscoveryReleasePull'}