Solved

script to copy group from one user to other

Posted on 2014-01-20
2
699 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The viewer will learn how to count occurrences of each item in an array.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

16 Experts available now in Live!

Get 1:1 Help Now