Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Get-ADUsers in group named like and list recursive groups

$
0
0

Hi.

We have had a large organizational change and I have to change security groups and user rights accordingly for the whole organization accordingly.

For that reason I need to list all users MemberOf group starting with "gteam2_*" and have a list showing which recursive groups each "gteam2_*" group is MemberOf.

Example:

User John is MemberOf global security group "gteam2_HumanRessource".

Gteam2_HumanRessource is member of the following groups (mostly local groups but can also be global groups):

RDAT_SHP_HR_Menue

RDAT_SHP_HR_Admins

GFUNK_HR

etc.

I want to have an Output looking like

Groupname;Name;SAMAccountName;Description;Manager (as SamAccountName);NestedGroups

Gteam2_HR;John;John Clease;HR-Department;MAN;RDAT_SHP_HR_Menue

Gteam2_HR;John;John Clease;HR-Department;MAN;RDAT_SHP_HR_Admins

Gteam2_HR;John;John Clease;HR-Department;MAN;GFUNK_HR

So far I have managed to get this Output:

Groupname;SamAccountName;Name

My script looks like this:

____________________________________

Import-Module ActiveDirectory
$server = read-host -prompt 'type domain'
$Groups = (Get-AdGroup -server $server -filter * | Where {$_.name -like "gteam2_*"} | select name -expandproperty name)


$Table = @()

$Record = [ordered]@{
"Group Name" = ""
"Name" = ""
"Username" = ""
}

 

Foreach ($Group in $Groups)
{

$Arrayofmembers = Get-ADGroupMember -server $server -identity $Group -Recursive | select name,samaccountname

foreach ($Member in $Arrayofmembers)
{
$Record."Group Name" = $Group
$Record."Name" = $Member.name
$Record."UserName" = $Member.samaccountname
$objRecord = New-Object PSObject -property $Record
$Table += $objrecord

}

}

# Output the result to the file groupmemberships.csv
$Path = "C:\users\John\documents\"

# Get the group name, which we'll use to name the HTML File.
$file = $path + $server + '_' + 'Gteam2Members.csv'

$Table | export-csv -path "$file" -NoTypeInformation -Encoding "UTF8"

_____________________

I need the following info in my output/added to my script, but I do not know how:

Description (of SamAccountName)

Manager (listetd as SamAccountName)

Nested groups that gteam_* is a MemberOf

I would be very happy if someone could help me.

Regards Carsten

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles