goezler

Copy-AdGroupMemberShip.ps1

Jan 21st, 2021 (edited)
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.83 KB | None | 0 0
  1. <#  
  2.     .Synopsis  
  3.     Copy or clone source user's member of group to another user, Copy group membership from one user to another in Active Directory.
  4.    .Description  
  5.    Run this script on domain controller, or install RSAT tool on your client machine. This will copy existing given users group to other give group. It validates and verify whether Source and Destination users exists or you have access.
  6.    .Example  
  7.    .\Copy-AdGroupMemberShip.ps1 -SourceUserGroup Administrator -DestinationUsers user1, user2, user3
  8.        
  9.    It takes provided Source user, note down which groups it is member of. Add same groups in the member of tabs of users list provided in parameter DestinationUsers.
  10.    .Example
  11.    .\Copy-AdGroupMemberShip.ps1 -SourceUser Administrator -DestinationUsers (Get-Content C:\Userlist.txt)
  12.  
  13.    Users list can be provided into text file.
  14.    .Example
  15.    user1, user2, user3 | .\Copy-AdGroupMemberShip.ps1 -SourceUser Administrator
  16.  
  17.    .Notes
  18.    NAME: Copy-AdGroupMemberShip
  19.    AUTHOR: Kunal Udapi
  20.    CREATIONDATE: 3 February 2019
  21.    LASTEDIT: 4 February 2019
  22.    KEYWORDS: Copy or clone source user's member of group to another user.
  23.     .Link  
  24.     #Check Online version: http://kunaludapi.blogspot.com
  25.     #Check Online version: http://vcloud-lab.com
  26.     #Requires -Version 3.0  
  27.     #>  
  28. #requires -Version 3  
  29. [CmdletBinding()]
  30. param
  31. (  
  32.     [Parameter(Mandatory=$true,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)]
  33.     [alias('DestUser')]
  34.     [String[]]$DestinationUsers,
  35.     [String]$SourceUserGroup = 'Administrator') #param
  36. Begin
  37. {  
  38.     Import-Module ActiveDirectory
  39. } #Begin
  40.  
  41. Process
  42. {
  43.     try
  44.     {
  45.         $sourceUserMemberOf = Get-AdUser $SourceUserGroup -Properties MemberOf -ErrorAction Stop
  46.     }
  47.     catch
  48.     {
  49.         Write-Host -BackgroundColor DarkRed -ForegroundColor White $Error[0].Exception.Message
  50.         Break
  51.     }
  52.    
  53.     #$destinationUser = @('TestUser','vKunal','Test','TestUser1','Test2')
  54.     $DestinationUser = [System.Collections.ArrayList]$DestinationUsers
  55.    
  56.     $confirmedUserList = @()
  57.     foreach ($user in $destinationUser)
  58.     {
  59.         try
  60.         {
  61.             Write-Host -BackgroundColor DarkGray "Checking user '$user' status in AD..." -NoNewline
  62.             [void](Get-ADUser $user -ErrorAction Stop)
  63.             Write-Host -BackgroundColor DarkGreen -ForegroundColor White "...Tested user '$user' exist in AD"
  64.             $confirmedUserList += $user
  65.         }
  66.         catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
  67.         {
  68.             Write-Host -BackgroundColor DarkRed -ForegroundColor White "...User '$user' doesn't exist in AD"
  69.            
  70.         } #catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
  71.         catch
  72.         {
  73.             Write-Host -BackgroundColor DarkRed -ForegroundColor White "...Check your access"
  74.         } #catch
  75.     } #foreach ($user in $destinationUser)
  76.    
  77.     Write-host "`n"
  78.    
  79.     foreach ($group in $sourceUserMemberOf.MemberOf)
  80.     {
  81.         try
  82.         {
  83.             $groupInfo = Get-AdGroup $group
  84.             $groupName = $groupInfo.Name
  85.             $groupInfo | Add-ADGroupMember -Members $confirmedUserList -ErrorAction Stop
  86.             Write-Host -BackgroundColor DarkGreen "Added destination users to group '$groupName'"
  87.         } #try
  88.  
  89.         catch
  90.         {
  91.             #$Error[0].Exception.GetType().fullname
  92.             if ($null -eq $confirmedUserList[0]) {
  93.                 Write-Host -BackgroundColor DarkMagenta "Provided destination user list is invalid, Please Try again."
  94.                 break
  95.             }
  96.             Write-Host -BackgroundColor DarkMagenta $groupName - $($Error[0].Exception.Message)
  97.         } #catch
  98.     } #foreach ($group in $sourceUserMemberOf.MemberOf)
  99. } #Process
  100. end {}
  101.  
Add Comment
Please, Sign In to add comment