functions/Test-AdminPrivilege.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function Test-AdminPrivilege {
    [CmdletBinding()]
    [OutputType('System.Boolean')]
    Param
    (
    )

    # Test Windows or Not
    if (Test-IsWindows) {
        # Check Administrator privilege
        $local:user =
        try {
            [Security.Principal.WindowsIdentity]::GetCurrent()
        }
        catch { }
        (New-Object -TypeName 'Security.Principal.WindowsPrincipal' -ArgumentList $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
    }
    else {
        # Except Windows, always return $true
        $true
    }
}