Question Ajout Users via txtbox avec password

Plus d'informations
il y a 15 ans 9 mois #6969 par DGI-SYSTEM
Bonjour,

Je suis en cours d'essai de modification de code que j'ai trouvé pour l'aménager afin de pouvoir créer un utilisateur dans un AD via une \"fenêtre\" simple avec l'application d'un mot de passe voulu

voici mon script :

[code:1]
[void][Reflection.Assembly]::LoadWithPartialName(\"System.Windows.Forms\"«»)

##################### Code pour Active Directory ########################
#
$domaineSMTP = \"@mondomainedemessagerie\"
$objOU = [ADSI]\"LDAP://ipserveur.domaine.local/OU=users,DC=domaine,DC=local\"



########################## Code pour le Forms #############################
#
#Je créer mes objets de forms
#
$Creer = New-Object System.Windows.Forms.Button
$Info = New-Object System.Windows.Forms.Button
$MAJ = New-Object System.Windows.Forms.Button
$lbl_Login = new-object System.Windows.Forms.Label
$txtBoxLogin = new-object System.Windows.Forms.TextBox
$lbl_prenom = new-object System.Windows.Forms.Label
$txtBoxPrenom = new-object System.Windows.Forms.TextBox
$txtBoxNom = new-object System.Windows.Forms.TextBox
$lbl_nom = new-object System.Windows.Forms.Label
$lbl_Description = new-object System.Windows.Forms.Label
$lblGroupe = new-object System.Windows.Forms.Label
$txtBoxDescription = new-object System.Windows.Forms.TextBox
$txtBoxGroupe = new-object System.Windows.Forms.TextBox
$lbl_Password = new-object System.Windows.Forms.Label
$txtBoxPassword = new-object System.Windows.Forms.TextBox
#
#
# Creer
#
$Creer.Text = \"Creer\"
$Creer.Location = New-Object System.Drawing.Size(253, 210)
$Creer.Size = New-Object System.Drawing.Size(90,30)
#
# lbl_Login
#
$lbl_Login.AutoSize = $true
$lbl_Login.Location = new-object System.Drawing.Point(78, 47)
$lbl_Login.Name = \"lbl_Login\"
$lbl_Login.Size = new-object System.Drawing.Size(21, 13)
$lbl_Login.TabIndex = 1
$lbl_Login.Text = \"Login :\"
#
# txtBoxLogin
#
$txtBoxLogin.Location = new-object System.Drawing.Point(119, 44)
$txtBoxLogin.Name = \"txtBoxLogin\"
$txtBoxLogin.Size = new-object System.Drawing.Size(100, 20)
$txtBoxLogin.TabIndex = 2
#
# lbl_prenom
#
$lbl_prenom.AutoSize = $true
$lbl_prenom.Location = new-object System.Drawing.Point(56, 104)
$lbl_prenom.Name = \"lbl_prenom\"
$lbl_prenom.Size = new-object System.Drawing.Size(49, 13)
$lbl_prenom.TabIndex = 3
$lbl_prenom.Text = \"Prenom :\"
#
# txtBoxPrenom
#
$txtBoxPrenom.Location = new-object System.Drawing.Point(119, 101)
$txtBoxPrenom.Name = \"textBox1\"
$txtBoxPrenom.Size = new-object System.Drawing.Size(100, 20)
$txtBoxPrenom.TabIndex = 4
#
# txtBoxNom
#
$txtBoxNom.Location = new-object System.Drawing.Point(393, 101)
$txtBoxNom.Name = \"textBox2\"
$txtBoxNom.Size = new-object System.Drawing.Size(100, 20)
$txtBoxNom.TabIndex = 6
#
# lbl_nom
#
$lbl_nom.AutoSize = $true
$lbl_nom.Location = new-object System.Drawing.Point(346, 101)
$lbl_nom.Name = \"lbl_nom\"
$lbl_nom.Size = new-object System.Drawing.Size(32, 13)
$lbl_nom.TabIndex = 5
$lbl_nom.Text = \"Nom:\"
#
# lbl_Description
#
$lbl_Description.AutoSize = $true
$lbl_Description.Location = new-object System.Drawing.Point(39, 162)
$lbl_Description.Name = \"lbl_Description\"
$lbl_Description.Size = new-object System.Drawing.Size(66, 13)
$lbl_Description.TabIndex = 7
$lbl_Description.Text = \"Description :\"
#
# txtBoxDescription
#
$txtBoxDescription.Location = new-object System.Drawing.Point(119, 159)
$txtBoxDescription.Name = \"textBox3\"
$txtBoxDescription.Size = new-object System.Drawing.Size(374, 20)
$txtBoxDescription.TabIndex = 8
#
#Label Groupe
#
$lblGroupe.Location = new-object System.Drawing.Point(56, 215)
$lblGroupe.Name = \"lblGroupe\"
$lblGroupe.Size = new-object System.Drawing.Size(60, 40)
$lblGroupe.Text = \"Groupe :\"
#
# txtBoxGroupe
#
$txtBoxGroupe.Location = new-object System.Drawing.Point(119, 215)
$txtBoxGroupe.Name = \"txtBoxGroupe\"
$txtBoxGroupe.Size = new-object System.Drawing.Size(374, 20)


########################################
# lbl_Password
#
$lbl_Password.AutoSize = $true
$lbl_Password.Location = new-object System.Drawing.Point(330, 47)
$lbl_Password.Name = \"lbl_Password\"
$lbl_Password.Size = new-object System.Drawing.Size(30, 13)
$lbl_Password.TabIndex = 9
$lbl_Password.Text = \"Password :\"
#
# txtBoxPassword
#
$txtBoxPassword.Location = new-object System.Drawing.Point(393, 47)
$txtBoxPassword.Name = \"txtBoxPassword\"
$txtBoxPassword.Size = new-object System.Drawing.Size(100, 20)
$txtBoxPassword.TabIndex = 10

#############################################
#
# Gestions pour Creer un utilisateurs
#
$Creer.Add_Click(
{
[bool]$OK = $false

#Variable pour récupérer la saisie des TexTBox
[string]$CP = $txtBoxLogin.get_text()
[string]$Prenom = $txtBoxPrenom.get_text()
[string]$Nom = $txtBoxNom.get_text()
[string]$Description = $txtBoxDescription.get_text()
[string]$Groupe = $txtBoxGroupe.get_text()
[string]$userCN=\"CN=$($Nom) $($Prenom)\"
[string]$Password = $txtBoxPassword.get_text()





if($CP -eq '')
{
$txtBoxLogin.set_text('Veuillez saisir le sAMAccountName')
}
elseif($Prenom -eq '')
{
$txtBoxPrenom.set_text('Veuillez saisir un prénom')
}
elseif($Nom -eq '')
{
$txtBoxNom.set_text('Veuillez saisir un nom')
}
else{$OK = $true}


if ($OK -eq $true)
{
#Créer l'utilisateur dans l'AD

$objUser= $objOU.Create(\"user\", $userCN)
$objUser.put(\"sAMAccountName\", $CP)
$objUser.put(\"userPrincipalName\", $CP+ $domaineSMTP)
$objUser.put('givenName', $Prenom)
$objUser.put('sn', $Nom)
$objUser.put('password', $Password)

if($Description -ne '')
{
$objUser.put('Description', $Description)
}

$objUser.SetInfo()
$objUser.SetPassword('P@ssw0rd')
#Permet d'activer le compte
$objUser.userAccountControl[0] = $objUser.userAccountControl[0] -band (-bnot 2)
$objUser.SetInfo()


#Remise à zéro des champs
$txtBoxLogin.set_text('')
$txtBoxDescription.set_text('')
$txtBoxNom.set_text('')
$txtBoxPrenom.set_text('')
$txtBoxPassword.set_text('')
}

})


#
#Je dessine mon forms
#
$Form1 = new-object System.Windows.Forms.form
# Form1
#
$Form1.ClientSize = new-object System.Drawing.Size(576, 250)
$Form1.Controls.Add($txtBoxDescription)
$Form1.Controls.Add($lbl_Description)
$Form1.Controls.Add($txtBoxNom)
$Form1.Controls.Add($lbl_nom)
$Form1.Controls.Add($txtBoxPrenom)
$Form1.Controls.Add($lbl_prenom)
$Form1.Controls.Add($txtBoxLogin)
$Form1.Controls.Add($lbl_Login)
$Form1.Controls.Add($txtBoxPassword)
$Form1.Controls.Add($lbl_Password)
$Form1.Controls.Add($Creer)
$Form1.Name = \"Form1\"
$Form1.Text = \"Création d'un nouveau jeune\"
$Form1.ShowDialog()[/code:1]
Je suis preneur de toute bonne idée !!!

Geoffrey<br><br>Message édité par: Arnaud, à: 26/05/10 22:31

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 15 ans 9 mois #6972 par Arnaud Petitjean
Salut !

Je suis preneur de toute bonne idée !!!

d

Le refaire en s'appuyant sur WPF (Windows Presentation Foundation) afin de rendre indépendante ton interface graphique par rapport à ton code ?

Parce que le problème des Windows Forms c'est que c'est assez difficile de modifier l'interface sans impacter le code; cela fait donc des scripts très rigides.

Arnaud

MVP PowerShell et créateur de ce magnifique forum :-)
Auteur de 6 livres PowerShell aux éditions ENI
Fondateur de la société Start-Scripting
Besoin d'une formation PowerShell ?

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 15 ans 9 mois #6977 par DGI-SYSTEM
Bonjour,

Merci de ta réponse si rapide, mais je ne suis pas une grande star dans le développement, j'ai télécharger le Visual Studio Express, je vais voir ce que je peux faire faire avec cela, mais existe-t-il des exemples qui ressemblerais à ce que je veux faire ?

Geoffrey

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 15 ans 9 mois #6978 par Laurent Dardenne
Salut,
dgisystem écrit:

existe-t-il des exemples qui ressemblerais à ce que je veux faire ?

tu peux regarder PowerBoots et PowerShell Packs.

En attendant qq remarques sur ton script.
Ajoute ceci :
[code:1]
$Form1.Add_Shown({$Form1.Activate()})
$Form1.ShowDialog()
[/code:1]
Le code de validation ne doit pas renseigner les champs, sinon tu te tires une balle dans le pied :-)
De plus le contenu d'un champ peut être constitué uniquement d'espaces.

Tu peux :
- utiliser directement la propriété Text au lieu de set_ et get_,
- éviter de déclarer des variables :
[code:1]
#Créer l'utilisateur dans l'AD
$objUser= $objOU.Create(\&quot;user\&quot;, \&quot;CN=$($txtBoxNom.Text) $($txtBoxPrenom.Text)\&quot; )
$objUser.put(\&quot;sAMAccountName\&quot;, $txtBoxLogin.Text)
$objUser.put(\&quot;userPrincipalName\&quot;, $txtBoxLogin.Text + $domaineSMTP)
$objUser.put('givenName', $txtBoxPrenom.Text)
$objUser.put('sn', $txtBoxNom.Text)
$objUser.put('password', $txtBoxPassword.Text )
[/code:1]
- ajouter une gestion d'erreur (serveur offline,compte dupliqué...),
- et enfin l'usage de ErrorProvider facilitera la validation, voir : projets.developpez.com/projects/add-lib/...t-Form/ErrorProvider.

Concernant la remarque d'Arnaud, tu peux déjà placer ton code dans différentes fonctions au lieu de tout coder dans le gestionnaire d'événement.

A mon avis WPF ne résoudra rien si tu continues à coder de cette manière.<br><br>Message édité par: Laurent Dardenne, à: 27/05/10 10:21

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 15 ans 9 mois #6979 par DGI-SYSTEM
Re,

Merci pour ces précitions

Je dois encore avoir une erreur qui persiste aprés ce code :

[void][Reflection.Assembly]::LoadWithPartialName(\&quot;System.Windows.Forms\&quot;)

##################### Code pour Active Directory ########################
#
$domaineSMTP = \&quot;@dgi-system.local\&quot;
$objOU = [ADSI]\&quot;LDAP://sbs08.dgi-system.local/OU=MyBusiness,DC=dgi-system,DC=local\&quot;



########################## Code pour le Forms #############################
#
#Je créer mes objets de forms
#
$Creer = New-Object System.Windows.Forms.Button
$Info = New-Object System.Windows.Forms.Button
$MAJ = New-Object System.Windows.Forms.Button
$lbl_Login = new-object System.Windows.Forms.Label
$txtBoxLogin = new-object System.Windows.Forms.TextBox
$lbl_prenom = new-object System.Windows.Forms.Label
$txtBoxPrenom = new-object System.Windows.Forms.TextBox
$txtBoxNom = new-object System.Windows.Forms.TextBox
$lbl_nom = new-object System.Windows.Forms.Label
$lbl_Description = new-object System.Windows.Forms.Label
$lblGroupe = new-object System.Windows.Forms.Label
$txtBoxDescription = new-object System.Windows.Forms.TextBox
$txtBoxGroupe = new-object System.Windows.Forms.TextBox
$lbl_Password = new-object System.Windows.Forms.Label
$txtBoxPassword = new-object System.Windows.Forms.TextBox
#
#
# Creer
#
$Creer.Text = \&quot;Creer\&quot;
$Creer.Location = New-Object System.Drawing.Size(253, 210)
$Creer.Size = New-Object System.Drawing.Size(90,30)
#
# lbl_Login
#
$lbl_Login.AutoSize = $true
$lbl_Login.Location = new-object System.Drawing.Point(78, 47)
$lbl_Login.Name = \&quot;lbl_Login\&quot;
$lbl_Login.Size = new-object System.Drawing.Size(21, 13)
$lbl_Login.TabIndex = 1
$lbl_Login.Text = \&quot;Login :\&quot;
#
# txtBoxLogin
#
$txtBoxLogin.Location = new-object System.Drawing.Point(119, 44)
$txtBoxLogin.Name = \&quot;txtBoxLogin\&quot;
$txtBoxLogin.Size = new-object System.Drawing.Size(100, 20)
$txtBoxLogin.TabIndex = 2
#
# lbl_prenom
#
$lbl_prenom.AutoSize = $true
$lbl_prenom.Location = new-object System.Drawing.Point(56, 104)
$lbl_prenom.Name = \&quot;lbl_prenom\&quot;
$lbl_prenom.Size = new-object System.Drawing.Size(49, 13)
$lbl_prenom.TabIndex = 3
$lbl_prenom.Text = \&quot;Prenom :\&quot;
#
# txtBoxPrenom
#
$txtBoxPrenom.Location = new-object System.Drawing.Point(119, 101)
$txtBoxPrenom.Name = \&quot;textBox1\&quot;
$txtBoxPrenom.Size = new-object System.Drawing.Size(100, 20)
$txtBoxPrenom.TabIndex = 4
#
# txtBoxNom
#
$txtBoxNom.Location = new-object System.Drawing.Point(393, 101)
$txtBoxNom.Name = \&quot;textBox2\&quot;
$txtBoxNom.Size = new-object System.Drawing.Size(100, 20)
$txtBoxNom.TabIndex = 6
#
# lbl_nom
#
$lbl_nom.AutoSize = $true
$lbl_nom.Location = new-object System.Drawing.Point(346, 101)
$lbl_nom.Name = \&quot;lbl_nom\&quot;
$lbl_nom.Size = new-object System.Drawing.Size(32, 13)
$lbl_nom.TabIndex = 5
$lbl_nom.Text = \&quot;Nom:\&quot;
#
# lbl_Description
#
$lbl_Description.AutoSize = $true
$lbl_Description.Location = new-object System.Drawing.Point(39, 162)
$lbl_Description.Name = \&quot;lbl_Description\&quot;
$lbl_Description.Size = new-object System.Drawing.Size(66, 13)
$lbl_Description.TabIndex = 7
$lbl_Description.Text = \&quot;Description :\&quot;
#
# txtBoxDescription
#
$txtBoxDescription.Location = new-object System.Drawing.Point(119, 159)
$txtBoxDescription.Name = \&quot;textBox3\&quot;
$txtBoxDescription.Size = new-object System.Drawing.Size(374, 20)
$txtBoxDescription.TabIndex = 8
#
#Label Groupe
#
$lblGroupe.Location = new-object System.Drawing.Point(56, 215)
$lblGroupe.Name = \&quot;lblGroupe\&quot;
$lblGroupe.Size = new-object System.Drawing.Size(60, 40)
$lblGroupe.Text = \&quot;Groupe :\&quot;
#
# txtBoxGroupe
#
$txtBoxGroupe.Location = new-object System.Drawing.Point(119, 215)
$txtBoxGroupe.Name = \&quot;txtBoxGroupe\&quot;
$txtBoxGroupe.Size = new-object System.Drawing.Size(374, 20)


########################################
# lbl_Password
#
$lbl_Password.AutoSize = $true
$lbl_Password.Location = new-object System.Drawing.Point(330, 47)
$lbl_Password.Name = \&quot;lbl_Password\&quot;
$lbl_Password.Size = new-object System.Drawing.Size(30, 13)
$lbl_Password.TabIndex = 9
$lbl_Password.Text = \&quot;Password :\&quot;
#
# txtBoxPassword
#
$txtBoxPassword.Location = new-object System.Drawing.Point(393, 47)
$txtBoxPassword.Name = \&quot;txtBoxPassword\&quot;
$txtBoxPassword.Size = new-object System.Drawing.Size(100, 20)
$txtBoxPassword.TabIndex = 10

#############################################
#
# Gestions pour Creer un utilisateurs
#
$Creer.Add_Click(
{
[bool]$OK = $false

#Variable pour récupérer la saisie des TexTBox
[string]$CP = $txtBoxLogin.get_text()
[string]$Prenom = $txtBoxPrenom.get_text()
[string]$Nom = $txtBoxNom.get_text()
[string]$Description = $txtBoxDescription.get_text()
[string]$Groupe = $txtBoxGroupe.get_text()
[string]$userCN=\&quot;CN=$($Nom) $($Prenom)\&quot;
[string]$Password = $txtBoxPassword.get_text()





if($CP -eq '')
{
$txtBoxLogin.set_text('Veuillez saisir le sAMAccountName')
}
elseif($Prenom -eq '')
{
$txtBoxPrenom.set_text('Veuillez saisir un prénom')
}
elseif($Nom -eq '')
{
$txtBoxNom.set_text('Veuillez saisir un nom')
}
else{$OK = $true}


if ($OK -eq $true)
{
#Créer l'utilisateur dans l'AD

$objUser= $objOU.Create(\&quot;user\&quot;, $userCN)
$objUser.put(\&quot;sAMAccountName\&quot;, $CP)
$objUser.put(\&quot;userPrincipalName\&quot;, $CP+ $domaineSMTP)
$objUser.put('givenName', $Prenom)
$objUser.put('sn', $Nom)
$objUser.put('password', $Password)

if($Description -ne '')
{
$objUser.put('Description', $Description)
}


#Permet d'activer le compte
$objUser.userAccountControl[0] = $objUser.userAccountControl[0] -band (-bnot 2)
$objUser.SetInfo()


#Remise à zéro des champs
$txtBoxLogin.set_text('')
$txtBoxDescription.set_text('')
$txtBoxNom.set_text('')
$txtBoxPrenom.set_text('')
$txtBoxPassword.set_text('')
}

})


#
#Je dessine mon forms
#
$Form1 = new-object System.Windows.Forms.form
# Form1
#
$Form1.ClientSize = new-object System.Drawing.Size(576, 250)
$Form1.Controls.Add($txtBoxDescription)
$Form1.Controls.Add($lbl_Description)
$Form1.Controls.Add($txtBoxNom)
$Form1.Controls.Add($lbl_nom)
$Form1.Controls.Add($txtBoxPrenom)
$Form1.Controls.Add($lbl_prenom)
$Form1.Controls.Add($txtBoxLogin)
$Form1.Controls.Add($lbl_Login)
$Form1.Controls.Add($txtBoxPassword)
$Form1.Controls.Add($lbl_Password)
$Form1.Controls.Add($Creer)
$Form1.Name = \&quot;Form1\&quot;
$Form1.Text = \&quot;Création d'un nouveau jeune\&quot;
$Form1.Add_Shown({$Form1.Activate()})
$Form1.ShowDialog()

Voila mon erreur :

Impossible d'indexer dans un objet de type System.DirectoryServices.PropertyValueCollection.
Au niveau de ligne : 39 Caractère : 32
+ $objUser.userAccountControl[ &lt;&lt;&lt;&lt; 0] = $objUser.userAccountControl[0] -band (-bnot 2)
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : CannotIndex

Exception lors de l'appel de « SetInfo » avec « 0 » argument(s) : « L'attribut ou la valeur de service d'annuaire spéci
fié n'existe pas.
 »
Au niveau de ligne : 40 Caractère : 20
+ $objUser.SetInfo &lt;&lt;&lt;&lt; ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

merci d'avance les gars

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 15 ans 9 mois #6984 par Laurent Dardenne
Teste déjà ton code lié à AD en dehors de l'interface.

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.049 secondes
Propulsé par Kunena