Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Test-Admin (private cmdlet) #88

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions SteamPS/Private/Server/Test-Admin.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
function Test-Admin {
<#
.SYNOPSIS
Checks whether the current user has administrator privileges.

.DESCRIPTION
This cmdlet verifies if the current user has administrator privileges on the system.

.EXAMPLE
PS C:\> Test-Admin
True
This example checks if the current user has administrator privileges and returns True if they do.

.NOTES
Author: Marius Storhaug <https://github.com/PSModule/Admin>
#>

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

begin {
Write-Verbose -Message "[BEGIN ] Starting: $($MyInvocation.MyCommand)"
}

process {
$IsUnix = $PSVersionTable.Platform -eq 'Unix'
if ($IsUnix) {
Write-Verbose -Message "Running on Unix, checking if user is root."
$whoAmI = $(whoami)
Write-Verbose -Message "whoami: $whoAmI"
$IsRoot = $whoAmI -eq 'root'
Write-Verbose -Message "IsRoot: $IsRoot"
$IsRoot
} else {
Write-Verbose -Message "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 -Message "IsAdmin: $isAdmin"
$isAdmin
}
}

end {
Write-Verbose -Message "[END ] Ending: $($MyInvocation.MyCommand)"
}
}
28 changes: 28 additions & 0 deletions Tests/Unit/Private/Test-Admin.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
BeforeAll {
. $SteamPSModulePath\Private\Server\Test-Admin.ps1
}

Describe 'Test-Admin Tests' {
Context 'Function: Test-Admin' {
It 'Should not throw' {
{ Test-Admin } | Should -Not -Throw
}

It 'Should return <Expected> for <OS> based runners' -ForEach @(
@{
Expected = if ($IsLinux -or $IsMacOS) {
$false
} else {
$true
}
OS = if ($IsLinux -or $IsMacOS) {
'Unix'
} else {
'Windows'
}
}
) {
Test-Admin | Should -Be $Expected
}
}
}