Solved

script to copy group from one user to other

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
NTP Servers 4 44
PowerShell and cisco ios 3 43
Local admin account 3 40
Modifying Powershell: Recipients returns System.Collections.ArrayList ? 4 33
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

756 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