Загрузка данных


$csvPath = "users.csv"
$logPath = "errors.log"
$ouPath = "OU=Users,DC=domain,DC=local"

if (Test-Path $logPath) { Remove-Item $logPath }

Import-Module ActiveDirectory
$users = Import-Csv $csvPath
$created = 0
$skipped = 0

foreach ($user in $users) {
    try {
        if (Get-ADUser -Filter "SamAccountName -eq '$($user.Login)'" -ErrorAction SilentlyContinue) {
            throw "Пользователь уже существует"
        }
        
        $securePass = ConvertTo-SecureString $user.Password -AsPlainText -Force
        $nameParts = $user.FullName -split ' '
        $firstName = $nameParts[1]
        $lastName = $nameParts[0]
        
        New-ADUser -Name "$firstName $lastName" -SamAccountName $user.Login -UserPrincipalName "$($user.Login)@domain.local" -GivenName $firstName -Surname $lastName -Enabled $true -AccountPassword $securePass -Path $ouPath -PassThru | Set-ADUser -ChangePasswordAtLogon $true
        $created++
    }
    catch {
        $skipped++
        "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') - $($user.Login) - $($_.Exception.Message)" | Out-File $logPath -Append
    }
}

Write-Host "Создано пользователей: $created"
Write-Host "Пропущено (ошибки): $skipped"
if ($skipped -gt 0) { Write-Host "См. errors.log" }