For a task at work I had to create a number of users in Active Directory for a demo environment I had to set up. I didn’t really want to do this manually, and I wanted something more realistic than ‘DemoUser 01’, ‘DemoUser 02’ and so on.
I found an online API to generate random, but realistic user data, and decided to create a wrapper function to quickly get user data.
I then created a simple script to use this data to create demo users in Active Directory. I thought this might be of interest to others, so decided to share it here on my blog.
The code could probably be more polished, but as this was for a demo environment I decided that this would be enough. If you take my code and improve on it, please let me know! 🙂
First the wrapper function to get user data from randomuser.me.
| function Get-RandomUser { | |
| <# | |
| .SYNOPSIS | |
| Generate random user data. | |
| .DESCRIPTION | |
| This function uses the free API for generating random user data from https://randomuser.me/ | |
| .EXAMPLE | |
| Get-RandomUser 10 | |
| .EXAMPLE | |
| Get-RandomUser -Amount 25 -Nationality us,gb -Format csv -ExludeFields picture | |
| .LINK | |
| https://randomuser.me/ | |
| .NOTES | |
| Author: Øyvind Kallstad | |
| Date: 08.04.2016 | |
| Version: 1.0 | |
| #> | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Position = 0)] | |
| [ValidateRange(1,5000)] | |
| [int] $Amount, | |
| [Parameter()] | |
| [ValidateSet('Male','Female')] | |
| [string] $Gender, | |
| # Supported nationalities: AU, BR, CA, CH, DE, DK, ES, FI, FR, GB, IE, IR, NL, NZ, TR, US | |
| # Pictures won't be affected by this, but data such as location, cell/home phone, id, etc. will be more appropriate. | |
| [Parameter()] | |
| [string[]] $Nationality, | |
| # Seeds allow you to always generate the same set of users. | |
| [Parameter()] | |
| [int] $Seed, | |
| [Parameter()] | |
| [ValidateSet('json','csv','yaml','xml')] | |
| [string] $Format = 'json', | |
| # Fields to include in the results. | |
| # Supported values: gender, name, location, email, login, registered, dob, phone, cell, id, picture, nat | |
| [Parameter()] | |
| [string[]] $IncludeFields, | |
| # Fields to exclude from the the results. | |
| # Supported values: gender, name, location, email, login, registered, dob, phone, cell, id, picture, nat | |
| [Parameter()] | |
| [string[]] $ExcludeFields | |
| ) | |
| $rootUrl = "http://api.randomuser.me/?format=$($Format)" | |
| if ($Amount) { | |
| $rootUrl += "&results=$($Amount)" | |
| } | |
| if ($Gender) { | |
| $rootUrl += "&gender=$($Gender)" | |
| } | |
| if ($Seed) { | |
| $rootUrl += "&seed=$($Seed)" | |
| } | |
| if ($Nationality) { | |
| $rootUrl += "&nat=$($Nationality -join ',')" | |
| } | |
| if ($IncludeFields) { | |
| $rootUrl += "&inc=$($IncludeFields -join ',')" | |
| } | |
| if ($ExcludeFields) { | |
| $rootUrl += "&exc=$($ExcludeFields -join ',')" | |
| } | |
| Invoke-RestMethod -Uri $rootUrl | |
| } |
This is the code that I used to create the demo users.
| $adPath = 'OU=Users,OU=Dev,DC=test,DC=local' | |
| $adDomain = 'test.local' | |
| $userPassword = 'aV3ryl0ng-and.securePASSWORD!!' | |
| $users = Get-RandomUser -Amount 30 -Nationality gb,uk -ExcludeFields picture | Select-Object -ExpandProperty results | |
| foreach ($user in $users) { | |
| $newUserProperties = @{ | |
| Name = "$($user.name.first) $($user.name.last)" | |
| GivenName = $user.name.first | |
| Surname = $user.name.last | |
| Path = $adPath | |
| AccountPassword = (ConvertTo-SecureString $userPassword -AsPlainText -Force) | |
| SamAccountName = $user.login.username | |
| UserPrincipalName = "$($user.login.username)@$($adDomain)" | |
| Enabled = $true | |
| } | |
| try {New-ADUser @newUserProperties} catch {} | |
| } |
Unfortunately the password returned with the API is not always strong enough for Active Directory, so I decided to hard code it. These are demo users so having the same password for all users isn’t an issue.
I hope this have been helpful. Please leave any comments below.
Hi Øyvind,
thanks a lot for sharing, very useful. I’ve forked your code and refactored your code a bit https://gist.github.com/920ca69db98d0e71d7e1d68f795f5101.
Dirk
LikeLiked by 1 person
I started similar project in pure PowerShell – New-RandomPerson https://github.com/it-praktyk/New-RandomPerson
LikeLiked by 1 person
Nice one! 🙂
LikeLike