网络上的许多示例显示了使用以下方法检查当前用户是否具有管理权限的方法
[Security.Principal.WindowsPrincipal]
[Security.Principal.WindowsIdentity]::GetCurrent()
在特定服务器上运行命令时,是否有类似的方法来检查不是“当前”身份,而是检查任何(本地或域,例如,从 Get-ACL
cmdlet 检索)。
我检查了https://learn.microsoft.com/en-us/dotnet/api/system.security.principal.windowsidentity.-ctor?view=netframework-4.7.2#System_Security_Principal_WindowsIdentity__ctor_System_String_ ,但找不到实现它的方法(仅当您使用带有 UPN 参数的构造函数时,这不适合我的情况)。如果有任何提示,我将不胜感激。
最佳答案
您可以尝试以下功能,对于给定的用户名:
- 尝试在与调用用户(域与本地)相同的上下文中找到底层身份(NT 用户帐户);用户名可以用多种格式指定,其中包括 NTLM 格式 (
<domain>\<username>
)。 - 然后在内置的 local
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'
}
关于windows - 有没有办法使用 PowerShell 检查本地服务器上任意安全主体的管理权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53912463/