forked from RobBridgeman/ADImporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateDemoUsers.ps1
190 lines (160 loc) · 8.23 KB
/
CreateDemoUsers.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#Credit for original script to Helge Klein https://helgeklein.com.
#Adapted to allow higher numbers of users with the same information set.
# Summary of changes.
# Reduced Male and Female names into one list for ease of expansion
# Changed Displayname code to create each combination of names possible
# Changed sAMAccountname generation to add unique account ID with orgShortName as suffix.
# Known issues
# Usercount (For me anyway) seems to be inaccurate when import completes. May be related to errorcheck compensation when usercount is reduced. Consistently seem to get many more users that intended.
Set-StrictMode -Version 2
$DebugPreference = "SilentlyContinue" # SilentlyContinue | Continue
Import-Module ActiveDirectory
# Set the working directory to the script's directory
Push-Location (Split-Path ($MyInvocation.MyCommand.Path))
#
# Global variables
#
# User properties
$ou = "OU=YourOUHere,DC=AC,DC=Local" # Which OU to create the user in
$initialPassword = "Password1" # Initial password set for the user
$orgShortName = "AC" # This is used to build a user's sAMAccountName
$dnsDomain = "AC.local" # Domain is used for e-mail address and UPN
$company = "AC co" # Used for the user object's company attribute
$departments = ( # Departments and associated job titles to assign to the users
@{"Name" = "Finance & Accounting"; Positions = ("Manager", "Accountant", "Data Entry")},
@{"Name" = "Human Resources"; Positions = ("Manager", "Administrator", "Officer", "Coordinator")},
@{"Name" = "Sales"; Positions = ("Manager", "Representative", "Consultant")},
@{"Name" = "Marketing"; Positions = ("Manager", "Coordinator", "Assistant", "Specialist")},
@{"Name" = "Engineering"; Positions = ("Manager", "Engineer", "Scientist")},
@{"Name" = "Consulting"; Positions = ("Manager", "Consultant")},
@{"Name" = "IT"; Positions = ("Manager", "Engineer", "Technician")},
@{"Name" = "Planning"; Positions = ("Manager", "Engineer")},
@{"Name" = "Contracts"; Positions = ("Manager", "Coordinator", "Clerk")},
@{"Name" = "Purchasing"; Positions = ("Manager", "Coordinator", "Clerk", "Purchaser")}
)
[System.Collections.ArrayList]$phoneCountryCodes = @{"NL" = "+31"; "GB" = "+44"; "DE" = "+49"} # Country codes for the countries used in the address file
# Other parameters
$userCount = 5000 # How many users to create
$locationCount = 2 # How many different offices locations to use counting from 0, where 0 is 1
# Files used
$firstNameFile = "Firstnames.txt" # Format: FirstName
$lastNameFile = "Lastnames.txt" # Format: LastName
$addressFile = "Addresses.txt" # Format: City,Street,State,PostalCode,Country
$postalAreaFile = "PostalAreaCode.txt" # Format: PostalCode,PhoneAreaCode
# Check locationCount before importing Files else it chokes when it's set too high
if ($locationCount -ge $phoneCountryCodes.Count) {Write-Error ("ERROR: selected locationCount is higher than configured phoneCountryCodes2. You may want to configure $($phoneCountryCodes.Count-1) as max locationCount");continue}
#
# Read input files
#
$firstNames = Import-CSV $firstNameFile -Encoding utf7 # This will remove some "illegal" characters from the names as those characters are not displayed properly (in WS2012R2)
$lastNames = Import-CSV $lastNameFile -Encoding utf7 # This will remove some "illegal" characters from the names as those characters are not displayed properly (in WS2012R2)
$addresses = Import-CSV $addressFile -Encoding utf7 # This will remove some "illegal" characters from the names as those characters are not displayed properly (in WS2012R2)
$postalAreaCodesTemp = Import-CSV $postalAreaFile
# Convert the postal & phone area code object list into a hash
$postalAreaCodes = @{}
foreach ($row in $postalAreaCodesTemp)
{
$postalAreaCodes[$row.PostalCode] = $row.PhoneAreaCode
}
$postalAreaCodesTemp = $null
#
# Preparation
#
$securePassword = ConvertTo-SecureString -AsPlainText $initialPassword -Force
# Select the configured number of locations from the address list
$locations = @()
$addressIndexesUsed = @()
for ($i = 0; $i -le $locationCount; $i++)
{
# Determine a random address
$addressIndex = -1
do
{
$addressIndex = Get-Random -Minimum 0 -Maximum $addresses.Count
} while ($addressIndexesUsed -contains $addressIndex)
# Store the address in a location variable
$street = $addresses[$addressIndex].Street
$city = $addresses[$addressIndex].City
$state = $addresses[$addressIndex].State
$postalCode = $addresses[$addressIndex].PostalCode
$country = $addresses[$addressIndex].Country
$locations += @{"Street" = $street; "City" = $city; "State" = $state; "PostalCode" = $postalCode; "Country" = $country}
# Do not use this address again
$addressIndexesUsed += $addressIndex
}
#
# Create the users
#
#
# Randomly determine this user's properties
#
# Create (and overwrite) new array lists [0]
$CSV_Fname = New-Object System.Collections.ArrayList
$CSV_Lname = New-Object System.Collections.ArrayList
#Populate entire $firstNames and $lastNames into the array
$CSV_Fname.Add($firstNames)
$CSV_Lname.Add($lastNames)
# Sex & name
$i = 0
if ($i -lt $userCount)
{
foreach ($firstname in $firstNames)
{
foreach ($lastname in $lastnames)
{
$Fname = ($CSV_Fname | Get-Random).FirstName
$Lname = ($CSV_Lname | Get-Random).LastName
#Capitalise first letter of each name
$displayName = (Get-Culture).TextInfo.ToTitleCase($Fname + " " + $Lname)
# Address
$locationIndex = Get-Random -Minimum 0 -Maximum $locations.Count
$street = $locations[$locationIndex].Street
$city = $locations[$locationIndex].City
$state = $locations[$locationIndex].State
$postalCode = $locations[$locationIndex].PostalCode
$country = $locations[$locationIndex].Country
$matchcc = $phoneCountryCodes.GetEnumerator() | Where-Object {$_.Name -eq $country} # match the phone country code to the selected country above
# Department & title
$departmentIndex = Get-Random -Minimum 0 -Maximum $departments.Count
$department = $departments[$departmentIndex].Name
$title = $departments[$departmentIndex].Positions[$(Get-Random -Minimum 0 -Maximum $departments[$departmentIndex].Positions.Count)]
# Phone number
if ($matchcc.Name -notcontains $country)
{
Write-Debug ("ERROR1: No country code found for $country")
continue
}
if (-not $postalAreaCodes.ContainsKey($postalCode))
{
Write-Debug ("ERROR2: No country code found for $country")
continue
}
$officePhone = $matchcc.Value + " " + $postalAreaCodes[$postalCode].Substring(1) + " " + (Get-Random -Minimum 100000 -Maximum 1000000)
# Build the sAMAccountName: $orgShortName + employee number
$employeeNumber = Get-Random -Minimum 100000 -Maximum 1000000
$sAMAccountName = $orgShortName + $employeeNumber
$userExists = $false
Try { $userExists = Get-ADUser -LDAPFilter "(sAMAccountName=$sAMAccountName)" }
Catch { }
if ($userExists)
{
$i=$i-1
if ($i -lt 0)
{$i=0}
continue
}
#
# Create the user account
#
New-ADUser -SamAccountName $sAMAccountName -Name $displayName -Path $ou -AccountPassword $securePassword -Enabled $true -GivenName $Fname -Surname $Lname -DisplayName $displayName -EmailAddress "$Fname.$Lname@$dnsDomain" -StreetAddress $street -City $city -PostalCode $postalCode -State $state -Country $country -UserPrincipalName "$sAMAccountName@$dnsDomain" -Company $company -Department $department -EmployeeNumber $employeeNumber -Title $title -OfficePhone $officePhone
"Created user #" + ($i+1) + ", $displayName, $sAMAccountName, $title, $department, $officePhone, $country, $street, $city"
$i = $i+1
$employeeNumber = $employeeNumber+1
if ($i -ge $userCount)
{
"Script Complete. Exiting"
exit
}
}
}
}