Public/Test-ALHIsAdmin.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
<#PSScriptInfo
.VERSION 1.0.0 .GUID 8a3d22fe-43e0-401d-93c3-7925b5bc49bd .AUTHOR Dieter Koch .COMPANYNAME .COPYRIGHT (c) 2021-2023 Dieter Koch .TAGS .LICENSEURI https://github.com/admins-little-helper/ALH/blob/main/LICENSE .PROJECTURI https://github.com/admins-little-helper/ALH .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES 1.0.0 Initial release #> <# .DESCRIPTION Contains a function to check if PowerShell runs with elevated permissions. .LINK https://github.com/admins-little-helper/ALH #> function Test-ALHIsAdmin { <# .SYNOPSIS Check if PowerShell runs with elevated permissions. .DESCRIPTION Check if PowerShell runs with elevated permissions. Also supports .NET CORE on Linux/macOS. .EXAMPLE Test-ALHIsAdmin Returns $true or $false. .INPUTS Nothing .OUTPUTS System.Boolean .NOTES Author: Dieter Koch Email: diko@admins-little-helper.de .LINK https://github.com/admins-little-helper/ALH/blob/main/Help/Test-ALHIsAdmin.txt #> [OutputType([bool])] [CmdletBinding()] param () if ((($PSVersionTable.PSEdition -eq 'Core') -and ($PSVersionTable.Platform -eq 'Win32NT')) -or ($PSVersionTable.PSEdition -eq 'Desktop') ) { Write-Verbose -Message "Running on Windows." return ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator') } elseif (($PSVersionTable.PSEdition -eq 'Core') -and ($PSVersionTable.Platform -eq 'Unix')) { Write-Verbose -Message "Running on Unix/Linux/macOS." return ((id -u) -eq 0) } else { Write-Warning -Message 'Unknown PowerShell Plattform (OS) and/or PowerShell Edition.' return } } #region EndOfScript <# ################################################################################ ################################################################################ # # ______ _ __ _____ _ _ # | ____| | | / _| / ____| (_) | | # | |__ _ __ __| | ___ | |_ | (___ ___ _ __ _ _ __ | |_ # | __| | '_ \ / _` | / _ \| _| \___ \ / __| '__| | '_ \| __| # | |____| | | | (_| | | (_) | | ____) | (__| | | | |_) | |_ # |______|_| |_|\__,_| \___/|_| |_____/ \___|_| |_| .__/ \__| # | | # |_| ################################################################################ ################################################################################ # created with help of http://patorjk.com/software/taag/ #> #endregion |