我有一个命令可以显示用户的 AD 组成员身份:
Get-ADPrincipalGroupMembership username | select name
但是我必须为每个用户输入每个用户名。
我想知道是否有人可以帮助我编写脚本,在其中我将提供包含用户名列表的 CSV 并从文件中读取每个用户并将用户所属的 AD 组输出到 CSV 文件。
更新1
我想到的人(我知道这不是最好的):
$users = Get-Content -path 'C:\temp\disabledadusersnameaudit.txt'
foreach($user in $users){
write-host "Group Membership for: " $user
Get-ADPrincipalGroupMembership -Identity $user | Select name | ft -hidetableheaders
}
但是收到此错误消息:
Get-ADPrincipalGroupMembership : An unspecified error has occurred
At line:4 char:1
+ Get-ADPrincipalGroupMembership -Identity $user | Select name | ft -hidetablehead ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (SSmall:ADPrincipal) [Get-ADPrincipalGroupMembership], ADException
+ FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.GetADPrincipalGroupMembership
更新2
设法从之前的答案中找到脚本:
如何使用Powershell脚本从AD获取多个用户成员资格组?
$userlist = Get-Content 'C:\temp\disabledadusersnameaudit.txt'
Get-ADUser -Filter '*' -Properties memberof | Where-Object {
$userlist -contains $_.SamAccountName
} | ForEach-Object {
$username = $_
$groups = $_ | Select-Object -Expand memberof |
ForEach-Object { (Get-ADGroup $_).Name }
"{0}: {1}" -f $username, ($groups -join ', ')
} | Out-File 'c:\temp\Audit.csv'
这可行,但这里有 2 个问题,审核 CSV 如下所示:
所有 AD 组都在一列中,需要通过 分隔它们,我猜是分隔符?
另一个问题是我想说明在审核中也找不到的用户名。
更新3
@Santiago 我运行了你的脚本并通过 PS 屏幕得到了这个:
WARNING: Cannot find an object with identity: 'Username1' under: 'DC=my,DC=domain,DC=net'.
WARNING: The search filter cannot be recognized
WARNING: Cannot find an object with identity: 'Username2' under: 'DC=my,DC=domain,DC=net'.
WARNING: Cannot find an object with identity: 'Username3' under: 'DC=my,DC=domain,DC=net'.
WARNING: The search filter cannot be recognized
标识它无法找到的用户,但无法为它可以找到的用户工作,状态为“警告:无法识别搜索过滤器”
更新4
现在正在进步。
User Membership
---- ----------
Username1
WARNING: Cannot find an object with identity: 'Username2' under: 'DC=my,DC=domain,DC=net'.
WARNING: Cannot find an object with identity: 'Username3' under: 'DC=my,DC=domain,DC=net'.
WARNING: Cannot find an object with identity: 'Username4' under: 'DC=my,DC=domain,DC=net'.
Username5
它可以找到用户名但不显示 AD 组?显示为空白
更新5
到达那里:
User Membership
---- ----------
Username1 AD GroupName1, AD GroupName2, AD GroupName3, AD GroupName4, AD Group...
WARNING: Cannot find an object with identity: 'Username2' under: 'DC=my,DC=domain,DC=net'.
WARNING: Cannot find an object with identity: 'Username3' under: 'DC=my,DC=domain,DC=net'.
WARNING: Cannot find an object with identity: 'Username4' under: 'DC=my,DC=domain,DC=net'.
Username5 AD GroupName1, AD GroupName2, AD GroupName3, AD GroupName4
当用户有很多 AD 组时,它不会显示...(3 个句号/句点)上的所有组,我想这是由于它可以输出的字符量所致 - 2 个问题改善。
首先,当输出到 CSV 时,它是否仍会显示为...或者是否会具有用户所属的完整 AD 组?
第二种是找不到用户时,而不是:
警告:无法在“DC=my,DC=domain,DC=net”下找到身份为“Username2”的对象。
它是否会回显“$User 不在域中!”因为这样会好得多。
再次感谢圣地亚哥的帮助!
我没有对您在问题中显示的错误的解释,但也知道
Get-ADPrincipalGroupMembership
有问题。我建议首先查询用户以获取他们的 DistinguishedName
,然后您可以查询将该用户作为 member
的所有组:
Get-Content -Path 'C:\temp\disabledadusersnameaudit.txt' | ForEach-Object {
try {
$user = Get-ADUser $_
$membership = Get-ADGroup -LDAPFilter "(member=$($user.DistinguishedName))"
[pscustomobject]@{
User = $user.samAccountName
Membership = $membership.samAccountName -join ', '
}
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
[pscustomobject]@{
User = "'$($_.TargetObject)' could not be found in Domain."
Membership = $null
}
}
catch {
Write-Warning $_
}
} | Export-Csv .....path.csv -NoTypeInformation