Situatie
Avem de facut n useri in AD, si vrem sa facem totul printr-o singura comanda pentru a nu ne repeta.
Solutie
Avem nevoie de un .csv file in care vom completa 7 coloane si altele optionale: nume initiala prenume username parola ou// optionale: email oras zipcode departament telefon etc.
De exemplu: Niculai G Robert robert.niculai 123456 OU=IT,CN=Domain Admins,CN=Users
Dupa ce am completat, salvam fisierul csv intr-un loc usor accesibil.Mai jos o sa vedem scriptul cu tot ce face:
Import-Module activedirectory
#aici introcuem calea catre csv file
$ADUsers = Import-csv C:\cale\catre\fisier\conturi.csv
foreach ($User in $ADUsers)
{
$Username = $User.username
$Password = $User.password
$Firstname = $User.firstname
$Lastname = $User.lastname
$OU = $User.ou
$email = $User.email
$streetaddress = $User.streetaddress
$city = $User.city
$zipcode = $User.zipcode
$state = $User.state
$country = $User.country
$telephone = $User.telephone
$jobtitle = $User.jobtitle
$company = $User.company
$department = $User.department
$Password = $User.Password
if (Get-ADUser -F {SamAccountName -eq $Username})
{
#daca userul exista scriptul ne atentioneaza
Write-Warning “A user account with username $Username already exist in Active Directory.”
}
else
{
#daca nu exista, scriptul continua normal
New-ADUser `
-SamAccountName $Username `
#aici se trece domeniul
-UserPrincipalName “$Username@classit.local” `
-Name “$Firstname $Lastname” `
-GivenName $Firstname `
-Surname $Lastname `
-Enabled $True `
-DisplayName “$Lastname, $Firstname” `
-Path $OU `
-City $city `
-Company $company `
-State $state `
-StreetAddress $streetaddress `
-OfficePhone $telephone `
-EmailAddress $email `
-Title $jobtitle `
-Department $department `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $True
#^putem modifica in false daca vrem sa bifam sau nu password change
}
}
Scriptul de sus se ruleaza in powershell.Daca avem alt domeniu sau alta cale, se modifica linia 41 (ad) si 4(calea fisierului).
Leave A Comment?