Book Image

Active Directory with PowerShell

By : Pamarthi Venkata Sitaram, YELLAPRAGADA U PADMAVATHI
5 (1)
Book Image

Active Directory with PowerShell

5 (1)
By: Pamarthi Venkata Sitaram, YELLAPRAGADA U PADMAVATHI

Overview of this book

Table of Contents (16 chapters)
Active Directory with PowerShell
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Comparing AD groups' membership


Sometimes, you might find a need to compare membership of two security groups. This is generally required to find whether they are identical. This not only helps in getting rid of duplicate groups, but also in troubleshooting permission issues. The function discussed in the following code takes two group names as input and compares their members. The output contains a report that shows the number of objects that exist in both the groups, and the number of objects that are found in the first group but not the second and vice versa:

Function Compare-ADGroups {
[CmdletBinding()]
Param(
  [Parameter(Mandatory=$true, Position = 0)]
  [String]$Group1,
  [Parameter(Mandatory=$true, Position = 1)]
  [String]$Group2,
  [Parameter(Position = 2)]
  [switch]$Nested
)
try {
  $Group1Obj = @(Get-ADGroupMember -Identity $Group1 - Recursive:$Nested -EA Stop)
  $Group2Obj = @(Get-ADGroupMember -Identity $Group2 - Recursive:$Nested -EA Stop)
  } catch {
    Write-Warning "Failed...