Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Function Migrate-ADGroupMembers
{
Param (
[Parameter(mandatory=$true)]
[Alias('source', 'src', 'from')]
$SGSource,
[Parameter(mandatory=$true)]
[Alias('destination', 'dest', 'to')]
$SGDest
)
$Error.Clear()
#Get Source Memebers
Write-Host -ForegroundColor White "Getting Source Group Members"
$SourceMembers = Get-ADGroupMember -Identity $SGSource
If($SourceMembers.count -gt 0)
{
Write-Host -ForegroundColor Green "Found $($SourceMembers.count) members in $SGSource"
Write-Host -ForegroundColor White "Adding members to $SGDest"
Try
{
Add-ADGroupMember -Identity $SGDest -Members ($SourceMembers)
}
Catch
{
Write-Host -ForegroundColor Red "Could not add all members due to an error"
Write-Host -ForegroundColor Red "`n"
Write-Host -ForegroundColor Red "$Error[0]"
}
Finally
{
$DestinationMembers = Get-ADGroupMember -Identity $SGDest
If ($SourceMembers.count -eq $DestinationMembers.count)
{
Write-Host -ForegroundColor Green "Count Match between:"
Write-Host -ForegroundColor Green "$($SourceMembers.count) $SGSource"
Write-Host -ForegroundColor Green "$($DestinationMembers.count) $SGDest"
}
ElseIf ($SourceMembers.count -gt $DestinationMembers.count)
{
Write-Host -ForegroundColor Red -BackgroundColor Black "MISMATCH IN MEMBERSHIP COUNT"
Write-Host -ForegroundColor Red -BackgroundColor Black "SOURCE GROUP HAS MORE MEMBERS THAN DESTINATION"
$($SourceMembers | Select Name).name | where {$($DestinationMembers | Select Name).name -notcontains $psitem}
}
ElseIf ($SourceMembers.count -lt $DestinationMembers.count)
{
Write-Host -ForegroundColor Green -BackgroundColor Black "MISMATCH IN MEMBERSHIP COUNT"
Write-Host -ForegroundColor Green -BackgroundColor Black "DESTINATION GROUP HAS MORE MEMBERS THAN SOURCE"
$($DestinationMembers | Select Name).name | where {$($SourceMembers | Select Name).name -notcontains $psitem}
}
Else
{
Write-Host -ForegroundColor Red -BackgroundColor Red " "
Write-Host -ForegroundColor Red -BackgroundColor Black "MISMATCH IN MEMBERSHIP COUNT"
Write-Host -ForegroundColor Red -BackgroundColor Black "$($SourceMembers.count) :: $SGSource"
Write-Host -ForegroundColor Red -BackgroundColor Black "$($DestinationMembers.count) :: $SGDest"
Write-Host -ForegroundColor Red -BackgroundColor Red " "
}
}
}
Else
{
Write-Host -ForegroundColor Red -BackgroundColor Black "Security Group has no members:"
Write-Host -ForegroundColor Red -BackgroundColor Black "$SGSource"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.