![]()
![]()
I created a Create Lab AD script a while back.
The script is now updated with some fixes.
The script got error messages sometimes, but created users
anyway. With wrong samaccountnames, but created users ![]()
I looked at the text files with names and thought at the first that It was empty lines
that was the bad thing. So I did a easy fix for that, but got the errors anyway.
After some tests and looking to the files I understood that the problem was when the names
was shorter then 3, so I did a fix for that too.
I have been posted the script in pieces before.
http://poweradmin.se/blog/2009/12/15/create-lab-ad-functions-updated/
http://poweradmin.se/blog/2009/12/16/create-lab-ad-continue/
http://poweradmin.se/blog/2009/12/17/create-lab-ad-part-3/
http://poweradmin.se/blog/2009/12/17/create-lab-ad-part-4/
http://poweradmin.se/blog/2009/12/21/create-lab-ad-part-5/
http://poweradmin.se/blog/2009/12/21/create-lab-ad-part-5-changed/
http://poweradmin.se/blog/2009/12/21/create-lab-ad-part-6/
http://poweradmin.se/blog/2009/12/22/lab-ad-with-5000-users/
http://poweradmin.se/blog/2009/12/22/the-story-continues/
I thought that I had posted the whole script, but It seems like I did not…
…the first half of last year was not good in my private life, so I have missed a lot in my PowerShelling ![]()
BUT this year will be a really good one instead ![]()
The Story, short version
The script will download first names (1000) and last names (1000) from a site in Sweden.
Then It will clean up the file, use it to create 1-5000 AD users with random names in a Lab OU with the choice of OUs, number of
letters from first name and last name for the samaccountname and more.
The whole script:
cls # Populate LAB AD with "Real" users and OUs # Number Of Users to create $nrOfUsers = 5000 # Number of Letters from Firstname in User Name $lettersUNamefName = 3 # Number of Letters from Lastname in User Name $lettersUNamelName = 3 # Name of the AD # Default is the domain where your user are. $activeLabDomain = new-object DirectoryServices.DirectoryEntry $labDomain = $activeLabDomain.distinguishedName # LAB OU $labOU = "Lab OU" # OUs to create $labOUs = "Finance", "IT", "Marketing", "Operations", "Service", "Customer Support" # OU structure under each OU $OUStructure = "Users", "Groups", "Computers","Service Accounts", "Admins" # Country $userCountry = "Sweden", "Norway", "Denmark", "Finland" # Path to the file with Firstnames $fNameFile = "c:\scripts\fname.txt.tmp" $fNameFileFixed = "c:\scripts\fname.txt" # Path to the file with Lastnames $lNameFile = "c:\scripts\lname.txt.tmp" $lNameFileFixed = "c:\scripts\lname.txt" function getFNames { # Get a list of Swedish Firstnames from Gothenburg University param ($fNameFile) if(!(test-path -path $fNameFile)){ if ($WebClient -eq $null) {$Global:WebClient=new-object System.Net.WebClient } $url="http://spraakbanken.gu.se/statistik/lbfnamn.phtml" $fNames = $WebClient.DownloadString($url) -replace "[0-9]","" -replace "<td>","" -replace "<tr>","" -replace "</td>","" -replace "</tr>","" -replace "<b>","" -replace "</b>","" -replace "<td class=""le"">","" -replace "<table>","" -replace "</table>","" -replace "<br />","" -replace "<hr />","" -replace "<body>","" -replace "</body>","" -replace "<html>","" -replace "</html>","" -replace "<table width=""%""><td width=""%"">","" -replace "<td class=""lastupd"">Senaste ändring <b class=""date""> okt av lbtekn@svenska.gu.se</a>.","" -replace "<!doctype html public ""-//wc//dtd html . transitional//en"">","" -replace "<head>","" -replace "</head>","" -replace "<h>","" -replace "</h>","" -replace "<title>","" -replace "</title>","" -replace "<tr class=""headrow"">","" -replace " <meta http-equiv=""Content-Type"" content=""text/html; charset=iso--"">","" -replace " <link rel=""STYLESHEET"" href=""/css/statistik.css"" type=""text/css"">","" -replace "Vanliga förnamn, ordnade efter frekvens.","" -replace " Rang Pojknamn Frekvens Rang Flicknamn Frekvens","" -replace "\s{3,}", " " $fNames = $fNames -split (" ") foreach ($fName in $fNames) { $fName | Out-File $fNameFile -append } } } function getLNames { # Get a list of Swedish Lastnames from Gothenburg University param ($lNameFile) if(!(test-path -path $lNameFile)){ if ($WebClient -eq $null) {$Global:WebClient=new-object System.Net.WebClient } $url="http://spraakbanken.gu.se/statistik/lbenamn.phtml" $lNames = $WebClient.DownloadString($url) -replace "[0-9]","" -replace "<td>","" -replace "<tr>","" -replace "</td>","" -replace "</tr>","" -replace "<b>","" -replace "</b>","" -replace "<td class=""le"">","" -replace "<table>","" -replace "</table>","" -replace "<br />","" -replace "<hr />","" -replace "<body>","" -replace "</body>","" -replace "<html>","" -replace "</html>","" -replace "<table width=""%""><td width=""%"">","" -replace "<td class=""lastupd"">Senaste ändring <b class=""date""> okt av lbtekn@svenska.gu.se</a>.","" -replace "<!doctype html public ""-//wc//dtd html . transitional//en"">","" -replace "<head>","" -replace "</head>","" -replace "<h>","" -replace "</h>","" -replace "<title>","" -replace "</title>","" -replace "<tr class=""headrow"">","" -replace " <meta http-equiv=""Content-Type"" content=""text/html; charset=iso--"">","" -replace " <link rel=""STYLESHEET"" href=""/css/statistik.css"" type=""text/css"">","" -replace "Vanliga förnamn, ordnade efter frekvens.","" -replace " Rang Pojknamn Frekvens Rang Flicknamn Frekvens","" -replace "\s{3,}", " " -replace " efternamn,frekvensordning Vanliga efternamn, i frekvensordning Rang Namn Frekvens ","" $lNames = $lNames -split (" ") foreach ($lName in $lNames) { $lName | where {$_.length -gt "2"} | Out-File $lNameFile -append } } } # Creating the "root" Lab OU $search = [System.DirectoryServices.DirectorySearcher]"[ADSI]LDAP://$labDomain" $search.Filter = "(&(name=$labOU)(objectCategory=organizationalunit))" $result = $search.FindOne() if ($result -eq $null) { $labADSIDomain = [ADSI]"LDAP://$labDomain" $objOU = $labADSIDomain.Create("OrganizationalUnit", "ou=" + $labOU) $objOU.SetInfo() Write-Host $labOU "created" } else { Write-Host $labOU "exists" } # Creating all OUs in the Lab OU $labDomainOU = [ADSI]"LDAP://ou=$labOU,$labDomain" foreach ($labUnit in $labOUs) { $search = [System.DirectoryServices.DirectorySearcher]$labDomainOU $search.Filter = "(&(name=$labUnit)(objectCategory=organizationalunit))" $result = $search.FindOne() if ($result -eq $null) { $objOU = $labDomainOU.Create("OrganizationalUnit", "ou=" + $labUnit) $objOU.SetInfo() Write-Host $labUnit "created" } else { Write-Host $labUnit "exists" } } getFNames $fNameFile getLNames $lNameFile gc $fNameFile | where {$_ -ne ""} > $fNameFileFixed gc $lNameFile | where {$_ -ne ""} > $lNameFileFixed del $fNameFile del $lNameFile for($i = 0;$i -lt $nrOfUsers ;$i++) { $firstName = Get-Content $fNameFileFixed | Get-Random -ErrorAction SilentlyContinue $lastName = Get-Content $lNameFileFixed | Get-Random -ErrorAction SilentlyContinue $mylabOUs = $labOUs | Get-Random $userFirstName = $firstName -creplace('å','a') -creplace('ä','a') -creplace('ö','o') -creplace('Å','A') -creplace('Ä','A') -creplace('Ö','O') -creplace('ü','u') -creplace('Ü','U') -creplace('é','e') -creplace('É','E') $userFirstName = $userFirstName.ToLower() $userShortFirstName = $userFirstName.Substring(0,$lettersUNamefName) $userLastName = $lastName -creplace('å','a') -creplace('ä','a') -creplace('ö','o') -creplace('Å','A') -creplace('Ä','A') -creplace('Ö','O') -creplace('ü','u') -creplace('Ü','U') -creplace('é','e') -creplace('É','E') $userLastName = $userLastName.ToLower() $userShortLastName = $userLastName.Substring(0,$lettersUNamelName) $userNumber = Get-Random -Minimum 10000 -Maximum 99999 $userSAM = $userShortFirstName + $userShortLastName + $userNumber $userLastTele = Get-Random -Minimum 1000 -Maximum 9999 $userTele = "+468-440 " + $userLastTele $myUserCountry = $userCountry | Get-Random switch ($myUserCountry) { 'Sweden' {$userCoutryCode = "se"} 'Denmark' {$userCoutryCode = "dk"} 'Finland' {$userCoutryCode = "fi"} 'Norway' {$userCoutryCode = "no"} default {$userCoutryCode = "local"} } $userEmail = $userFirstName + "." + $userLastName + "@poweradmin." + $userCoutryCode $userDescription = $firstName + " " + $lastName + " at " + $mylabOUs + " in " + $myUserCountry $userPrincipalName = $userFirstName + "." + $userLastName + "@poweradmin.local" $displayName = $LastName + ", " + $FirstName # Creating the User $objOU = new-object DirectoryServices.DirectoryEntry("LDAP://OU=$myLabOUs,OU=$labOU," + $labDomain) $objUser = $objOU.Create("user", "cn=$FirstName $LastName") $objUser.Put("sAMAccountName", $userSAM) $objUser.Put("userPrincipalName",$userPrincipalName) $objUser.Put("displayName",$displayName) $objUser.put("mail", $userEmail) $objUser.put("department", $myLabOUs) $objUser.put("company","Power Admin Corp") $objUser.put("employeeNumber", $userNumber) $objUser.put("telephoneNumber", $userTele) $objUser.put("wWWHomePage", "http://www.poweradmin.se/blog") $objUser.SetInfo() Write-Host "Created - " $firstName $lastName "($userSAM) in" $mylabOUs $objUser.Put("givenName", $firstName) $objUser.Put("sn", $lastName) $objUser.Put("description", $userDescription) $objUser.SetInfo() # Password $objUser.psbase.invoke("setpassword", "myH@rdP@ssw0rd99!") $objUser.SetInfo() # Enable the account $objUser.psbase.invokeset('accountdisabled', $false) $objUser.SetInfo() # Change password at next login $objUser.PwdLastSet = 0 $objUser.Setinfo() }
The script is as allways provided as is and you should test this in a lab environment…
Pingback: Fredrik Wall
Pingback: ShayLevy
Pingback: Tweets that mention Create Lab AD – Updated version | Dalle & DXter -- Topsy.com
Pingback: Grant Johnson
Nice work, using native M$ .NET assemblies and no extended PSsnapins, I Like holla back /XKDS
THANKS! as soon as I figured out that I needed to run this from c:\scripts it worked like a charm.
quick question. is there a way to get the UPN to populate? When I import the users into Exchange 2007 this causes issues.
Thanks Again.