microsoft/hve-core
Publicmirrored from https://github.com/microsoft/hve-coreAvailable
.github/skills/installer/hve-core-installer/scripts/file-status-check.ps1
65lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # SPDX-License-Identifier: MIT |
| 3 | <# |
| 4 | .SYNOPSIS |
| 5 | Compares current agent files against the .hve-tracking.json manifest. |
| 6 | .DESCRIPTION |
| 7 | For each tracked file, computes the current SHA256 hash and compares it |
| 8 | against the stored hash to determine status: managed, modified, ejected, |
| 9 | or missing. |
| 10 | .EXAMPLE |
| 11 | ./scripts/file-status-check.ps1 |
| 12 | .OUTPUTS |
| 13 | Per-file status lines: FILE=<path>|STATUS=<status>|ACTION=<action>. |
| 14 | #> |
| 15 | [CmdletBinding()] |
| 16 | param() |
| 17 | |
| 18 | $ErrorActionPreference = 'Stop' |
| 19 | |
| 20 | $manifest = Get-Content ".hve-tracking.json" | ConvertFrom-Json -AsHashtable |
| 21 | $statusReport = @() |
| 22 | |
| 23 | foreach ($file in $manifest.files.Keys) { |
| 24 | $entry = $manifest.files[$file] |
| 25 | $status = $entry.status |
| 26 | |
| 27 | if ($status -eq "ejected") { |
| 28 | $statusReport += @{ |
| 29 | file = $file |
| 30 | status = "ejected" |
| 31 | action = "Skip (user owns this file)" |
| 32 | } |
| 33 | continue |
| 34 | } |
| 35 | |
| 36 | if (-not (Test-Path $file)) { |
| 37 | $statusReport += @{ |
| 38 | file = $file |
| 39 | status = "missing" |
| 40 | action = "Will restore" |
| 41 | } |
| 42 | continue |
| 43 | } |
| 44 | |
| 45 | $currentHash = (Get-FileHash -Path $file -Algorithm SHA256).Hash.ToLower() |
| 46 | if ($currentHash -ne $entry.sha256) { |
| 47 | $statusReport += @{ |
| 48 | file = $file |
| 49 | status = "modified" |
| 50 | action = "Requires decision" |
| 51 | currentHash = $currentHash |
| 52 | storedHash = $entry.sha256 |
| 53 | } |
| 54 | } else { |
| 55 | $statusReport += @{ |
| 56 | file = $file |
| 57 | status = "managed" |
| 58 | action = "Will update" |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | $statusReport | ForEach-Object { |
| 64 | Write-Host "FILE=$($_.file)|STATUS=$($_.status)|ACTION=$($_.action)" |
| 65 | } |
| 66 | |