Add a Remote Computer to AD Group


A less simple tool I use to modify (add) a Remote Computer to an AD Group, and then triggers a Scan for AD Infos right after.



I'm obviously not a Powershell expert ahah

I'll have to ask someone more competent than me to make a presentable version of this thing !

First, I don't like Microsoft's consoles when it comes to managing AD groups.
So, basically, I find it better in a Powershell prompt, it is much more visible / readable / fast.

So we ask the AD groups, we put it in a menu that attaches a number. Simply enter the desired number, the computer is added to the corresponding group and the shell closes itself.

In this 'thing', I look only for groups named " G_ordinateurs* ". You need to adapt this.

Here is the code 


#Script pour ajouter un poste à un groupe AD

#On cherche les groupes AD et on affiche un menu de sélection:
$Groupes = Get-ADGroup -Filter "name -like 'G_ordinateurs*'"
$Menu = @{}
for ($Valeur=1;$Valeur -le $Groupes.count; $Valeur++)
{Write-Host "$Valeur= $($Groupes[$Valeur-1].name)"
[int]$Choix = Read-Host 'Choisir le Groupe AD: '

#On enregistre la sélection du menu
$Selection = $Menu.Item($Choix)

#On recré les Distinguished Names:
$ADUserInfo = Get-ADComputer "$(Computer:TARGET)" -Properties *
$DistingName = $ADUserInfo.DistinguishedName
$Identity = $DistingName
$GroupName = 'G_ordinateurs*'
$DistingName2 = $ADGroupInfo.DistinguishedName
#2nd menu pour correspondance DistinguishedName avec Name du menu d'avant:
$Menu2 = @{}
$ADGroupInfo = Get-ADGroup -Filter "name -like 'G_ordinateurs*'"
for ($Valeur2=1;$Valeur2 -le $ADGroupInfo.count; $Valeur2++)
{Write-Host "$Valeur2= $($ADGroupInfo[$Valeur2-1].distinguishedname)"
$Choix2 = $Menu2.Item($Choix)

#On ajoute le poste au groupe choisi:
Add-ADPrincipalGroupMembership -Identity:"$Identity" -MemberOf:"$Choix2" -Verbose

Write-Host ""
Write-Host "Le poste "$(Computer:TARGET)" a ete ajoute au Groupe AD '$Selection', OK!" -ForegroundColor Green
Write-Host ""

Invoke-Command -ScriptBlock {pdqinventory ScanComputers -Computers "$(Computer:TARGET)" -ScanProfile "AD Infos"}



Screenshots :






Please sign in to leave a comment.

Didn't find what you were looking for?

New post