Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

test existing group and user in powershell v2

$
0
0

hello,

i want to add a user in a group in my script.

this function is done by my script but i want to test if group and user exist and sometimes users are in the group , i want to catch the error message and show an other message.

Thank you

script:

Function UserAdd
{
$Group = Read-Host "Merci de choisir votre groupe d'accès HO"
$domainLDAP = [ADSI]''
$ObjRecherche = New-Object System.DirectoryServices.DirectorySearcher($domainLDAP)
$objRecherche.Filter ="(&(objectClass=group)(Name=$Group)(description=*))"
$ObjResult = $ObjRecherche.findall()

#Récupération du DistinguishedName et la description#

foreach ($result in $objResult)

{

$groupEntry = $result.getdirectoryentry()

$groupDN = $groupEntry.distinguishedName
$groupDesc = $groupEntry.description

}

$User = Read-Host "Merci d'entrer le login HO de la personne à ajouter dans le groupe."
$domainLDAP= [ADSI]''
$ObjUserRecherche = New-Object System.DirectoryServices.DirectorySearcher($domainLDAP)
$objUserRecherche.Filter ="(&(objectClass=user)(SamAccountName=$User))"
$ObjuserResult = $ObjUserRecherche.findall()

foreach ($UserResult in $ObjuserResult)

{
 
$userEntry = $UserResult.GetDirectoryEntry()
$userDN = $userEntry.distinguishedName
 
}

#Connexion au groupe et ajout du user en tant que membre.#

$Connexion = [ADSI]"LDAP://$GroupDN"
$Connexion.Add("LDAP://$userDN")
$Connexion.SetInfo()

Write-Host "`n`nVotre utilisateurs $User a été ajouté au groupe du domaine HO $Group, bonne journée."
}

do{
$verifyUser = read-host "Voulez vous Ajouter un utilisateur à un groupe, O ou N ?"
IF ($verifyUser -eq "o"){Useradd}
}
until ($verifyUser -eq "N")
Write-Host "Merci et à bientôt"


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>