Solved

script to copy group from one user to other

Posted on 2014-01-20
2
710 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Updating Printer Details in VB.Net 16 38
Network Policy to lock out from idle session 7 23
How do I convert from bytes to TB 4 22
Active Directory Replication 1 9
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

740 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