Create Lab AD Functions (Updated)

I wrote a script some years back in VBScript that made
Active Directory Users with “Real Swedish” names.

It’s took Firstname from one text file and Lastname from
another text file, randomly. And made random Passwords as well.

So instead of getting users like “user0001” and “user998” I got
”Anders Svensson” and “Lena Persson”. Prettys nice.

The bad part about the VBScript was that It took 10-15 min to clean
the textfiles first.

I’m a computer geek and I do lots of labs with AD so I wanted to
make a similar thing with PowerShell.

After some work I have made 2 functions that will get two
lists of common First and Last names from The Gothenburg University.

The first function are:

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
    }
    }
}
You will call It like this:$fNameFile = "c:\scripts\fname.txt"getFNames $fNameFile

or simply

getFNames 'c:\scripts\fname.txt'

First of all It will look for the file with all the first names.
If It can’t find It, It will download It from the server at
Gothenburg University and clean It.

Now You Got a text file with lots of names (1000).

And the other function are:

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 | Out-File $lNameFile -append
    }
    }
}

 

And this one will give you the same as the other function.

You will call It like this:$lNameFile = "c:\scripts\lname.txt"getLNames $lNameFile

or simply

getLNames 'c:\scripts\lname.txt'

Now you got 1000 first names and 1000 last names to make random names of.

    $firstName = Get-Content $fNameFile | Get-Random -ErrorAction SilentlyContinue
    $lastName = Get-Content $lNameFile | Get-Random -ErrorAction SilentlyContinue

$firstName + + $lastName

I will post more of my script tommorow. This was todays function gift from me :)

 

No related posts.

This entry was posted in PowerShell Blogs and tagged , , . Bookmark the permalink.

4 Responses to Create Lab AD Functions (Updated)

  1. Fredrik Wall says:

    2 functions to create "real" names, http://bit.ly/5hMhNj #PowerShell

  2. Dick Svensson says:

    Nice work dalle!

  3. Fredrik Wall says:

    Thanks!

    I hope that you will like the rest of my post regarding to setup a Lab AD. They will be posted every day from now on.

    It’s pretty nice to create a new lab AD with “real” names, OUs, addresses etc in 2 min or so :)

    And the script can be applyed to other tasks as well.

  4. Pingback: Create Lab AD – Updated version | Dalle & DXter

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>