Solved

script to copy group from one user to other

Posted on 2014-01-20
2
703 Views
Last Modified: 2014-01-20
hello,

i have a powershell script to copy all group from one user to second user.
i need to add to this script an exclusion to exclude a list of group to be copied.
ideally a txt file named groupexlusion.txt where i can add group to exlude like this:

group1
group2

so when i lunsh my script to copy groups from user 1 to user2, all group will be copied exept the group1 and group2 (if user1 is memeber of this 2 group)

here my script:

Import-Module ActiveDirectory
$ErrorActionPreference = "SilentlyContinue"

while ($ok -ne "non" ) {
    Write-Host 
    $usr1 = Read-Host "plz enter the username source" 
    $test = Get-ADUser -LDAPFilter "(sAMAccountName=$usr1)"
   
    If ($test -eq $Null) {
        while ($test -eq $Null) {
          Write-Host  
          Write-Host "user not exist"
          Write-Host
		  $usr1 = Read-Host "plz enter the username source" 
		  $test = Get-ADUser -LDAPFilter "(sAMAccountName=$usr1)"
	   }
    }
    Write-Host
    Write-Host "here the groups $usr1 is member:" 
        Get-ADPrincipalGroupMembership -Identity $usr1 | select-Object name
    Write-Host
    Write-Host	


    $usr2 = Read-Host "plz enter the username destination" 
    $test2 = Get-ADUser -LDAPFilter "(sAMAccountName=$usr2)"
   
    If ($test2 -eq $Null) {
    	while ($test2 -eq $Null) {
          Write-Host  
          Write-Host "user not exist"
          Write-Host
		  $usr2 = Read-Host "plz enter the username destination" 
		  $test2 = Get-ADUser -LDAPFilter "(sAMAccountName=$usr2)"
	}
    }


    Write-Host
    Write-Host "here the groups $usr2 is memeber : "
    Write-Host 
        Get-ADPrincipalGroupMembership -Identity $usr2 | select-Object name
    Write-Host
	Write-Host 
    
    
    
    
    do{
        $go = Read-Host "Would you copy groups from $usr1 to $usr2 ? plz respond 'oui' or 'non'"
    }
    while  (($go -ne "oui") -and ($go -ne "non"))
    
    if ($go -eq "oui") {
        Get-ADPrincipalGroupMembership -Identity $usr1 | % {Add-ADPrincipalGroupMembership -Identity $usr2 -MemberOf $_}
    }
    
    
    
    Write-Host
    Write-Host "here the groups $usr2 is now memeber : "
    Write-Host 
        Get-ADPrincipalGroupMembership -Identity $usr2 | select-Object name
    Write-Host 
    Write-Host "------------------------------------------------------------"
	Write-Host
    
    do{
    $ok = Read-Host "do you need to do this operation to other user ? plz respond 'oui' or 'non'"
    }
    while  (($ok -ne "oui") -and ($ok -ne "non"))
}    
   
    

Open in new window

0
Comment
Question by:cawasaki
2 Comments
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 39793882
Try replacing line 55 with the following two lines.
$exclude = Get-Content groupexclusion.txt
Get-ADPrincipalGroupMembership -Identity $usr1 | ? {$exclude -notcontains $_.name} | % {Add-ADPrincipalGroupMembership -Identity $usr2 -MemberOf $_}

Open in new window

0
 

Author Closing Comment

by:cawasaki
ID: 39794021
thank you it work
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now