Rose, by any random name…

posh-getrand

Mr. Shakespeare might not have had pseudo-random numbers nor huge names lists like those from the U.S. Census Bureau on his mind when he was writing Romeo and Juliet, yet if his “rose by any other name” allows us to generate pseudonyms for test systems, so be it.

Alright, I’ll put it to you straight. This article is about using my good friend Windows PowerShell to generate test data which could be used in a database, directory, spreadsheet, etc. For example, if you’ve ever wanted to generate a hundred test accounts in Active Directory, or 9764 mailboxes in Exchange Server, rather than just having boring names like user1, user2, etc. we can easily generate random names.

1..100 | %{ dsadd user “cn=user$_,ou=Test,dc=777,dc=wernerconsulting,dc=com” }

It’s true that a simple pipeline such as that shown above could easily generate accounts (well, it’s good if they have valid passwords and are enabled, but that’s marginally more complex). Several years ago when I had to generate test accounts, I found some files of sample names from the United States Census Bureau, then wrote a little script to generate user accounts in Active Directory based on pseudo-random combinations of first and last name.

Let’s revisit that now with Windows PowerShell. Assuming that we have downloaded three files of (1) female first names, (2) male first names, and (3) all last names, we could easily pick from the lists to compose fictitious names. Some URLs are at the bottom of this message.

Here’s an example PowerShell script to generate fictitious names.

$global:_RN_female = get-content FemaleFirstNames.txt

$global:_RN_male = get-content MaleFirstNames.txt

$global:_RN_last = get-content LastNames.txt

filter global:Get-RandomName( $percentFemale = 0.50 ){

BEGIN{

$rand = new-object Random

}

PROCESS{

$l = $_RN_last[$rand.Next($_RN_last.Count)].Split(“`t”)[0].ToLower()

if( $rand.NextDouble() -lt $percentFemale ){

$gender = “male”

}

else{

$gender = “female”

}

$f = (Invoke-Expression (“`$_RN_{0}[`$rand.Next(`$_RN_{0}.Count)]” -f $gender)).Split(“`t”)[0].ToLower()

“{0} {1}” -f $f,$l

}

END{

}

}

Once we’ve saved that script to a .ps1 file and then run it, we have the files of first and last names loaded and a function named Get-RandomName defined. Simply invoking this yields one name. Sending a number of objects to it generates a number of names at once. This script could easily be modified to keep the first and last names separate – that will be demonstrated in a later post.

To generate one name, simply use:

Get-RandomName

To generate a list of 100 names, use:

1..100 | Get-RandomName

Next, in a later article, we’ll take a look at how to use these names in the creation of test objects. For now, we’ll end with some URLs to name files, with the understanding that you could use any files with one name per line. Anything on the line after the first <TAB> character is discarded by the script.

<http://www.census.gov/genealogy/names/names_files.html>

<http://www.census.gov/genealogy/www/freqnames2k.html>

Leave a Reply

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