Powershell: AD-Gruppen des aktuellen Users auslesen

Hier ein kurzer Code-Schnipsel, um die Gruppen des aktuellen Users auszugeben:

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

Um die Gruppen eines beliebigen Users auszulesen muss ein anderer Ansatz gewählt werden: (http://stackoverflow.com/questions/8009593/list-group-memberships-for-ad-users)

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
}

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website