Monday, March 26, 2018

Update Group Membership – PowerShell Script


If you have multiple domains or performing a user or group migration, you may need to manually update (depend on your scenario) the source or target group membership.  This script can be used to update group membership based on source user’s group membership.  The input for this script the user name (sAMAccountName) and it assumes that the source and target sAMAccountName are the same. 
Input file (Users.csv) Format:















Script validates users in the source domain and collect “memberof” details and then add the target user (migrated user) to the same group. At the end of the operation, the source user and the target user (migrated user) will be part of same security group in the source domain. 
You can see some other “Update Group Membership” script here - http://portal.sivarajan.com/2014/01/update-group-membershippowershell-script.html
Script:
#
# Update Group Membership
# Santhosh Sivarajan (Santhosh@Sivarajan.Com)
#
Clear
Import-Module ActiveDirectory
$userN = ""
$GroupDetails = ""
$Group = ""
$GroupsDN = ""
$uValidation = ""
$tagetDomain = "labanddemo.com"
$Cdate = (Get-Date).tostring("dd-MM-yyyy-hh-mm-ss")


        $SGBeforeUpdateFile = New-Item -type file -force "C:\Temp\Groups_Before_$Cdate.csv"
        $SGAfterUpdateFile = New-Item -type file -force "C:\Temp\Groups_After_$Cdate.csv"
        Import-CSV "C:\Temp\Users.csv" | % {
        $userN = $_.userName
        $sourceDomain = $_.Domain

               
                   $uValidation = Get-ADUser -filter {sAMAccountName -eq $userN} -Server $tagetDomain
                  
                       If($uValidation -eq $Null)
                        {
                           Write-Host "User $userN Doesn't Exist in $tagetDomain Domain"
                           $errorFile = New-Item -type file -force "C:\Temp\Error_$Cdate.csv"
                           "User $userN Doesn't Exist in $tagetDomain Domain"| Out-File $errorFile -encoding ASCII -append
                        }
                        Else
                        {
                           $userN | Out-File $SGBeforeUpdateFile -encoding ASCII -append
                           $GroupDetails = get-aduser -Server $sourceDomain -identity $userN -Properties memberof
                           $GroupsDN = $GroupDetails.memberof
                           $GroupsDN | Out-File $SGBeforeUpdateFile -encoding ASCII -append
                           foreach ($Group in $GroupsDN)
                              {
                               $MigrateduserN = Get-ADUser $userN -Server $tagetDomain -Properties DistinguishedName
                               Write-host "Adding User -> $MigrateduserN"
                               Write-host "To Group -> $Group"
                               Add-ADGroupmember -Server $sourceDomain -Identity $Group -Members $MigrateduserN
                               $members = Get-ADGroupmember -Server $sourceDomain -Identity $Group
                               $GroupName = Get-ADGroup -Server $sourceDomain $Group
                               $GroupName.Name | Out-File $SGAfterUpdateFile -encoding ASCII -append
                               $members.distinguishedName | Out-File $SGAfterUpdateFile -encoding ASCII -append
                               Write-host "....Done!" -ForegroundColor Green
                               Write-host ""
                               }
                        }

        }

Download:
You can also download the script from the following locations:

  1. OneDrive
  2. TechNet Gallery 


2 comments:

If you are interested in writing your critical essay soon, try to check https://samedaypaper.org/blog/critical-essay out. Here you can find high quality writing tips.

Post a Comment

Popular Posts

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More