Difference between revisions of "AD Groups Script Examples (PowerShell)"
Jump to navigation
Jump to search
(Initial creation) |
(Added "Compare Group Members") |
||
Line 35: | Line 35: | ||
} | } | ||
} | } | ||
</source> | |||
== Compare Group Members == | |||
This script creates a table of all the members of some groups. This enables you to compare a number of groups side by side in a flexible grid. | |||
<source lang="powershell"> | |||
$GroupNames = @() | |||
$GroupNames += "Group A" | |||
$GroupNames += "Group B" | |||
$GroupNames += "Group C" | |||
$GroupNames += "Group D" | |||
Import-Module ActiveDirectory | |||
# Create table for output | |||
$table = New-Object system.Data.DataTable "Groups" | |||
$col1 = New-Object system.Data.DataColumn Signum,([string]) | |||
$col2 = New-Object system.Data.DataColumn Name,([string]) | |||
$table.columns.add($col1) | |||
$table.columns.add($col2) | |||
# Add columns for each group | |||
foreach ($GroupName in $GroupNames) { | |||
$table.columns.add((New-Object system.Data.DataColumn $GroupName,([bool]))) | |||
} | |||
# Go through each group | |||
foreach ($GroupName in $GroupNames) { | |||
Write-Host "Getting members of $GroupName..." | |||
$GroupMembers = Get-ADGroupMember $GroupName | |||
foreach ($user in $GroupMembers) { | |||
if ($user.objectClass -ne "user") { | |||
# Add handling for groups here ! | |||
Continue | |||
} | |||
# Check if row already exists for user, otherwise create a new row | |||
$row = $table.Select("Signum = '" + $user.SamAccountName + "'") | |||
if ($row.count) { | |||
$row[0][$GroupName] = $true | |||
} else { | |||
$row = $table.NewRow() | |||
$row.Signum = $user.SamAccountName | |||
$row.Name = $user.name | |||
$row[$GroupName]= $true | |||
$table.Rows.Add($row) | |||
} | |||
} | |||
} | |||
$table.AcceptChanges() | |||
$table | Select -ExcludeProperty RowError, RowState, Table, ItemArray, HasErrors | Out-GridView -Title "Group Compare results" | |||
</source> | </source> | ||
[[Category:PowerShell]] | [[Category:PowerShell]] | ||
[[category:Active Directory]] | [[category:Active Directory]] |