结合get-aduser和get-qadmemberof

时间:2016-12-15 19:33:12

标签: powershell

我正在尝试将这两个条目组合在一起...... get-aduser XXXX |选择名称,给定名称,姓氏 和 get-qadmemberof -identity XXXX |选择名称|排序名称 他们都工作正常,但我想要结合,所以我的结果将显示用户的名字,并在下面显示用户所在的组。

2 个答案:

答案 0 :(得分:0)

Get-Aduser XXXX | Get-ADPrincipalGroupMembership | select name

这是如何使用Microsoft Native AD模块获取“XXXX”的组名。

答案 1 :(得分:0)

可以通过以下几种方式实现。

方式1:

#Output will resemble "CN=GroupName,OU=Groups,OU=Domain,OU=Local" Not always ideal output
Get-ADUser -Identity "TestUser" -Properties -ExpandProperty MemberOf

方式2:凯文提到

#Output will resemble more what you are looking for
Get-ADUser -Identity "TestUser" | Get-ADPrincipalGroupMembership | Select-Obeject -ExpandProperty Name

方式3:完整脚本

#This will create a csv titled with the Users sAMAccountName from the .txt file, 
#within that csv is a list of the users groups.
#To improve on this script you could use PSObjects to make this more efficent
#Don't Want to give you all the answers ;)

Import-Module ActiveDirectory
$UserList = Get-Content C:\Temp\UserList.txt
ForEach($User in $UserList){
  $UserMembership = Get-ADUser $User | Get-ADPrincipalGroupMembership | Select-Object -Property Name
  $UserMembership | Export-CSV "C:\Temp\$User.CSV" -NoTypeInformation
}