$ErrorActionPreference = 'Stop' Set-StrictMode -Version Latest function Get-Image([string]$Id) { $image = Join-Path ([Environment]::GetFolderPath('ProgramFiles')) ('RustDiscovery\releases\' + $Id + '\rmm-agent-windows.exe') return '"' + $image + '" --service --config "' + $configPath + '"' } $configPath = Join-Path ([Environment]::GetFolderPath('CommonApplicationData')) 'RustDiscovery\config.json' $from = Get-Image 'FROM_UUID' $to = Get-Image 'TO_UUID' $service = Get-CimInstance -ClassName Win32_Service -Filter "Name='RmmDiscoveryWindows'" $config = Get-Content -LiteralPath $configPath -Raw | ConvertFrom-Json if ($config.PSObject.Properties['enable_jobs'] -and $config.enable_jobs) { throw 'Job-enabled agent requires a coordinated handoff.' } 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.' } } $control = Get-Service 'RmmDiscoveryWindows' if ($control.Status -ne 'Running') { $control.Start() $control.WaitForStatus('Running',[TimeSpan]::FromSeconds(45)) }