Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Import-Module ActiveDirectory
- # CSV Location
- $filepath = "Path\to\NewUser.csv"
- # CSV-Datei in Variable speichern
- $users = Import-Csv $filepath
- # Schleife durchlaufen und alle Benutzer erstellen
- ForEach ($user in $users) {
- # Nutzer-Informationen in Variablen speichern
- $vorname = $user.'Vorname'
- $vornameFirstChar=$vorname.SubString(0,1) # 1. Buchstabe für Anmeldename und E-Mail
- $nachname = $user.'Nachname'
- $jobtitel = $user.'Jobtitel'
- # Passwort generieren
- function Get-RandomCharacters($length, $characters) {
- $random = 1..$length | ForEach-Object { Get-Random -Maximum $characters.length }
- $private:ofs=""
- return [String]$characters[$random]
- }
- $PasswortLesbar = Get-RandomCharacters -length 4 -characters 'abcdefklmnoprtuvwxyz'
- $PasswortLesbar += Get-RandomCharacters -length 2 -characters 'ABCDEFGHKMNPRTUVWXYZ'
- $PasswortLesbar += Get-RandomCharacters -length 1 -characters '123456789'
- $PasswortLesbar += Get-RandomCharacters -length 1 -characters '!"§$%&=?@#*+'
- $Passwort = ConvertTo-SecureString $PasswortLesbar -AsPlainText -Force
- # AD-User erstellen
- New-ADUser -Name "$vornameFirstChar.$nachname" -GivenName $vorname -Surname $nachname -UserPrincipalName "$vorname.$nachname" -EmailAddress "$vornameFirstChar.$nachname@DOMAIN.TLD" -AccountPassword $Passwort -ChangePasswordAtLogon $True -Enabled $True
- # Verzeichnisse erstellen
- New-Item -ItemType directory -Path C:\Public\$vornameFirstChar.$nachname
- New-Item -ItemType directory -Path C:\Home\$vornameFirstChar.$nachname
- # Berechtigungen für Home-Laufwerk setzen
- $Acl = Get-Acl C:\Home\$vornameFirstChar.$nachname
- $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule("$vornameFirstChar.$nachname", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
- $Acl.SetAccessRule($Ar)
- Set-Acl C:\Home\$vornameFirstChar.$nachname $Acl
- # Erfolgsmeldung ausgeben
- echo ""
- echo ""
- echo "Account für $vorname $nachname wurde erstellt! Passwort: $PasswortLesbar"
- echo ""
- }
- Pause
Add Comment
Please, Sign In to add comment