非提升时检查登录用户是否为管理员

Check if logged on user is an administrator when non-elevated

我需要检查当前登录的用户是否是管理员,但是发现当涉及到 AD 组是管理员组的成员时,仅使用 'net localgroup administrators' 是不够的。

[编辑:] 很容易将一般的管理员权限与特定实例的提升权限混淆,我只想强调这个 question/answer 不处理进程提升状态检查。要求通常是确定登录用户是否是管理员组成员。更常见的是需要确定您的脚本是否具有 运行 管理员权限。如果这是您的要求,请改为查看此处:Administrative privileges

在这种特殊情况下,有一个禁用管理共享的策略(排除了我以前使用 Windows XP 使用 \127.0.0 测试管理共享是否存在的技术。1\admin$ 判断当前用户是否为管理员)。 [/编辑]

下面是我收集并编写的代码,查看登录用户是否是管理员。

我希望这能帮助其他需要和我做同样事情的人。

如果有人能提供更优雅的解决方案,我们将不胜感激!

如前所述,本地 Administrators 组的成员身份不足以确定当前进程是否已提升。您可以像这样在 PowerShell 中测试提升:

$elevated = ([Security.Principal.WindowsPrincipal] `
 [Security.Principal.WindowsIdentity]::GetCurrent()
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

感谢比尔的提醒 - 抱歉,已经很晚了,我从圣诞节开始工作 7-days/wk。

Function IsCurrentUserAdmin( [String] $UserName )
# Returns true if current user in in the administrators group (directly or nested group) and false if not.
{
    $group = [ADSI] "WinNT://./Administrators,group" # 
    $members = @($group.psbase.Invoke("Members"))
    $AdminList = ($members | ForEach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)})
    If ($AdminList -contains $UserName) {
        Return $true
    } Else {
        # Adapted $LocalUsers from http://www.powertheshell.com/finding-local-user-accounts-in-powershell-3-0/
        $LocalUsers = net user | Select-Object -Skip 4 
        $LocalUsers = ($LocalUsers | Select-Object -First ($LocalUsers.Count - 2)).Trim()
        ForEach ($Item In $AdminList) {
            If (($LocalUsers.Contains($Item)) -eq $false) {
                # Lookup each AD group that is a member of the local administrators group and see if the current user is a member and return true if found
                If (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole($Item) -eq $true) { Return $true }
            }
        }
        Return $false
    }
}

# Check if logged on user is an administrators group member and quit this program if so (to enable admins to manually install long-running software etc without logoff risk / disruption)

$UserName = ${Env:UserName}
[Bool] $AdminTest = IsCurrentUserAdmin $UserName
If ($AdminTest -eq $True) { 
    # Do something
} Else {
    # Do something else
}

如果您想确定当前用户是否是本地管理员组的成员(即使没有提升权限),这里有一些选项。

$null -ne (whoami /groups /fo csv |
  ConvertFrom-Csv |
  Where-Object { $_.SID -eq "S-1-5-32-544" })

您还可以使用 isadmin.exe (https://westmesatech.com/?page_id=23) 并检查退出代码 2(管理员成员,但未启用,因此未提升)。

使用 SID:

([Security.Principal.WindowsIdentity]::GetCurrent().Groups | Select-String 'S-1-5-32-544')

或使用 "Well-known" 安全标识符名称:

([Security.Principal.WindowsIdentity]::GetCurrent().Groups.IsWellKnown('BuiltinAdministratorsSid') -eq $true)

如果您想获取所有 SID 及其名称,请查看此页面:https://support.microsoft.com/en-us/help/243330/well-known-security-identifiers-in-windows-operating-systems

通过在 whoami /all

中寻找 BUILTIN\Administrators
$output = whoami /all
$IsAdministrator = $false

foreach($line in $output) {
  if ($line -like "*BUILTIN\Administrators*") {
      $IsAdministrator = $true
      break;
   } 
} 

if ($IsAdministrator)
{
    Write-Host "The Computer contains Adminstrator priveledges" -ForegroundColor Black -BackgroundColor Green
} else {
    Write-Host "The Computer does not have Adminstrator priveledges" -ForegroundColor -BackgroundColor Red
}