PSRandomUser.psm1
|
# ------------------------------------------------------------------------------ # PSRandomUser.psm1 # DO NOT EDIT - generated by CreateModuleScriptFile.ps1 # Source of truth: PSRandomUser/Public/*.ps1 # ------------------------------------------------------------------------------ function New-RandomUser { <# .SYNOPSIS The function New-RandomUser creates a random user using the API provided by the creators of RANDOM USER GENERATOR - https://randomuser.me .DESCRIPTION The function New-RandomUser creates a random user using the API provided by the creators of RANDOM USER GENERATOR - https://randomuser.me a free and easy to use service to generate random user data for application testing. You can request a different nationality of a randomuser. Pictures won't be affected by this, but data such as location, home phone, id, etc. will be more appropriate. .EXAMPLE New-RandomUser Creates a single random user .EXAMPLE New-RandomUser -Nationality GB -PassLength 14 -Quantity 10 -Email "leigh-services.com" Creates 10 random users from United Kingdom, with a password length of 14 characters and with an email address @leigh-services.com .INPUTS [string] Nationality [int] PassLength [int] Quantity [string] Email .OUTPUTS [PSObject] .NOTES Author: Luke Leigh Website: https://blog.lukeleigh.com/ LinkedIn: https://www.linkedin.com/in/lukeleigh/ GitHub: https://github.com/BanterBoy/ GitHubGist: https://gist.github.com/BanterBoy .LINK https://github.com/BanterBoy #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute( 'PSUseShouldProcessForStateChangingFunctions','', Justification = 'Read-only data generator; the New verb is required by approved-verb policy but the function does not change any system state.')] [CmdletBinding( DefaultParameterSetName = "Default")] Param ( # Please select the user nationality. The default setting will choose a Random nationality. [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, HelpMessage = "Please select the user nationality. The default setting will choose a Random nationality.")] [ValidateSet('AU', 'BR', 'CA', 'CH', 'DE', 'DK', 'ES', 'FI', 'FR', 'GB', 'IE', 'IR', 'NO', 'NL', 'NZ', 'TR', 'US', 'Random') ] [string] $Nationality = "Random", # Please enter or select password length. The default length is 10 characters. [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, HelpMessage = "Please enter or select password length. The default length is 10 characters.")] [ValidateSet('8', '10', '12', '14', '16', '18', '20') ] [int] $PassLength = "10", # Please select number of results. The default is 1. Min-Max = 1-5000 [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, HelpMessage = "Please select number of results. The default is 1. Min-Max = 1-5000")] [ValidateRange(1, 5000)] [int] $Quantity = "1", # Specify the user's e-mail address. This parameter sets the EmailAddress property of a user object. The default value is $env:USERDNSDOMAIN. [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, HelpMessage = "Specify the user's e-mail address. This parameter sets the EmailAddress property of a user object.")] [string] $Email = "$env:USERDNSDOMAIN" ) BEGIN { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 } PROCESS { $Uri = "https://randomuser.me/api/?nat=$Nationality&password=upper,lower,special,number,$PassLength&format=PrettyJSON&results=$Quantity" $Results = Invoke-RestMethod -Method GET -Uri $Uri -UseBasicParsing $mail = ($Email).ToLower() try { foreach ( $item in $Results.results ) { $rawSam = ($item.name.first + $item.name.last) $normalized = $rawSam.Normalize([Text.NormalizationForm]::FormD) $stripped = -join ($normalized.ToCharArray() | Where-Object { [Globalization.CharUnicodeInfo]::GetUnicodeCategory($_) -ne [Globalization.UnicodeCategory]::NonSpacingMark }) $safeSam = ($stripped -replace '[^A-Za-z0-9]', '').ToLower() if ($safeSam.Length -gt 20) { $safeSam = $safeSam.Substring(0, 20) } $RandomUser = [ordered]@{ "Name" = $item.name.first + " " + $item.name.last "Title" = $item.name.title "GivenName" = $item.name.first "Surname" = $item.name.last "DisplayName" = $item.name.title + " " + $item.name.first + " " + $item.name.last "HouseNumber" = $item.location.street.number "StreetAddress" = $item.location.street.name "City" = $item.location.city "State" = $item.location.state "Country" = $item.nat "CountryName" = $item.location.country "PostalCode" = $item.location.postcode "UserPrincipalName" = ($item.name.first + "." + $item.name.last).ToLower() + "@" + $mail "PersonalEmail" = $item.email "SamAccountName" = $safeSam "HomePhone" = $item.phone "MobilePhone" = $item.cell "Gender" = $item.gender "Nationality" = $item.nat "Age" = $item.dob.age "DateOfBirth" = $item.dob.date "NINumber" = $item.id.value "TimeZone" = $item.location.timezone.description "TimeOffset" = $item.location.timezone.offset "Latitude" = $item.location.coordinates.latitude "Longitude" = $item.location.coordinates.longitude "Username" = $item.login.username "UUID" = $item.login.uuid "AccountPassword" = $item.login.password "Salt" = $item.login.salt "MD5" = $item.login.md5 "Sha1" = $item.login.sha1 "Sha256" = $item.login.sha256 "LargePicture" = $item.picture.large "MediumPicture" = $item.picture.medium "ThumbnailPicture" = $item.picture.thumbnail } $obj = New-Object -TypeName PSObject -Property $RandomUser Write-Output $obj } } catch { $PSCmdlet.WriteError($_) } } END { } } function New-TempADUser { <# .SYNOPSIS This function creates a new temporary AD user using the data parsed from New-TempADUserDetails. .DESCRIPTION Creates a temporary Active Directory user account by calling New-ADUser with the supplied identity and address attributes. TEST/LAB USE ONLY - never run against a production directory. Requires the ActiveDirectory RSAT module and a reachable domain controller. .EXAMPLE New-TempADUserDetails | New-TempADUser Pipes a generated user object straight into New-TempADUser to provision the account. .EXAMPLE New-TempADUser -Name 'Test User' -GivenName 'Test' -Surname 'User' ` -DisplayName 'Test User' -SamAccountName 'testuser' ` -UserPrincipalName 'test.user@contoso.local' ` -AccountPassword 'P@ssw0rd!Plain' -Path 'CN=Users,DC=contoso,DC=local' Creates a single temporary AD user using explicit parameters (no pipeline input). .INPUTS [PSObject] .OUTPUTS None. New-ADUser is invoked without -PassThru, so no object is returned. .NOTES Author: Luke Leigh Website: https://blog.lukeleigh.com/ LinkedIn: https://www.linkedin.com/in/lukeleigh/ GitHub: https://github.com/BanterBoy/ GitHubGist: https://gist.github.com/BanterBoy .LINK https://github.com/BanterBoy #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute( 'PSAvoidUsingUsernameAndPasswordParams','', Justification = 'TEST-ONLY module for disposable lab accounts. Parameter shape is intentional and matches the pipeline output of New-TempADUserDetails; documented in the module manifest description and README.')] [CmdletBinding( SupportsShouldProcess = $true, DefaultParameterSetName = "Default")] param ( [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Full name (CN) for the new AD user" )] [ValidateNotNullOrEmpty()] [string] $Name, [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Job title for the new AD user" )] [string] $Title, [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Given (first) name for the new AD user" )] [string] $GivenName, [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Surname (last name) for the new AD user" )] [string] $Surname, [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Display name shown in directory listings" )] [string] $DisplayName, [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "sAMAccountName for the new AD user (must be <=20 chars, alphanumeric)" )] [ValidateNotNullOrEmpty()] [string] $SamAccountName, [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Street address for the new AD user" )] [string] $StreetAddress, [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "State or region for the new AD user" )] [string] $State, [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "City for the new AD user" )] [string] $City, [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Country (two-letter code) for the new AD user" )] [string] $Country, [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Postal / ZIP code for the new AD user" )] [string] $PostalCode, [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "User principal name, e.g. first.last@contoso.com" )] [ValidateNotNullOrEmpty()] [string] $UserPrincipalName, [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, HelpMessage = "Please enter the DistinguishedName for the OU path for your Email address." )] [string] $Path = $null, [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Plaintext password - converted to SecureString. TEST/LAB USE ONLY." )] [ValidateNotNullOrEmpty()] [string] $AccountPassword ) begin { } process { $resolvedPath = $Path if (-not $PSBoundParameters.ContainsKey('Path') -or [string]::IsNullOrEmpty($resolvedPath)) { try { $resolvedPath = (Get-ADDomain).UsersContainer } catch { $PSCmdlet.WriteError($_) return } } Write-Verbose 'Converting plaintext password to SecureString - test/lab use only.' $securePassword = ConvertTo-SecureString -String $AccountPassword -AsPlainText -Force $userUserSettings = @{ Name = $Name Title = $Title GivenName = $GivenName Surname = $Surname DisplayName = $DisplayName SamAccountName = $SamAccountName UserPrincipalName = $UserPrincipalName StreetAddress = $StreetAddress State = $State City = $City Country = $Country PostalCode = $PostalCode AccountPassword = $securePassword Enabled = $true ChangePasswordAtLogon = $true } if (-not [string]::IsNullOrEmpty($resolvedPath)) { $userUserSettings['Path'] = $resolvedPath } if ($PSCmdlet.ShouldProcess($UserPrincipalName, 'Create temporary AD user')) { try { New-ADUser @userUserSettings -Verbose } catch { $PSCmdlet.WriteError($_) } } } end { } } function New-TempADUserDetails { <# .SYNOPSIS The function New-TempADUserDetails creates user account details for use in creating Temporary Active Directory users, by accessing the API provided by the creators of RANDOM USER GENERATOR - https://randomuser.me. .DESCRIPTION The function New-TempADUserDetails creates user account details for use in creating Temporary Active Directory users. It does this by accessing the API provided by the creators of RANDOM USER GENERATOR - https://randomuser.me a free and easy to use service to generate random user data for application testing. You can specify the nationality of a user and data such as location, home phone, id, etc. will be more appropriate. This Function was created to produce random user details tailored for Active Directory Users. This function can be piped into New-TempADUser to create a random user into Active Directory. .EXAMPLE New-TempADUserDetails Creates a single set of random user details for use in creating a Temporary Active Directory user. .EXAMPLE New-TempADUserDetails -Path (Get-ADDomain).UsersContainer Creates a single set of random user details for use in creating a Temporary Active Directory user and adds it to the specified Users Container in Active Directory. .EXAMPLE New-TempADUserDetails -Nationality GB -PassLength 14 -Quantity 10 -Email "leigh-services.com" Creates 10 Active Directory users from United Kingdom, with a password length of 14 characters and with an email address @leigh-services.com. The users will be added to the Users Container of the current Active Directory Domain. .INPUTS [string] Nationality [int] PassLength [int] Quantity [string] Email .OUTPUTS [PSObject] .NOTES Author: Luke Leigh Website: https://blog.lukeleigh.com/ LinkedIn: https://www.linkedin.com/in/lukeleigh/ GitHub: https://github.com/BanterBoy/ GitHubGist: https://gist.github.com/BanterBoy .LINK https://github.com/BanterBoy #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute( 'PSUseShouldProcessForStateChangingFunctions','', Justification = 'Read-only data generator; the New verb is required by approved-verb policy but the function does not change any system state.')] [Diagnostics.CodeAnalysis.SuppressMessageAttribute( 'PSUseSingularNouns','', Justification = 'Plural noun is intentional - the function emits a Details object intended to be piped into New-TempADUser; renaming would be a breaking public API change.')] [CmdletBinding( DefaultParameterSetName = "Default")] Param ( # Please select the user nationality. The default setting will choose a Random nationality. [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, HelpMessage = "Please select the user nationality. The default setting will choose a Random nationality.")] [ValidateSet('AU', 'BR', 'CA', 'CH', 'DE', 'DK', 'ES', 'FI', 'FR', 'GB', 'IE', 'IR', 'NO', 'NL', 'NZ', 'TR', 'US', 'Random') ] [string] $Nationality = "Random", # Please enter or select password length. The default length is 10 characters. [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, HelpMessage = "Please enter or select password length. The default length is 10 characters.")] [ValidateSet('8', '10', '12', '14', '16', '18', '20') ] [int] $PassLength = "10", # Please select number of results. The default is 1. Min-Max = 1-5000 [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, HelpMessage = "Please select number of results. The default is 1. Min-Max = 1-5000")] [ValidateRange(1, 5000)] [int] $Quantity = "1", # Specify the user's e-mail address. This parameter sets the EmailAddress property of a user object. The default value is $env:USERDNSDOMAIN. [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, HelpMessage = "Specify the user's e-mail address. This parameter sets the EmailAddress property of a user object.")] [string] $Email = "$env:USERDNSDOMAIN", # Enter the X.500 path of the Organizational Unit (OU) or container where the new object is created. [Parameter( Mandatory = $false, ParameterSetName = "Default", ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True, HelpMessage = "Enter the X.500 path of the Organizational Unit (OU) or container where the new object is created.")] [string] $Path ) BEGIN { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 } PROCESS { $Uri = "https://randomuser.me/api/?nat=$Nationality&password=upper,lower,special,number,$PassLength&format=json&results=$Quantity" $Results = Invoke-RestMethod -Method GET -Uri $Uri -UseBasicParsing $mail = ($Email).ToLower() try { foreach ( $item in $Results.results ) { $rawSam = ($item.name.first + $item.name.last) $normalized = $rawSam.Normalize([Text.NormalizationForm]::FormD) $stripped = -join ($normalized.ToCharArray() | Where-Object { [Globalization.CharUnicodeInfo]::GetUnicodeCategory($_) -ne [Globalization.UnicodeCategory]::NonSpacingMark }) $safeSam = ($stripped -replace '[^A-Za-z0-9]', '').ToLower() if ($safeSam.Length -gt 20) { $safeSam = $safeSam.Substring(0, 20) } if ($Path) { $NewFakeUser = [ordered]@{ "Name" = $item.name.first + " " + $item.name.last "Title" = $item.name.title "GivenName" = $item.name.first "Surname" = $item.name.last "DisplayName" = $item.name.title + " " + $item.name.first + " " + $item.name.last "HouseNumber" = $item.location.street.number "StreetAddress" = $item.location.street.name "City" = $item.location.city "State" = $item.location.state "Country" = $item.nat "CountryName" = $item.location.country "PostalCode" = $item.location.postcode "UserPrincipalName" = ($item.name.first + "." + $item.name.last).ToLower() + "@" + $mail "SamAccountName" = $safeSam "AccountPassword" = $item.login.password "Path" = $Path } $obj = New-Object -TypeName PSObject -Property $NewFakeUser Write-Output $obj } else { $NewFakeUser = [ordered]@{ "Name" = $item.name.first + " " + $item.name.last "Title" = $item.name.title "GivenName" = $item.name.first "Surname" = $item.name.last "DisplayName" = $item.name.title + " " + $item.name.first + " " + $item.name.last "HouseNumber" = $item.location.street.number "StreetAddress" = $item.location.street.name "City" = $item.location.city "State" = $item.location.state "Country" = $item.nat "CountryName" = $item.location.country "PostalCode" = $item.location.postcode "UserPrincipalName" = ($item.name.first + "." + $item.name.last).ToLower() + "@" + $mail "SamAccountName" = $safeSam "AccountPassword" = $item.login.password } $obj = New-Object -TypeName PSObject -Property $NewFakeUser Write-Output $obj } } } catch { $PSCmdlet.WriteError($_) } } END { } } Export-ModuleMember -Function 'New-RandomUser','New-TempADUserDetails','New-TempADUser' |