Powershell: créer users AD à partir d'un CSV - Shell/Batch - Programmation
MarshPosté le 06-11-2020 à 16:39:37
Bonjour à tous
Je suis noob de noob en Powershell, et j'essaie de faire un truc qui devrait normalement être assez simple: importer des utilisateurs dans l'Active Directory à partir d'un fichier CSV
#Vérifiez si le compte utilisateur existe déjà dans AD if (Get-ADUser -F {SamAccountName -eq $Username}) { #Si l’utilisateur existe, éditez un message d’avertissement Write-Warning "A user account $Username has already exist in Active Directory." } else { #Si un utilisateur n’existe pas, créez un nouveau compte utilisateur
#Le compte sera créé dans I’unité d’organisation indiquée dans la variable $OU du fichier CSV ; n’oubliez pas de changer le nom de domaine dans la variable « -UserPrincipalName ». New-ADUser ` -SamAccountName $Username ` -UserPrincipalName "$Username@domain.com" ` -Name "$Firstname $Lastname" ` -GivenName $Firstname ` -Surname $Lastname ` -Enabled $True ` -ChangePasswordAtLogon $True ` -DisplayName "$Lastname, $Firstname" ` #-Department $Department ` -Path $OU ` -EmailAddress $EmailAddress ` -AccountPassword (convertto-securestring $Password -AsPlainText -Force)
Powershell me sort ouat' mille erreurs, dont le MDP qui serait soi-disant pas assez complexe (ce n'est aps le cas)
PS C:\SCRIPTS> .\add_new_users-from_CSV.ps1 New-ADUser : The password does not meet the length, complexity, or history requirement of the domain. At C:\SCRIPTS\add_new_users-from_CSV.ps1:26 char:15 + New-ADUser ` + ~~~~~~~~~~~~ + CategoryInfo : InvalidData: (CN=Prenomun NOM...domain,DC=com:String) [New-ADUser], ADPasswordComplexityE xception + FullyQualifiedErrorId : ActiveDirectoryServer:1325,Microsoft.ActiveDirectory.Management.Commands.NewADUser
-Path : The term '-Path' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\SCRIPTS\add_new_users-from_CSV.ps1:36 char:13 + -Path $OU ` + ~~~~~ + CategoryInfo : ObjectNotFound: (-Path:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
-AccountPassword : The term '-AccountPassword' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\SCRIPTS\add_new_users-from_CSV.ps1:38 char:13 + -AccountPassword (convertto-securestring $Password -AsPla ... + ~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (-AccountPassword:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
New-ADUser : The password does not meet the length, complexity, or history requirement of the domain. At C:\SCRIPTS\add_new_users-from_CSV.ps1:26 char:15 + New-ADUser ` + ~~~~~~~~~~~~ + CategoryInfo : InvalidData: (CN=Prenomdeux N...crocloud,DC=com:String) [New-ADUser], ADPasswordComplexityE xception + FullyQualifiedErrorId : ActiveDirectoryServer:1325,Microsoft.ActiveDirectory.Management.Commands.NewADUser
-Path : The term '-Path' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\SCRIPTS\add_new_users-from_CSV.ps1:36 char:13 + -Path $OU ` + ~~~~~ + CategoryInfo : ObjectNotFound: (-Path:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
-AccountPassword : The term '-AccountPassword' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\SCRIPTS\add_new_users-from_CSV.ps1:38 char:13 + -AccountPassword (convertto-securestring $Password -AsPla ... + ~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (-AccountPassword:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
Marsh Posté le 06-11-2020 à 16:39:37
Bonjour à tous
Je suis noob de noob en Powershell, et j'essaie de faire un truc qui devrait normalement être assez simple: importer des utilisateurs dans l'Active Directory à partir d'un fichier CSV
Le script Powershell ressemble à ça:
Source: https://blog.netwrix.fr/2018/12/19/ [...] owershell/
#Entrez un chemin d’accès vers votre fichier d’importation CSV
$ADUsers = Import-csv C:\scripts\newusers2.csv
foreach ($User in $ADUsers)
{
$Username = $User.username
$Password = $User.password
$Firstname = $User.firstname
$Lastname = $User.lastname
#$Department = $User.department
$OU = $User.ou
$EmailAddress = $User.emailaddress
#Vérifiez si le compte utilisateur existe déjà dans AD
if (Get-ADUser -F {SamAccountName -eq $Username})
{
#Si l’utilisateur existe, éditez un message d’avertissement
Write-Warning "A user account $Username has already exist in Active Directory."
}
else
{
#Si un utilisateur n’existe pas, créez un nouveau compte utilisateur
#Le compte sera créé dans I’unité d’organisation indiquée dans la variable $OU du fichier CSV ; n’oubliez pas de changer le nom de domaine dans la variable « -UserPrincipalName ».
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName "$Username@domain.com" `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Surname $Lastname `
-Enabled $True `
-ChangePasswordAtLogon $True `
-DisplayName "$Lastname, $Firstname" `
#-Department $Department `
-Path $OU `
-EmailAddress $EmailAddress `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force)
}
}
Le fichier CSV à ça:
firstname,lastname,username,emailaddress,description,Password,OU
Prenomun,NOMUN,pnomun,prenomun.nomun@caca.com,sponsor - caca,PkI8rF15qX1Q1,OU=TEST_UN
Prenomdeux,NOMDEUX,pnomdeux,prenomdeux.nomdeux@prout.com,sponsor - prout,PkI8rF15qX1Q2,OU=TEST_DEUX
Powershell me sort ouat' mille erreurs, dont le MDP qui serait soi-disant pas assez complexe (ce n'est aps le cas)
PS C:\SCRIPTS> .\add_new_users-from_CSV.ps1
New-ADUser : The password does not meet the length, complexity, or history requirement of the domain.
At C:\SCRIPTS\add_new_users-from_CSV.ps1:26 char:15
+ New-ADUser `
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (CN=Prenomun NOM...domain,DC=com:String) [New-ADUser], ADPasswordComplexityE
xception
+ FullyQualifiedErrorId : ActiveDirectoryServer:1325,Microsoft.ActiveDirectory.Management.Commands.NewADUser
-Path : The term '-Path' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\SCRIPTS\add_new_users-from_CSV.ps1:36 char:13
+ -Path $OU `
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (-Path:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
-AccountPassword : The term '-AccountPassword' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\SCRIPTS\add_new_users-from_CSV.ps1:38 char:13
+ -AccountPassword (convertto-securestring $Password -AsPla ...
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (-AccountPassword:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
New-ADUser : The password does not meet the length, complexity, or history requirement of the domain.
At C:\SCRIPTS\add_new_users-from_CSV.ps1:26 char:15
+ New-ADUser `
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (CN=Prenomdeux N...crocloud,DC=com:String) [New-ADUser], ADPasswordComplexityE
xception
+ FullyQualifiedErrorId : ActiveDirectoryServer:1325,Microsoft.ActiveDirectory.Management.Commands.NewADUser
-Path : The term '-Path' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\SCRIPTS\add_new_users-from_CSV.ps1:36 char:13
+ -Path $OU `
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (-Path:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
-AccountPassword : The term '-AccountPassword' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\SCRIPTS\add_new_users-from_CSV.ps1:38 char:13
+ -AccountPassword (convertto-securestring $Password -AsPla ...
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (-AccountPassword:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Quelqu'un pour m'aiguiller un peu ?