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 62 63 64 65 66 67 68 69 70 71 72 73 74
|
Function dropdown_list($listgroup)
{
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$form1 = New-Object Windows.Forms.Form
$form1.text = "Controls"
$form1.Size = New-Object System.Drawing.Size(300,200)
#dropdown list (ComboBox).
$liste1 = New-Object System.Windows.Forms.Combobox
$liste1.Location = New-Object Drawing.Point 20,20
$liste1.Size = New-Object System.Drawing.Size(250,30)
$liste1.DropDownStyle = "DropDownList"
$liste1.Items.AddRange(($listgroup))
$liste1.SelectedIndex = 0
#add the control to the window
$form1.controls.add($liste1)
#displays all
$form1.ShowDialog()
#displays the selection.
write-host "ComboBox = " $liste1.Text
}
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition #path of the folder where is located the current script
$listgroup=""
$lgroup=Get-ADGroup -filter * -Server "gad.schneider-electric.com:3268"
foreach($lg in $lgroup) {$listgroup+='"'+$lg.name+'",' }
$listgroup
#$listgroup="GAD-SU-NextGenPDM_NonPROD","GAD-SU-NextGenPDM_PrePROD","GAD-SU-MediaReviewUATGroup","GAD-SU-MediaReviewUserGroup","GAD-SU-NextGenPDM_TRN","GAD-SU-NextGenPDM_DEV"
# dropdown list to select the group to be copied
$groupsource=dropdown_list $listgroup
#. "$scriptPath\liste_der.ps1"
#$groupsource=$liste1.Text
# dropdown list to select the group to feed
. "$scriptPath\liste_der.ps1"
$groupdest=$liste1.Text
#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)
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 -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 -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 -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 -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 -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 $user.SamAccountName"`t " -NoNewline -ForegroundColor White
Write-Host $user.Name -NoNewline -ForegroundColor Yellow
Add-ADGroupMember -Identity $samgrdest -members $u -Server "gad.schneider-electric.com"
}
Write-Host ""
} |
Partager