Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

script to copy group from one user to other

Posted on 2014-01-20
2
Medium Priority
?
720 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 41

Accepted Solution

by:
footech earned 2000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Let's recap what we learned from yesterday's Skyport Systems webinar.
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

609 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