Question
Ajout Users via txtbox avec password
- DGI-SYSTEM
- Auteur du sujet
- Hors Ligne
- Nouveau membre
-
Réduire
Plus d'informations
- Messages : 3
- Remerciements reçus 0
il y a 15 ans 9 mois #6969
par DGI-SYSTEM
Ajout Users via txtbox avec password a été créé 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
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.
- Arnaud Petitjean
-
- Hors Ligne
- Modérateur
-
il y a 15 ans 9 mois #6972
par Arnaud Petitjean
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 ?
Réponse de Arnaud Petitjean sur le sujet Re:Ajout Users via txtbox avec password
Salut !
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
dJe suis preneur de toute bonne idée !!!
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.
- DGI-SYSTEM
- Auteur du sujet
- Hors Ligne
- Nouveau membre
-
Réduire
Plus d'informations
- Messages : 3
- Remerciements reçus 0
il y a 15 ans 9 mois #6977
par DGI-SYSTEM
Réponse de DGI-SYSTEM sur le sujet Re:Ajout Users via txtbox avec password
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
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.
- Laurent Dardenne
- Hors Ligne
- Modérateur
-
Réduire
Plus d'informations
- Messages : 6311
- Remerciements reçus 68
il y a 15 ans 9 mois #6978
par Laurent Dardenne
Tutoriels PowerShell
Réponse de Laurent Dardenne sur le sujet Re:Ajout Users via txtbox avec password
Salut,
dgisystem écrit:
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(\"user\", \"CN=$($txtBoxNom.Text) $($txtBoxPrenom.Text)\" )
$objUser.put(\"sAMAccountName\", $txtBoxLogin.Text)
$objUser.put(\"userPrincipalName\", $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
dgisystem écrit:
tu peux regarder PowerBoots et PowerShell Packs.existe-t-il des exemples qui ressemblerais à ce que je veux faire ?
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(\"user\", \"CN=$($txtBoxNom.Text) $($txtBoxPrenom.Text)\" )
$objUser.put(\"sAMAccountName\", $txtBoxLogin.Text)
$objUser.put(\"userPrincipalName\", $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.
- DGI-SYSTEM
- Auteur du sujet
- Hors Ligne
- Nouveau membre
-
Réduire
Plus d'informations
- Messages : 3
- Remerciements reçus 0
il y a 15 ans 9 mois #6979
par DGI-SYSTEM
Réponse de DGI-SYSTEM sur le sujet Re:Ajout Users via txtbox avec password
Re,
Merci pour ces précitions
Je dois encore avoir une erreur qui persiste aprés ce code :
[void][Reflection.Assembly]::LoadWithPartialName(\"System.Windows.Forms\")
##################### Code pour Active Directory ########################
#
$domaineSMTP = \"@dgi-system.local\"
$objOU = [ADSI]\"LDAP://sbs08.dgi-system.local/OU=MyBusiness,DC=dgi-system,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)
}
#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.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[ <<<< 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 <<<< ()
+ CategoryInfo : NotSpecified: (
[], MethodInvocationException
+ FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI
merci d'avance les gars
Merci pour ces précitions
Je dois encore avoir une erreur qui persiste aprés ce code :
[void][Reflection.Assembly]::LoadWithPartialName(\"System.Windows.Forms\")
##################### Code pour Active Directory ########################
#
$domaineSMTP = \"@dgi-system.local\"
$objOU = [ADSI]\"LDAP://sbs08.dgi-system.local/OU=MyBusiness,DC=dgi-system,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)
}
#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.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[ <<<< 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 <<<< ()
+ CategoryInfo : NotSpecified: (
+ FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI
merci d'avance les gars
Connexion ou Créer un compte pour participer à la conversation.
- Laurent Dardenne
- Hors Ligne
- Modérateur
-
Réduire
Plus d'informations
- Messages : 6311
- Remerciements reçus 68
il y a 15 ans 9 mois #6984
par Laurent Dardenne
Tutoriels PowerShell
Réponse de Laurent Dardenne sur le sujet Re:Ajout Users via txtbox avec password
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
- Vous êtes ici :
-
Accueil
-
forum
-
PowerShell
-
Entraide pour les débutants
- Ajout Users via txtbox avec password