Создание пользователей Active Directory через CSV файл
Иногда бывает нужно создать в Active Directory кучу пользователей.
Создавать все это добро вручную долго и муторно, а если для каждой группы пользователей нужна своя OU? Или названия компаний для каждого свое?
Ниже я покажу скрипт, который создает пользователей из CSV файла.
Алгоритм следующий:
1) Создаем OU с названием компании пользователя
2) Создаем группы для пользователя и включаем его в них (мы создадим глобальную группу и универсальную)
3) Переводим названия созданных групп в AD в транслит
4) Добавляем пользователей в другие (общие) группы AD
Шапка CSV файла будет такой:
1 |
Lastname;Firstname;Login;Password;Position;Company;Number |
Сам скрипт:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
#Задаем переменные $CSVpatch = "C:\PowerShell_Scripts\365.csv" $CSVcopy = "C:\PowerShell_Scripts\36502.csv" $CSVwithADgroups = "C:\PowerShell_Scripts\365ADG.csv" $TMPcompany = "C:\PowerShell_Scripts\temp.txt" $TMPListcompany = "C:\PowerShell_Scripts\TMPListcompany.txt" $TMPListcompanyreplased = "C:\PowerShell_Scripts\TMPListcompanyreplased.txt" $TMPcompanyTranslit = "C:\PowerShell_Scripts\translit.txt" $TMPcompanyTranslitCSV = "C:\PowerShell_Scripts\translit.csv" $TMPfile = "C:\PowerShell_Scripts\tmpfile.txt" $AllinoneCSV = "C:\PowerShell_Scripts\allinonetranslit.csv" # Создаем новый OU по названием компаний Import-Csv $CSVpatch -Delimiter ";" | % { $Company = $_.Company; # Set the user NEW-ADOrganizationalUnit "$Company" –path "OU=Users,OU=Office365_Sync,DC=test,DC=local" } # Удаляем временные файлы remove-item $CSVcopy remove-item $CSVwithADgroups remove-item $TMPcompany remove-item $TMPListcompany remove-item $TMPListcompanyreplased remove-item $TMPcompanyTranslit remove-item $TMPcompanyTranslitCSV remove-item $AllinoneCSV # Обрабатываем и преобразуем названия компаний (копируем столбец с компаниями и удаляем пробелы) Import-Csv $CSVpatch -Delimiter ";" | % { $Company = $_.Company; # Set the user $Company >> $TMPcompany } $hash = @{} # define a new empty hash table gc $TMPcompany | % { if ($hash.$_ -eq $null) { $_ }; $hash.$_ = 1 } > $TMPfile #$TMPListcompany (Get-Content $TMPcompany) -replace " ","-" | Set-Content $TMPListcompanyreplased # Преобразуем в транслит название компаний remove-item $TMPcompanyTranslit function global:Translit { param([string]$inString) $Translit = @{ [char]'а' = "a" [char]'А' = "A" [char]'б' = "b" [char]'Б' = "B" [char]'в' = "v" [char]'В' = "V" [char]'г' = "g" [char]'Г' = "G" [char]'д' = "d" [char]'Д' = "D" [char]'е' = "e" [char]'Е' = "E" [char]'ё' = "yo" [char]'Ё' = "Yo" [char]'ж' = "zh" [char]'Ж' = "Zh" [char]'з' = "z" [char]'З' = "Z" [char]'и' = "i" [char]'И' = "I" [char]'й' = "j" [char]'Й' = "J" [char]'к' = "k" [char]'К' = "K" [char]'л' = "l" [char]'Л' = "L" [char]'м' = "m" [char]'М' = "M" [char]'н' = "n" [char]'Н' = "N" [char]'о' = "o" [char]'О' = "O" [char]'п' = "p" [char]'П' = "P" [char]'р' = "r" [char]'Р' = "R" [char]'с' = "s" [char]'С' = "S" [char]'т' = "t" [char]'Т' = "T" [char]'у' = "u" [char]'У' = "U" [char]'ф' = "f" [char]'Ф' = "F" [char]'х' = "h" [char]'Х' = "H" [char]'ц' = "c" [char]'Ц' = "C" [char]'ч' = "ch" [char]'Ч' = "Ch" [char]'ш' = "sh" [char]'Ш' = "Sh" [char]'щ' = "sch" [char]'Щ' = "Sch" [char]'ъ' = "" [char]'Ъ' = "" [char]'ы' = "y" [char]'Ы' = "Y" [char]'ь' = "" [char]'Ь' = "" [char]'э' = "e" [char]'Э' = "E" [char]'ю' = "yu" [char]'Ю' = "Yu" [char]'я' = "ya" [char]'Я' = "Ya" } $outCHR="" foreach ($CHR in $inCHR = $inString.ToCharArray()) { if ($Translit[$CHR] -cne $Null ) {$outCHR += $Translit[$CHR]} else {$outCHR += $CHR} } Write-Output $outCHR } Get-Content $TMPListcompanyreplased | ForEach-Object {Translit($_)} > $TMPcompanyTranslit ##Создаем группы в AD Copy-Item $CSVpatch $CSVcopy function Insert-Content { param ( [String]$Path ) process { $( ,$_; Get-Content $Path -ea SilentlyContinue) | Out-File $Path } } 'ADgroup' | Insert-Content $TMPcompanyTranslit import-csv $TMPcompanyTranslit -delimiter ';' | export-csv -NoTypeInformation -UseCulture $TMPcompanyTranslitCSV (Get-Content $TMPcompanyTranslitCSV) -replace '"' | Set-Content $TMPcompanyTranslitCSV $csv1 = @(gc $CSVcopy) $csv2 = @(gc $TMPcompanyTranslitCSV) $csv3 = @() for ($i=0; $i -lt $csv1.Count; $i++) { $csv3 += $csv1[$i] + ';' + $csv2[$i] } $csv3 | Out-File $AllinoneCSV Import-Csv $AllinoneCSV -Delimiter ";"| % { $Company = $_.Company; # Set the user $OU = "OU=$Company,OU=test,OU=Office365_Sync,DC=test,DC=local" $ADgroup = $_.ADgroup; # Set the user #echo $OU #echo $ADgroup } clear Import-Csv $AllinoneCSV -Delimiter ";"| % { $Firstname = $_.Firstname; # Set the user $Lastname = $_.Lastname; # Set the user $Login = $_.Login; # Set the user $Password = $_.Password; # Set the user $Position = $_.Position; # Set the user $Company = $_.Company; # Set the user $Number = $_.Number; # Set the user $OU = "OU=$Company,OU=test,OU=Office365_Sync,DC=test,DC=local" $ADgroup = $_.ADgroup; # Set the user echo $ADgroup $Displayname = "$Lastname $Firstname" New-ADUser -SamAccountName "$Login" -name "$Displayname" -GivenName "$Firstname" -Surname "$Lastname" -PasswordNeverExpires $true -UserPrincipalName "$Login@domain.ru" -DisplayName "$Displayname" -EmailAddress "$Login@domain.ru" -Title "$Position" -enable $True -AccountPassword (ConvertTo-SecureString -AsPlainText "$Password" -Force) -Path "$OU" -company "$Company" -OtherAttributes @{'preferredLanguage'="RU"} New-ADGroup -SamAccountName "GD-$ADgroup" -name "GD-$ADgroup" -GroupScope 1 -GroupCategory security -description "$Company" -OtherAttributes @{'info'="domain.ru"} -Path "$OU" Set-ADGroup "GD-$ADgroup" -Replace @{mail="GD-$ADgroup@domain.ru"} New-ADGroup -SamAccountName "$ADgroup" -name "$ADgroup" -GroupScope 2 -GroupCategory distribution -description "$ADgroup"-OtherAttributes @{'mail'="$ADgroup@domain.ru"} -Path "$OU" } Start-Sleep -Seconds 20 clear Import-Csv $AllinoneCSV -Delimiter ";"| % { $Company = $_.Company; # Set the user $OU = "OU=$Company,OU=test,OU=Office365_Sync,DC=test,DC=local" $OU $users = Get-ADUser -Filter * -SearchBase "$OU" #$users $groups1 = Get-ADGroup -Filter * -SearchBase "$OU" | Where-Object {$_.SamAccountName -notlike "GD-*"} $groups2 = Get-ADGroup -Filter * -SearchBase "$OU" | Where-Object {$_.SamAccountName -like "GD-*"} Add-ADGroupMember "$groups1" -Member $users Add-ADGroupMember "$groups2" -Member $users Add-ADGroupMember "GD-TS-Users-Termsrv05-Region" -Member $users Add-ADGroupMember "GD-All-test-Users" -Member $users } remove-item $CSVcopy remove-item $CSVwithADgroups remove-item $TMPcompany remove-item $TMPListcompany remove-item $TMPListcompanyreplased remove-item $TMPcompanyTranslit remove-item $TMPcompanyTranslitCSV remove-item $AllinoneCSV remove-item $TMPfile |
Если вам нужно просто создать много учетных записей в Active Directory, то вам подойдет следующий скрипт:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#Задаем переменные $CSVpatch = "C:\PowerShell_Scripts\365.csv" Import-Csv $CSVpatch -Delimiter ";" | % { $Firstname = $_.Firstname; # Set the user $Lastname = $_.Lastname; # Set the user $Login = $_.Login; # Set the user $Password = $_.Password; # Set the user $Position = $_.Position; # Set the user $Company = $_.Company; # Set the user $Number = $_.Number; # Set the user $Displayname = "$Lastname $Firstname" New-ADUser -SamAccountName "$Login" -name "$Displayname" -GivenName "$Firstname" -Surname "$Lastname" -PasswordNeverExpires $true -UserPrincipalName "$Login@domain.local" -DisplayName "$Displayname" -EmailAddress "$Login@domain.ru" -Title "$Position" -enable $True -AccountPassword (ConvertTo-SecureString -AsPlainText "$Password" -Force) -company "$Company" } |
Комментарии