PS script to enumerate 25 OUs, look for 2 groups and pull membersl

Jay Thomas
Jay Thomas used Ask the Experts™
on
Hi all.
I look at this question and know that to you PS experts it's an eay one - but not all of us pick up scripting that easy (I did offer to pay for training via a EE-gig lst year, but nothing really came of it ;0)

So, i have 25 OUs under the UK parent OU. Flat straucture here - all 25 OUs under UK. These OUs have many groups assigned to them. You know the thing, it's got a bit out of contol. What i need is a script that can loop through all 25 OUs looking for 2 groups called 'UKSouth' and UKNorth' and from here do 2 actions. Output the group names to a spreadsheet for each OU where they are found and also record the list of users in those groups. Is this something easily achieveble?
Appreciate your looking thank you.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jose Gabriel Ortega CastroEE Rookie/Solution Guide/Topic Advisor and CEO Faru Bonon IT
Awarded 2018
Distinguished Expert 2018

Commented:
Hi what version is your Windows server (Active Directory)?

Author

Commented:
Hi, sorry, should have mentioned, it's 2008 R2
Jose Gabriel Ortega CastroEE Rookie/Solution Guide/Topic Advisor and CEO Faru Bonon IT
Awarded 2018
Distinguished Expert 2018

Commented:
I'd need also the Domain name and the Distinguished name of your "UK" OU.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Author

Commented:
Hi, so it's external facing domain. Could we call it contoso.com and a DN of "ou=uk,dc=contoso,dc=com
I could change there after? Hope this doesn't show a miss trust ???
Jose Gabriel Ortega CastroEE Rookie/Solution Guide/Topic Advisor and CEO Faru Bonon IT
Awarded 2018
Distinguished Expert 2018

Commented:
Nope, it doesn't really matter I'd do the logic to find that out in the script. Give me a bit. We gotta go step by step.

Author

Commented:
Thank you very much
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Try this; the first two columns will have the group information, the rest the respective members.
'UKSouth', 'UKNorth' | Get-ADGroup | ForEach-Object {
	$GroupOU = ($_.DistinguishedName -split ',(?=OU=)', 2)[1]
	$GroupName = $_.Name
	$_ | Get-ADGroupMember | Select-Object -Property @{n='GroupName'; e={$GroupName}}, @{n='GroupOU'; e={$GroupOU}}, SamAccountName, ObjectClass, DistinguishedName
} | Export-Csv -NoTypeInformation -Path C:\Temp\UKSouthNorth.csv

Open in new window

Jose Gabriel Ortega CastroEE Rookie/Solution Guide/Topic Advisor and CEO Faru Bonon IT
Awarded 2018
Distinguished Expert 2018
Commented:
And this will be my version. you can see the steps in there.

[CmdletBinding()]
param(
    [Parameter(Mandatory=$true,Position=0)]$OU
)

$SearchedGroup1="UKSouth"
$SearchedGroup2="UKNorth"
$outputFile="out.csv"

#region Functions
function Get-OUDN{
    [Cmdletbinding()]
    param(
        [Parameter(Mandatory=$true,Position=0)]$OuName
    )
    BEGIN{}
    PROCESS{
        $OU = Get-ADOrganizationalUnit -Filter {name -like "ou"}
    }
    END{
        RETURN $OU.DistinguishedName
    }

}
function Load-ADModule{
    [CmdletBinding()]
    [OutputType([bool])]
   
    $Getmd = Get-Module -ListAvailable | Where-Object{ $_.Name -like "ActiveD*"} | Select Name
    [bool]$output=$false

    if( $Getmd.Name -clike "ActiveDirectory"){
		if(! (Get-Module ActiveDirectory)){
			Write-Output "Loading the Active Directory Module"
			Import-Module ActiveDirectory
			Write-Output "Loaded"
		}
		else{
			Write-Output "Active Directory Module for Powershell is already loaded"
		}
        $output= $true
    }
	return $output
}
#endregion


####START SCRIPT
#LOAD AD
if(-not(Load-ADModule)){
    Write-Output "Active Directory Module was not found"
    Write-Output "For this script to run it is required the ActiveDirectory Module, Please run this script in a Windows Server with the Active Directory Module Enabled (or a Domain Controller)"
    exit(0)
}


#Get DN from the main OU
$DN =Get-OUDN $OU

$SearchedOU = Get-ADOrganizationalUnit -SearchBase $DN -Filter {Name -ne $OU}  | select Name,DistinguishedName


$groups=@()
foreach($CurrentOU in $SearchedOU){
    $GDN= $CurrentOU.DistinguishedName
    $group = get-ADGroup -SearchBase  $GDN -Filter  {Name -like $SearchedGroup1 -or Name -like $SearchedGroup2}
    if($group){
        $groups+=new-object psobject -Property @{"OU"=$CurrentOU;"GroupInfo"=$group}
    }
}

$output=@()
foreach($Currentgroup in $groups){
    foreach($itemgroup in $Currentgroup.GroupInfo){
        $Members =Get-ADGroupMember -Identity $itemgroup.DistinguishedName

        if($Members){
            foreach($Member in $Members){
                $output+= New-Object psobject -Property @{"OU"=$Currentgroup.OU.Name;"Group"=$itemgroup.Name;"Member"=$Member.SamAccountNAme;"MemberName"=$Member.Name;"MemberDN"=$Member.DistinguishedName}
            }
            #    $group | Add-Member -MemberType NoteProperty -Name "UsersInCurrentGroup" -Value $Members
        }
    }
    

}


$output | Export-Csv -NoTypeInformation -Path $outputFile

Open in new window

Author

Commented:
Thank you very much chaps. I'll try these tomorrowand eport back. Thank you again.

Author

Commented:
Thank you for your efforts. Sorry for the late feedback, just not had time to test both scripts. Thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial