feat(compare): offer a drift check after the full tenant export
After menu item 23 finishes it offers to compare with the newest earlier export of the same tenant (assignments included) and writes the report to <root>/_drift/<old>-to-<new>/. Compare-Tenants.py now also accepts a folder that holds a single export folder (e.g. a dated folder containing Exports/).
This commit is contained in:
@@ -79,6 +79,20 @@ def clean_name(raw: str, strip: str) -> str:
|
||||
return re.sub(strip, "", raw, count=1, flags=re.IGNORECASE).strip() if strip else raw
|
||||
|
||||
|
||||
def resolve_root(root: Path) -> Path:
|
||||
"""Accept either the export root or its parent (e.g. a dated folder holding a single 'Export' folder)."""
|
||||
def has_categories(p: Path) -> bool:
|
||||
return any(d.is_dir() and any(d.rglob("*.json")) for d in p.iterdir())
|
||||
if not root.is_dir() or any(f.suffix == ".json" for f in root.glob("MigrationTable.json")) or \
|
||||
any(d.name in LOOKUP_CATEGORIES or d.name == "SettingsCatalog" for d in root.iterdir() if d.is_dir()):
|
||||
return root
|
||||
inner = [d for d in root.iterdir() if d.is_dir() and has_categories(d)]
|
||||
if len(inner) == 1:
|
||||
print(f"Using {inner[0]} as export root")
|
||||
return inner[0]
|
||||
return root
|
||||
|
||||
|
||||
def load_json(path: Path) -> Any:
|
||||
try:
|
||||
return json.loads(path.read_text(encoding="utf-8-sig"))
|
||||
@@ -251,6 +265,7 @@ def main() -> None:
|
||||
for r in (args.root_a, args.root_b):
|
||||
if not r.is_dir():
|
||||
raise SystemExit(f"Not a folder: {r}")
|
||||
args.root_a, args.root_b = resolve_root(args.root_a), resolve_root(args.root_b)
|
||||
ta = Tenant(args.root_a, args.strip_a, args.label_a)
|
||||
tb = Tenant(args.root_b, args.strip_b, args.label_b)
|
||||
cats = sorted(ta.categories() & tb.categories())
|
||||
|
||||
+25
-1
@@ -537,7 +537,31 @@ while($true)
|
||||
if($SettingsFile) { $exportParams.SettingsFile = $SettingsFile }
|
||||
& (Join-Path $projectRoot "Scripts/Start-HeadlessIntune.ps1") @exportParams
|
||||
Write-Host "`nExport finished: $exportPath" -ForegroundColor Cyan
|
||||
Write-Host "Tip: compare with an earlier export of this tenant using menu item 22 (leave the prefix blank to see drift)." -ForegroundColor DarkGray
|
||||
|
||||
# Drift check against the newest earlier export of this tenant in the same root
|
||||
$python = Get-Command python3 -ErrorAction SilentlyContinue
|
||||
if(-not $python) { $python = Get-Command python -ErrorAction SilentlyContinue }
|
||||
$prev = Get-ChildItem -LiteralPath $root -Directory -ErrorAction SilentlyContinue |
|
||||
Where-Object { $_.Name -like "$safeName-*" -and $_.FullName -ne $exportPath } |
|
||||
Sort-Object LastWriteTime -Descending | Select-Object -First 1
|
||||
if($python -and (Test-Path -LiteralPath $exportPath -PathType Container))
|
||||
{
|
||||
$prevPath = if($prev) { $prev.FullName } else { $null }
|
||||
$hint = if($prevPath) { "Enter = $($prev.Name)" } else { "path to an earlier export, Enter to skip" }
|
||||
$ans = Read-Host "`nCompare with an earlier export to see drift? ($hint, n = skip)"
|
||||
if($ans -like 'n') { $prevPath = $null }
|
||||
elseif(-not [string]::IsNullOrWhiteSpace($ans)) { $prevPath = [IO.Path]::GetFullPath(($ans.Trim().Trim('"',"'") -replace '^~', $HOME)) }
|
||||
if($prevPath -and (Test-Path -LiteralPath $prevPath -PathType Container))
|
||||
{
|
||||
$prevName = Split-Path $prevPath -Leaf
|
||||
$curName = Split-Path $exportPath -Leaf
|
||||
$driftDir = Join-Path (Join-Path $root "_drift") "$prevName-to-$curName"
|
||||
& $python.Source (Join-Path $projectRoot "Scripts/Compare-Tenants.py") `
|
||||
--root-a $prevPath --label-a $prevName --root-b $exportPath --label-b $curName `
|
||||
--include-assignments --output-dir $driftDir
|
||||
Write-Host "Drift report: $(Join-Path $driftDir 'tenant-comparison.md')" -ForegroundColor Cyan
|
||||
}
|
||||
}
|
||||
Write-Host "`nPress any key to return to the menu..." -ForegroundColor DarkGray
|
||||
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user