private/ElevationCheck.ps1
|
function Test-OctaElevation { <# Returns $true when the current process is running as Administrator. #> [CmdletBinding()] param() $identity = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = [Security.Principal.WindowsPrincipal]::new($identity) return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) } function Assert-OctaElevation { <# Declares & checks elevation upfront, before any dry-run/apply happens (Scope & Security Requirements: "Elevation requirements MUST be declared per category before execution, not requested silently mid-run"). Returns $null when no elevation is needed or the session is already elevated. Returns the list of categories that need elevation when it is missing, so the caller (bin/octa.ps1) can report them and exit 3 - this function never calls exit itself. #> [CmdletBinding()] param( [Parameter(Mandatory)] [object[]]$Categories ) $needElevation = @($Categories | Where-Object { $_.RequiresElevation }) if ($needElevation.Count -gt 0 -and -not (Test-OctaElevation)) { return $needElevation } return $null } |