windows - 有没有办法使用 PowerShell 检查本地服务器上任意安全主体的管理权限?

标签 windows powershell privileges

网络上的许多示例显示了使用以下方法检查当前用户是否具有管理权限的方法

[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/

相关文章:

c - 在 Windows 上用 C 搜索文件

通过 jmeter/BSF 采样器/beanshell 运行时 powershell/powershell 挂起

powershell - 显示目录中文件的哈希值

windows - 如何为 Windows 构建或获取原始最新版本的 GCC?

python - 如何在Win10/powershell上获取免费/开放的本地端口以进行ssh转发

windows - 在 Windows 中查看共享内存的程序?

PowerShell PipelineVariable 参数仅包含 PSCustomObject 集合中的第一个值

android-studio - Android Studio 中的 Elevator.exe 更新

c# - 有没有办法确定 Visual Studio C# 解决方案中的哪些函数需要管理员权限

mysql - Phpmyadmin 不显示 "privileges"选项卡