Admin.psm1

[CmdletBinding()]
param()

if ($PSVersionTable.PSVersion -lt '6.0') {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute(
        'PSAvoidAssignmentToAutomaticVariable', '', Justification = 'Compatibility with PowerShell 6.0 and newer.'
    )]
    $IsWindows = [System.Environment]::OSVersion.Platform -eq 'Win32NT'
}

$scriptName = 'Admin'
Write-Verbose "[$scriptName] - Importing module"

#region - From [public]
Write-Verbose "[$scriptName] - [public] - Processing folder"

#region - From [public] - [Test-Admin]
Write-Verbose "[$scriptName] - [public] - [Test-Admin] - Importing"

function Test-Admin {
    <#
        .SYNOPSIS
        Test if the current context is running as a specified role.

        .EXAMPLE
        Test-Role

        Test if the current context is running as an Administrator.
    #>

    [OutputType([System.Boolean])]
    [CmdletBinding()]
    [Alias('Test-Administrator', 'IsAdmin', 'IsAdministrator')]
    param()

    $IsUnix = $PSVersionTable.Platform -eq 'Unix'
    if ($IsUnix) {
        Write-Verbose "Running on Unix, checking if user is root."
        $whoAmI = $(whoami)
        Write-Verbose "whoami: $whoAmI"
        $IsRoot = $whoAmI -eq 'root'
        Write-Verbose "IsRoot: $IsRoot"
        $IsRoot
    } else {
        Write-Verbose "Running on Windows, checking if user is an Administrator."
        $user = [Security.Principal.WindowsIdentity]::GetCurrent()
        $principal = New-Object Security.Principal.WindowsPrincipal($user)
        $isAdmin = $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
        Write-Verbose "IsAdmin: $isAdmin"
        $isAdmin
    }
}

Write-Verbose "[$scriptName] - [public] - [Test-Admin] - Done"
#endregion - From [public] - [Test-Admin]

Write-Verbose "[$scriptName] - [public] - Done"
#endregion - From [public]


$exports = @{
    Alias    = '*'
    Cmdlet   = ''
    Function = 'Test-Admin'
    Variable = ''
}
Export-ModuleMember @exports