Setting demo AD environments

Finally I will always have great AD demo environments with no accounts named TestUser01 or alike. 😉 This is the outcome of the setting up test AD environments discussion we had this week. Darren and Rob suggested a couple of tricks on duplicating AD to a test lab, and xaegr provided a great link to US census information data on the most frequently used names, as well as a sample script I am re-using and enhancing below.

I basically took what xaegr suggested, added other properties to user accounts (first name, last name, city, department), and added code creating global security groups for each department and adding users into the groups. Here goes the code:

################################################
# Script to provision demo AD labs
# (c) Dmitry Sotnikov, xaegr
# Requires AD cmdlets
################################################

# Add AD cmdlets (should be downloaded from 
# http://www.quest.com/activeroles_server/arms.aspx
# and installed on the local workstation
# the script assumes the workstation is a part of the domain

Add-PSSnapin  Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue

# set folder in which the data files are located
# this folder should contain files from
# http://www.census.gov/genealogy/names/names_files.html
# as well as cities.txt and departments.txt with the
# lists of cities and departments for the lab
cd c:\demofiles

# set OU for demo accounts
$OU = "ps64.local/test"
# number of accounts to generate
$num = 100

# read name files
$last = Get-Content dist.all.last | select -First 1000
$firstm = Get-Content dist.male.first | select -First 100
$firstf = Get-Content dist.female.first | select -first 100

# extract the names
$last = $last | where {$_ -match "^(\S+)"}|foreach-object {$matches[1]}
$firstf = $firstf | where {$_ -match "^(\S+)"}|foreach-object {$matches[1]}
$firstm = $firstm | where {$_ -match "^(\S+)"}|foreach-object {$matches[1]}

# read department and city info
$cities = Get-Content Cities.txt
$depts = Get-Content Departments.txt

# set up random number generator
$rnd = New-Object System.Random

function New-RandomADUser {
    # pick a male or a female first name
    if($rnd.next(2) -eq 1) {
        $fn = $firstm[$rnd.next($firstm.length)]
    } else {
        $fn = $firstf[$rnd.next($firstf.length)]
    }
    # random last name
    $ln=$last[$rnd.next($last.length)]

    # Set proper caps
    $ln = $ln[0] + $ln.substring(1, $ln.length - 1).ToLower()
    $fn = $fn[0] + $fn.substring(1, $fn.length - 1).ToLower()

    # random city and department
    $city = $cities[$rnd.next($cities.length)]
    $dept = $depts[$rnd.next($depts.length)]

    # Create and enable a user
    
    if ( ( Get-QADUser -SamAccountName ($fn.substring(0,1) + $ln) ) -eq $null ) {
    
        New-QADUser -Name "$fn $ln" -SamAccountName ($fn.substring(0,1) + $ln) `
                    -ParentContainer $OU -City $city -Department $dept `
                    -UserPassword "P@ssw0rd" -FirstName $fn -LastName $ln `
                    -DisplayName "$fn $ln" -Description "$city $dept" -Office $city `
                    | Enable-QADUser
    }
}

# Create 100 users
1..$num | ForEach-Object { New-RandomADUser }

# Create groups for each department
Get-QADUser -SearchRoot $OU | Group Department | ForEach-Object {
    New-QADGroup -Name $_.Name -SamAccountName $_.Name -ParentContainer $OU
}

# Add users to the groups based on their departments
Get-QADUser -SearchRoot $OU | Add-QADGroupMember -Identity { $_.Department }

The files for names can be found on the census page, the files for cities and departments I was using are attached (note that to increase probability of a certain department or city you just need to duplicate it a few times in the file) as well as the script code:

Let me know if there’s anything else you need for your demo environments!

Tags: , , , ,

6 Responses to “Setting demo AD environments”


  1. 1 Sergey December 25, 2007 at 12:20 pm

    Cool!!!

    That’s almost exactly what we were using in the cscript environment, now it’s in the PowerGUI, that’s awesome!

  2. 2 Peter Kriegel November 28, 2012 at 3:45 pm

    Hello Dimitry!

    thank you very much for this work !

    I cannot download the cities.txt and department.txt files!
    Got message I have to join your Blog but how can I ?

    • 3 Dmitry Sotnikov December 6, 2012 at 6:48 pm

      I’ve just tried both and they opened just fine for me. Just click the links!

      Or create the text files yourself. There’s nothing fancy in them. Just text files with one item (city name or department name) per line.


  1. 1 Митя Сотников: по-русски о PowerShell и не только : Создаем демо-среду Active Directory Trackback on December 14, 2007 at 5:10 pm
  2. 2 Secure Networks » Blog Archive » Script to Set Up Test AD Lab Trackback on March 25, 2010 at 9:46 pm
  3. 3 Populating A Test Lab with AD Objects Using Windows PowerShell « Ted's Tangled Mind Trackback on March 28, 2012 at 12:41 pm

Leave a comment




Legal

The posts on this blog are provided “as is” with no warranties and confer no rights. The opinions expressed on this site are mine and mine alone, and do not necessarily represent those of my employer - WSO2 or anyone else for that matter. All trademarks acknowledged.

© 2007-2014 Dmitry Sotnikov

December 2007
M T W T F S S
 12
3456789
10111213141516
17181920212223
24252627282930
31