网络上的许多示例显示了使用以下方法检查当前用户的管理权限的方法
[Security.Principal.WindowsPrincipal]
[Security.Principal.WindowsIdentity]::GetCurrent()
在特定服务器上运行命令时,是否存在类似的方法来检查“当前”身份,而不是任何(例如,从Get-ACL
cmdlet检索到的本地或域)。
我检查了https://docs.microsoft.com/en-us/dotnet/api/system.security.principal.windowsidentity.-ctor?view=netframework-4.7.2#System_Security_Principal_WindowsIdentity__ctor_System_String_,但找不到解决方法(仅当您将构造函数与UPN参数一起使用时,这对我而言不适合)。我将不胜感激。
答案 0 :(得分:2)
您可以尝试使用以下功能,针对给定的用户名:
<domain>\<username>
)。Administrators
组中(静态)成员身份的身份。function Test-LocalAdminGroupMembership {
param([string] $user)
# Load the required assembly (a no-op if already loaded).
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
# Obtain the specified user as a UserPrincipal instance.
$up = try {
if (-not $user) { # default to current user
[System.DirectoryServices.AccountManagement.UserPrincipal]::Current
} else {
[System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity(
[System.DirectoryServices.AccountManagement.UserPrincipal]::Current.Context,
$user
)
}
} catch {
Throw
}
# See if the well-known SID of the local Administrators group
# is among the SIDs of the groups that the user is a member of (PSv3+ syntax).
$up.GetGroups().SID.Value -contains 'S-1-5-32-544'
}