列出AD用户的组成员身份

Uwe*_*gen 4 powershell

使用以下Powershell代码段,我获取当前用户的组成员身份名称:

$groups = [System.Security.Principal.WindowsIdentity]::GetCurrent().Groups
foreach($i in $groups){
$i.Translate([System.Security.Principal.NTAccount]).value
}
Run Code Online (Sandbox Code Playgroud)

我该怎么修改这个我可以提供用户帐户名作为参数?

谢谢,

乌韦

jon*_*n Z 10

如果您有权访问ActiveDirectory模块,我建议您使用Get-ADUser.如果您不能使用该模块,您可以使用System.DirectoryServices.AccountManagement程序集:

Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$username = read-host -prompt "Enter a username"
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$user = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($ct, $username)
$groups = $user.GetGroups()
foreach($i in $groups){
  $i.SamAccountName
}
Run Code Online (Sandbox Code Playgroud)