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

If you are looking to automate repetitive tasks in Active Directory management using the PowerShell module, then this book is for you. Any experience in PowerShell would be an added advantage.
Table of Contents (11 chapters)
10
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...