Everything works except the user description. I’m guessing because the group and user description are the same attribute name. Not sure how to fix it.
Begin {
$data = @()
try{
Import-Module ActiveDirectory
}
catch{
Write-Warning "The Active Directory module was not found"
}
}#end begin
Process{
$Groups = Get-ADGroup -Filter * -SearchBase "OU=Infor,OU=COS Groups,DC=ad,DC=springfieldmo,DC=gov" -Properties name, groupcategory, groupscope,description
foreach( $Group in $Groups ){
Get-ADGroupMember -Identity $Group | Get-ADUser -Properties samaccountname, givenname,sn,title,description | foreach {
$data+= [pscustomobject]@{ObjectName =$_.name
objectclass =$_.objectclass
GroupName =$group.name
GroupCategory =$group.groupcategory
GroupScope =$group.groupscope
GroupDescription =$group.description
distinguishedName =$_.distinguishedName
UserName =$_.samaccountname
FirstName =$_.givenname
LastName =$_.sn
Title =$_.title
UserDescription =$_.Description
}
}#end foreach
}# end foreach
}# end process block
end {
$data
}
Olaf
June 10, 2019, 4:12pm
2
I’d recommend to use the #requires statement instead of your begin block and some minor tweaks but the user description works for me as intended I think. What is you problem?
That should be enough actually:
#Requires -Modules ActiveDirectory
$SearchBase = ‘OU=Infor,OU=COS Groups,DC=ad,DC=springfieldmo,DC=gov’
$Groups = Get-ADGroup -Filter * -SearchBase $SearchBase -Properties name, groupcategory, groupscope, description
foreach ( $Group in $Groups ) {
Get-ADGroupMember -Identity $Group |
Get-ADUser -Properties samaccountname, givenname, sn, title, description |
ForEach-Object {
[pscustomobject]@{
GroupName = $group.name
GroupCategory = $group.groupcategory
GroupScope = $group.groupscope
GroupDescription = $group.description
ObjectName = $.name
objectclass = $ .objectclass
distinguishedName = $.distinguishedName
UserName = $ .samaccountname
FirstName = $.givenname
LastName = $ .sn
Title = $.title
UserDescription = $ .Description
}
} #end foreach
} # end foreach
BTW: Actually since Powershell version 3.0 you don’t need to load the needed modules explicitly. Powershell does this for you automatically.