Modules/businessdev.ALbuild.Core/Private/Test-ALbuildElevated.ps1
|
function Test-ALbuildElevated { <# .SYNOPSIS Tells whether the current session can write machine-wide state. .DESCRIPTION Internal helper. On Windows this is local-administrator membership, which is what writing under 'C:\ProgramData' takes. Its own function rather than an inline check so callers can fail with a useful message BEFORE touching the file system - an UnauthorizedAccessException on a path does not tell an operator that the problem is scope - and so the surrounding logic stays testable without needing an elevated test run. Non-Windows has no equivalent notion here; write permission is decided by the file system and the attempt itself reports it, so this returns $true and lets the write speak. #> [CmdletBinding()] [OutputType([bool])] param() if ([System.Environment]::OSVersion.Platform -ne [System.PlatformID]::Win32NT) { return $true } try { $identity = [System.Security.Principal.WindowsIdentity]::GetCurrent() return ([System.Security.Principal.WindowsPrincipal] $identity).IsInRole( [System.Security.Principal.WindowsBuiltInRole]::Administrator) } catch { # Never let a diagnostic check decide the outcome: if membership cannot be determined, let the # write attempt be the authority. Write-Verbose "ALbuild: could not determine elevation ($($_.Exception.Message)); assuming allowed." return $true } } |