我需要检查当前登录的用户是否是管理员,但是发现只使用了“本地网络管理员”#39;当AD组成为管理员组的成员时,这是不够的。
[编辑:]很容易将管理员权限与特定实例的提升权限混淆,我只想强调这个问题/答案不涉及进程提升状态检查。要求是通常确定登录用户是否是管理员组成员。更常见的是需要确定您的脚本是否以管理员权限运行。如果这是您所需要的,请在此处查看:Administrative privileges
在这种特殊情况下,有一个策略禁用管理员共享(排除我之前使用Windows XP测试管理共享存在的技术,使用\ 127.0.0.1 \ admin $来确定当前用户是管理员)。 [/编辑]
下面是我收集并编写的代码,看看登录的用户是否是管理员。
我希望这可以帮助那些需要我做同样事情的人。
如果有人能提供更优雅的解决方案,我们将不胜感激!
答案 0 :(得分:6)
如上所述,本地Administrators
组的成员资格不足以确定当前进程是否提升。您可以在PowerShell中测试高程,如下所示:
$elevated = ([Security.Principal.WindowsPrincipal] `
[Security.Principal.WindowsIdentity]::GetCurrent()
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
答案 1 :(得分:3)
如果要确定当前用户是否是本地Administrators组的成员(即使未提升),可以选择以下选项。
whoami /groups /fo csv | convertfrom-csv | where-object { $_.SID -eq "S-1-5-32-544" }
您还可以使用isadmin.exe(http://www.westmesatech.com/wast.html)并检查退出代码为2(管理员成员,但未启用,因此未提升)。
答案 2 :(得分:1)
通过在 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
}
答案 3 :(得分:0)
使用SID:
([Security.Principal.WindowsIdentity]::GetCurrent().Groups | Select-String 'S-1-5-32-544')
或者使用"众所周知的"安全标识符名称:
([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
答案 4 :(得分:-1)
感谢单挑的比尔 - 道歉,现在已经很晚了,我自圣诞节以来已经工作了7天/周。
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" # http://stackoverflow.com/questions/16617307/check-if-an-account-is-a-member-of-a-local-group-and-perform-an-if-else-in-power
$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
}