Admin.psm1
[CmdletBinding()] param() $baseName = [System.IO.Path]::GetFileNameWithoutExtension($PSCommandPath) $script:PSModuleInfo = Test-ModuleManifest -Path "$PSScriptRoot\$baseName.psd1" $script:PSModuleInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Debug $_ } $scriptName = $script:PSModuleInfo.Name Write-Debug "[$scriptName] - Importing module" #region [classes] - [public] Write-Debug "[$scriptName] - [classes] - [public] - Processing folder" #region [classes] - [public] - [Scope] Write-Debug "[$scriptName] - [classes] - [public] - [Scope] - Importing" enum Scope { CurrentUser AllUsers } Write-Debug "[$scriptName] - [classes] - [public] - [Scope] - Done" #endregion [classes] - [public] - [Scope] Write-Debug "[$scriptName] - [classes] - [public] - Done" #endregion [classes] - [public] #region [functions] - [public] Write-Debug "[$scriptName] - [functions] - [public] - Processing folder" #region [functions] - [public] - [Test-Admin] Write-Debug "[$scriptName] - [functions] - [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. .LINK https://psmodule.io/Admin/Functions/Test-Admin/ #> [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-Debug "[$scriptName] - [functions] - [public] - [Test-Admin] - Done" #endregion [functions] - [public] - [Test-Admin] Write-Debug "[$scriptName] - [functions] - [public] - Done" #endregion [functions] - [public] #region Class exporter # Get the internal TypeAccelerators class to use its static methods. $TypeAcceleratorsClass = [psobject].Assembly.GetType( 'System.Management.Automation.TypeAccelerators' ) # Ensure none of the types would clobber an existing type accelerator. # If a type accelerator with the same name exists, throw an exception. $ExistingTypeAccelerators = $TypeAcceleratorsClass::Get # Define the types to export with type accelerators. $ExportableEnums = @( [Scope] ) $ExportableEnums | Foreach-Object { Write-Verbose "Exporting enum '$($_.FullName)'." } foreach ($Type in $ExportableEnums) { if ($Type.FullName -in $ExistingTypeAccelerators.Keys) { Write-Verbose "Enum already exists [$($Type.FullName)]. Skipping." } else { Write-Verbose "Importing enum '$Type'." $TypeAcceleratorsClass::Add($Type.FullName, $Type) } } $ExportableClasses = @( ) $ExportableClasses | Foreach-Object { Write-Verbose "Exporting class '$($_.FullName)'." } foreach ($Type in $ExportableClasses) { if ($Type.FullName -in $ExistingTypeAccelerators.Keys) { Write-Verbose "Class already exists [$($Type.FullName)]. Skipping." } else { Write-Verbose "Importing class '$Type'." $TypeAcceleratorsClass::Add($Type.FullName, $Type) } } # Remove type accelerators when the module is removed. $MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = { foreach ($Type in ($ExportableEnums + $ExportableClasses)) { $TypeAcceleratorsClass::Remove($Type.FullName) } }.GetNewClosure() #endregion Class exporter #region Member exporter $exports = @{ Alias = '*' Cmdlet = '' Function = 'Test-Admin' } Export-ModuleMember @exports #endregion Member exporter |