Fill Active Directory with test users for SharePoint

Original author: Wictor Wilen
  • Transfer
When you test SharePoint or any other products that use Active Directory, it is very important to use large amounts of data that are close to real. For example, if you have 10 or 20 test users in Active Directory, and possibly 50 or 100 with names like Test 1, Test 2, etc., this is clearly not enough.

Consider how you can fill Active Directory with a large (over 9000) amount of data that is as close to reality as possible.


We get the data


To generate user data, we will use the Fake Name Generator service . This wonderful service has the ability to generate up to 50,000 users at a time . In this case, you can select the country and user properties included in the export file.

Having selected all the properties, you will receive a file with all the data by e-mail.

Import data


Consider importing data from a previously received CSV file using PowerShell. We will slightly modify the initial data, and then create accounts in Active Directory based on them.

First, create OU ("Demo Users"), where we will place all new users and set a policy for passwords

Import-Module ActiveDirectory
$dn = (Get-ADDomain).DistinguishedName
$forest = (Get-ADDomain).Forest
Set-ADDefaultDomainPasswordPolicy $forest -ComplexityEnabled $false -MaxPasswordAge "1000" -PasswordHistoryCount 0 -MinPasswordAge 0
$ou = Get-ADOrganizationalUnit -Filter 'name -eq "Demo Users"'
if ($ou -eq $null) {
    New-ADOrganizationalUnit -Name "Demo Users" -Path $dn
    $ou = Get-ADOrganizationalUnit -Filter 'name -eq "Demo Users"'
}


Import a CSV file into PowerShell

$data = Import-Csv .\<Имя_Вашего_Файла>.csv


We will now place our data in a new PowerShell object. Please note that this object uses the parameter names of the New-ADUser cmdlet , so if you want to add attributes to your accounts, you need to do it here

$refineddata = $data | select  @{Name="Name";Expression={$_.Surname + ", " + $_.GivenName}},`
    @{Name="SamAccountName"; Expression={$_.Username}},`
    @{Name="UserPrincipalName"; Expression={$_.Username +"@" + $forest}},`
    @{Name="GivenName"; Expression={$_.GivenName}},`
    @{Name="Surname"; Expression={$_.Surname}},`
    @{Name="DisplayName"; Expression={$_.Surname + ", " + $_.GivenName}},`
    @{Name="City"; Expression={$_.City}},`
    @{Name="StreetAddress"; Expression={$_.StreetAddress}},`
    @{Name="State"; Expression={$_.State}},`
    @{Name="Country"; Expression={$_.Country}},`
    @{Name="PostalCode"; Expression={$_.ZipCode}},`
    @{Name="EmailAddress"; Expression={$_.EmailAddress}},`
    @{Name="AccountPassword"; Expression={ (Convertto-SecureString -Force -AsPlainText "WictorRocks!")}},`
    @{Name="OfficePhone"; Expression={$_.TelephoneNumber}},`
    @{Name="Title"; Expression={$_.Occupation}},`
    @{Name="Enabled"; Expression={$true}},`
    @{Name="PasswordNeverExpires"; Expression={$true}}


Note that in the script above, the Name and DisplayName properties are corrected, and UPN uses the DNS name of the forest. In addition, all users are active.

Time to add users to Active Directory! But we will not add them to the OU that we created earlier. Instead, create OUs for user countries. This allows you to better manage test data and test work in different OUs.

$refineddata | % {
    $subou = Get-ADOrganizationalUnit -Filter "name -eq ""$($_.Country)""" -SearchBase $ou.DistinguishedName
    if($subou -eq $null) {
        New-ADOrganizationalUnit -Name $_.Country -Path $ou.DistinguishedName
        $subou = Get-ADOrganizationalUnit -Filter "name -eq ""$($_.Country)""" -SearchBase $ou.DistinguishedName
    }
    $_ | Select @{Name="Path"; Expression={$subou.DistinguishedName}},* | New-ADUser
}


If you have any errors while creating users, most likely they are due to the fact that some of them have the same usernames.

Result


Go to the Active Directory management console and see what happened.



If you select any OU, you will see a lot of users there.



In this case, the user profile will be filled



Conclusion


You looked at how quickly and easily you can populate your Active Directory for test scripts.

It’s important that in the case of SharePoint, creating an unnecessarily large number of accounts can be expensive because of the costs of synchronizing and indexing them.

Also popular now: