69 lines
4.7 KiB
PowerShell
69 lines
4.7 KiB
PowerShell
# Inspect the enrolled test guest, its actual executables and public helper metadata.
|
|
# No private identity fields, service changes, job submission or update calls.
|
|
[CmdletBinding()]
|
|
param([bool]$ClockOnly=$false)
|
|
$ErrorActionPreference = 'Stop'
|
|
function JsonFile([string]$Path) {
|
|
if (Test-Path -LiteralPath $Path -PathType Leaf) {
|
|
return Get-Content -Raw -LiteralPath $Path | ConvertFrom-Json
|
|
}
|
|
}
|
|
function ImageHash([string]$Path) {
|
|
if ($Path -and (Test-Path -LiteralPath $Path -PathType Leaf)) {
|
|
return (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToLowerInvariant()
|
|
}
|
|
}
|
|
function ReadService($Name) {
|
|
$s = Get-CimInstance Win32_Service -Filter "Name='$Name'"
|
|
if (!$s) { return $null }
|
|
$match = [regex]::Match($s.PathName, '^\s*(?:"([^"]+)"|(.+?\.exe)(?:\s|$))')
|
|
$image = $match.Groups[1].Value
|
|
if (!$image) { $image = $match.Groups[2].Value }
|
|
$processImage=$null
|
|
if ($s.ProcessId -gt 0) {$processImage=(Get-CimInstance Win32_Process -Filter ('ProcessId='+$s.ProcessId)).ExecutablePath}
|
|
return [ordered]@{pid=$s.ProcessId;process_image=$processImage;process_sha256=(ImageHash $processImage);name=$s.Name; state=$s.State; identity=$s.StartName; image=$image; sha256=(ImageHash $image)}
|
|
}
|
|
$data = Join-Path $env:ProgramData 'RustDiscovery'
|
|
$helperData = Join-Path $env:ProgramData 'RustDiscoverySystem'
|
|
$probe=JsonFile (Join-Path $data 'state\helper-probe.json')
|
|
$timeSample=JsonFile (Join-Path $data 'state\helper-time-sample.json')
|
|
$refresh=JsonFile (Join-Path $data 'state\time-refresh-system_helper.json')
|
|
$lastError=@([regex]::Matches([string]$refresh.error,'(?:TIME|HELPER)_[A-Z_]{2,64}')|ForEach-Object {$_.Value})
|
|
$validated=[ordered]@{observed_at=$probe.observed_at;authenticated=$probe.authenticated_pipe_exchange;helper_ready=$probe.helper_ready;
|
|
agent_pid=$probe.agent_process_id;agent_id=$probe.agent_id;helper_sha256=$probe.helper_binary_sha256;policy_sha256=$probe.policy_sha256;clock_qualified=$probe.helper_clock_qualified;advertised=$probe.system_capability_advertised;diagnostic=$probe.diagnostic.code;
|
|
clock_verified_at=$timeSample.observed_at;clock_role=$timeSample.role;last_clock_error=$lastError;clock_refresh_at=$refresh.observed_at_utc}
|
|
|
|
if($ClockOnly) {
|
|
$service=Get-CimInstance Win32_Service -Filter "Name='RmmDiscoveryWindows'"
|
|
$validated['current_agent_pid']=$service.ProcessId
|
|
$validated|ConvertTo-Json -Compress
|
|
return
|
|
}
|
|
$identity = JsonFile (Join-Path $data 'state\identity.json')
|
|
$release = JsonFile (Join-Path $data 'release-state\installed-release.json')
|
|
if (!$release) {$release=JsonFile (Join-Path $data 'installed-release.json')}
|
|
$conf=JsonFile (Join-Path $data 'config.json')
|
|
$agent = ReadService 'RmmDiscoveryWindows'
|
|
$helper = ReadService 'RmmDiscoverySystemHelper'
|
|
$settings = JsonFile (Join-Path $helperData 'helper.json')
|
|
$pin = JsonFile (Join-Path $helperData 'agent-pin.json')
|
|
$clock = JsonFile (Join-Path $data 'job-time.json')
|
|
$case=JsonFile (Join-Path $helperData 'windows-update-enrollment.json')
|
|
$prerequisites = @()
|
|
if (!$identity.agent_id -or !$identity.asset_id) { $prerequisites += 'enrolled_identity' }
|
|
if (!$agent -or !$agent.sha256) { $prerequisites += 'agent_image' }
|
|
if (!$helper -or !$helper.sha256) { $prerequisites += 'system_helper_service' }
|
|
if (!$settings -or !$identity -or $settings.binding.agent_id -ne $identity.agent_id -or
|
|
$settings.binding.asset_id -ne $identity.asset_id -or $settings.agent.binary_sha256 -ne $agent.sha256 -or
|
|
$settings.helper.binary_sha256 -ne $helper.sha256) { $prerequisites += 'matching_settings_and_images' }
|
|
if (!$pin -or $pin.binding.agent_id -ne $identity.agent_id -or $pin.agent.binary_sha256 -ne $agent.sha256 -or
|
|
$pin.helper.binary_sha256 -ne $helper.sha256) { $prerequisites += 'matching_agent_pin' }
|
|
if (!$clock -or $clock.tenant_id -ne $release.tenant_id -or !$conf.localservice_time_policy) { $prerequisites += 'enrolled_signed_time_policy' }
|
|
if (!$conf.windows_update_helper) {$prerequisites += 'agent_windows_update_opt_in'}
|
|
if (!$settings.windows_update_max_runtime_seconds -or $settings.allowed_script_profiles -or $settings.allow_reboot -or $settings.allowed_services -or $settings.allowed_msi_recipe_sha256) {$prerequisites += 'narrow_update_helper_settings'}
|
|
[ordered]@{probe=$validated;case_id=$case.case_id;tenant_id=$release.tenant_id;release_pull=(ReadService 'RmmDiscoveryReleasePull');agent_id=$identity.agent_id;asset_id=$identity.asset_id;release_id=$release.release_id;
|
|
agent=$agent;helper=$helper;helper_settings_binding=$settings.binding.agent_id;
|
|
existing_helper_policy_sha256=$pin.policy_sha256;
|
|
windows_update_max_runtime_seconds=$settings.windows_update_max_runtime_seconds;
|
|
missing=$prerequisites} | ConvertTo-Json -Depth 4 -Compress
|