Enable per-order privileged scripts on managed devices

This commit is contained in:
2026-11-18 00:02:17 +01:00
parent 2aa460e337
commit ea87d8141c
20 changed files with 864 additions and 157 deletions
@@ -4,6 +4,31 @@ function Get-Image([string]$Id) {
$image = Join-Path ([Environment]::GetFolderPath('ProgramFiles')) ('RustDiscovery\releases\' + $Id + '\rmm-agent-windows.exe')
return '"' + $image + '" --service --config "' + $configPath + '"'
}
function Get-HelperImage([string]$Id) {
Join-Path ([Environment]::GetFolderPath('ProgramFiles')) ('RustDiscovery\releases\' + $Id + '\rmm-helper-windows.exe')
}
function Wait-Stopped([string]$Name) {
$control = Get-Service $Name
if ($control.Status -ne 'Stopped') {
$control.Stop()
$control.WaitForStatus('Stopped', [TimeSpan]::FromSeconds(45))
}
}
function Select-Image($Service, [string]$Image) {
if ($Service.PathName -cne $Image) {
$result = Invoke-CimMethod -InputObject $Service -MethodName Change -Arguments @{PathName=$Image}
if ($result.ReturnValue -ne 0) { throw 'SCM path change failed.' }
}
}
function Replace-Pin([string]$Path, [string]$Value) {
$temp = "$Path.next-$([Guid]::NewGuid())"
try {
[IO.File]::WriteAllText($temp, $Value, [Text.UTF8Encoding]::new($false))
[IO.File]::Replace($temp, $Path, $null)
} finally {
if (Test-Path -LiteralPath $temp) { Remove-Item -LiteralPath $temp }
}
}
$configPath = Join-Path ([Environment]::GetFolderPath('CommonApplicationData')) 'RustDiscovery\config.json'
$from = Get-Image 'FROM_UUID'
$to = Get-Image 'TO_UUID'
@@ -13,16 +38,36 @@ if ($config.PSObject.Properties['enable_jobs'] -and $config.enable_jobs) {
if (-not $config.PSObject.Properties['localservice_time_policy'] -or
$config.localservice_time_policy -cne (Join-Path (Split-Path $configPath) 'job-time.json')) { throw 'Job-enabled pair requires a coordinated helper handoff.' }
}
$managed = $config.PSObject.Properties['managed_script_helper'] -and $config.managed_script_helper
$helper = Get-CimInstance -ClassName Win32_Service -Filter "Name='RmmDiscoverySystemHelper'"
if (-not $service -or $service.StartName -ine 'NT AUTHORITY\LocalService' -or
$service.PathName -cnotin @($from,$to) -or (Get-Service 'RmmDiscoverySystemHelper' -ErrorAction SilentlyContinue)) { throw 'Unexpected service configuration.' }
if ($service.PathName -cne $to) {
$control = Get-Service 'RmmDiscoveryWindows'
if ($control.Status -ne 'Stopped') {
$control.Stop()
$control.WaitForStatus('Stopped', [TimeSpan]::FromSeconds(45))
}
$res = Invoke-CimMethod -InputObject $service -MethodName Change -Arguments @{PathName=$to}
if ($res.ReturnValue -ne 0) { throw 'SCM path change failed.' }
$service.PathName -cnotin @($from,$to) -or ([bool]$helper -ne [bool]$managed)) { throw 'Unexpected service configuration.' }
if ($managed) {
$helperExe = Get-HelperImage 'TO_UUID'
$helperTo = '"' + $helperExe + '" --service'
$helperFrom = '"' + (Get-HelperImage 'FROM_UUID') + '" --service'
if ($helper.StartName -ine 'LocalSystem' -or $helper.PathName -cnotin @($helperFrom,$helperTo)) { throw 'Unexpected managed helper service.' }
$policyRoot = Join-Path ([Environment]::GetFolderPath('CommonApplicationData')) 'RustDiscoverySystem'
$settingsPath = Join-Path $policyRoot 'helper.json'
$settings = Get-Content -LiteralPath $settingsPath -Raw | ConvertFrom-Json
if ($settings.schema_version -ne 5) { throw 'Managed helper settings required.' }
$rendered = ([ordered]@{binding=$settings.binding; release_id='TO_UUID'
agent_sha256='SELECTED_AGENT_SHA256'; helper_sha256='SELECTED_HELPER_SHA256'
max_runtime_seconds=$settings.windows_update_max_runtime_seconds; managed_scripts=$true} | ConvertTo-Json -Depth 6 -Compress) |
& $helperExe --prepare-enrolled-windows-update
if ($LASTEXITCODE -ne 0) { throw 'Signed successor helper could not render its pins.' }
$pins = $rendered | ConvertFrom-Json
Wait-Stopped 'RmmDiscoveryWindows'
Wait-Stopped 'RmmDiscoverySystemHelper'
Replace-Pin $settingsPath $pins.helper_settings
Replace-Pin (Join-Path $policyRoot 'agent-pin.json') $pins.agent_pin
Select-Image $helper $helperTo
Select-Image $service $to
Start-Service 'RmmDiscoverySystemHelper'
(Get-Service 'RmmDiscoverySystemHelper').WaitForStatus('Running', [TimeSpan]::FromSeconds(45))
} elseif ($service.PathName -cne $to) {
Wait-Stopped 'RmmDiscoveryWindows'
Select-Image $service $to
}
$control = Get-Service 'RmmDiscoveryWindows'
if ($control.Status -ne 'Running') {