1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| $ErrorActionPreference='SilentlyContinue' #to not display the error messages
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition #path of the folder where is located the current script
# textbox to select the group to be copied (the script of the textbox is common and in the file textbox.ps1)
$text="Please enter the name of the group to be copied:"
. "$scriptPath\textbox.ps1"
$groupsource=$objTextBox.Text #name of the source group
# textbox to select the group to be feeded
$text="Please enter the name of the group to be feeded:"
. "$scriptPath\textbox.ps1"
$groupdest=$objTextBox.Text #name of the destinatory GAD-SU-NextGenPDM_NonPROD
#one has to use for example sAMAccountName instead of name for any AD cmdlet
$samgrsource = ((Get-ADGroup -Filter {name -eq $groupsource} -Server "gad.schneider-electric.com").sAMAccountName)
$samgrdest = ((Get-ADGroup -Filter {name -eq $groupdest} -Server "gad.schneider-electric.com").sAMAccountName)
$DebugPreference='SilentlyContinue'
if (($samgrsource -eq $null) -OR ($samgrdest -eq $null)) { Write-Host "unknown name for source or destinatory file";return;}
if ($samgrsource -eq $groupdest) {Write-Host "source and destinatory files are identical";return;}
#To clean the group to be feeded
try {
Set-ADGroup -Identity $samgrdest -clear member -Server "gad.schneider-electric.com"
}
catch {Write-Host "unknown name for destinatory group";return}
try {
Get-ADGroupMember -Identity $samgrsource -Server "gad.schneider-electric.com"| foreach {
Write-Host $_.sAMAccountName"`t " -NoNewline -ForegroundColor Green
$u = $_ #the switch instruction generates aldo a $_ variable, so memorization
#to do a search in the right domain, first locate in DistinguishedName what is this domain
switch ($_.DistinguishedName) {
{($_).contains("DC=gad,DC=schneider-electric,DC=com")} {
$user = get-aduser -filter {sAMAccountName -like "SESA1%" -and sAMAccountName -eq $u.sAMAccountName -and Enabled -eq $True} -Server "gad.schneider-electric.com"
}
{($_).contains("DC=apa,DC=gad,DC=schneider-electric,DC=com")} {
$user = get-aduser -filter {sAMAccountName -like "SESA1%" -and sAMAccountName -eq $u.sAMAccountName -and Enabled -eq $True} -Server "apa.gad.schneider-electric.com"
}
{($_).contains("DC=gmea,DC=gad,DC=schneider-electric,DC=com")} {
$user = get-aduser -filter {sAMAccountName -like "SESA1%" -and sAMAccountName -eq $u.sAMAccountName -and Enabled -eq $True} -Server "gmea.gad.schneider-electric.com"
}
{($_).contains("DC=eur,DC=gad,DC=schneider-electric,DC=com")} {
$user = get-aduser -filter {sAMAccountName -like "SESA1%" -and sAMAccountName -eq $u.sAMAccountName -and Enabled -eq $True} -Server "eur.gad.schneider-electric.com"
}
{($_).contains("DC=nam,DC=gad,DC=schneider-electric,DC=com")} {
$user = get-aduser -filter {sAMAccountName -like "SESA1%" -and sAMAccountName -eq $u.sAMAccountName -and Enabled -eq $True} -Server "nam.gad.schneider-electric.com"
}
}
if ($user) {
#if the domain has been found, some displays+add the user in the new group
Write-Host $user.Enabled"`t " -NoNewline -ForegroundColor Red
Write-Host "sAMAccountName" $user.sAMAccountName"`t " -NoNewline -ForegroundColor White
Write-Host ".Name" $user.Name -NoNewline -ForegroundColor Yellow
Add-ADGroupMember -Identity $samgrdest -members $u -Server "gad.schneider-electric.com"
}
Write-Host ""
}}
catch {Write-Host "unknown name for source group"} |
Partager