diff --git a/Active Directory/Setup-ADDS-LabUsers.ps1 b/Active Directory/Setup-ADDS-LabUsers.ps1 new file mode 100644 index 0000000..2217c08 --- /dev/null +++ b/Active Directory/Setup-ADDS-LabUsers.ps1 @@ -0,0 +1,169 @@ +<# +.SYNOPSIS + Creates a specified number of test users in Active Directory, distributed across various departments, with randomly generated names and phone numbers. + +.DESCRIPTION + This script creates a specified number of test users in Active Directory, distributed across various departments, + with randomly generated names and phone numbers. It prompts the user to specify a password for the test users and creates a log file to track the creation process. + +.PARAMETER accounts + Specifies the number of test users to create. Default is 5. + +.PARAMETER userOU + Specifies the OU where the test users will be created. + +.EXAMPLE + .\Create-LabUsers.ps1 -accounts 10 -userOU "OU=TestUsers,DC=example,DC=com" + +.NOTES + This script is intended for use in a test or production environment. Make sure to test the script in a non-production environment before running it in production. + Author: D.de Kooker - info@dcomputers.nl + Version: 1.0 + + Inspired by: https://paulcunningham.dev/powershell-script-create-active-directory-users-in-test-lab/ + + DISCLAIMER: Use scripts at your own risk, if there is anything I can help you with I will try but I do not take responsibility for the way that anyone else uses my scripts. + Sharing is caring. Share your knowledge with the world so that everybody can learn from it. + +.LINK + The latest version can Always be found on my GIT page on the link below: + https://git.dcomputers.nl/Dcomputers/PowershellScripts +#> + +[CmdletBinding()] + param( + [Parameter(Mandatory=$false)] + [int]$accounts = 5, + + [parameter(Mandatory=$true)] + [string]$userOU + ) + +#region Global script settings and variables + $Version = "v1.0" + $logfilelocation = "$($MyInvocation.MyCommand.Path | Split-Path -Parent)\Logs" + $logfilename = "$(Get-Date -Format yyyyMMddHHmmss)-ADDS_LabUsers.log" + $summaryfilename = "$(Get-Date -Format yyyyMMddHHmmss)-ADDS_LabUsers_Summary.txt" + + $Departments = @("Administration", "Human Resources", "Legal", "Finance", "Engineering", "Sales", "Information Technology", "Service") +#endregion + +#region functions + function Initiate-Log { + # Get current user and session information + $username = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name + $computerName = $env:COMPUTERNAME + $sessionID = $pid + $date = Get-Date -Format "yyyy-MM-dd HH:mm:ss" + + # Write log header + $logHeader = "[$date] Log initiated by $username on $computerName (Session ID: $sessionID)" + Add-Content -Path $logfilelocation\$logfilename -Value "**********************" + Add-Content -Path $logfilelocation\$logfilename -Value "LogFile initiation" + Add-Content -Path $logfilelocation\$logfilename -Value "Start time: $date" + Add-Content -Path $logfilelocation\$logfilename -Value "Username: $username" + Add-Content -Path $logfilelocation\$logfilename -Value "Machine: $computerName" + Add-Content -Path $logfilelocation\$logfilename -Value "Process ID: $sessionID" + Add-Content -Path $logfilelocation\$logfilename -Value "Script Version: $Version" + Add-Content -Path $logfilelocation\$logfilename -Value "Script Source: https://git.dcomputers.nl/Dcomputers/PowershellScripts" + Add-Content -Path $logfilelocation\$logfilename -Value "**********************" + } + + function Write-Log { + [CmdletBinding()] + Param ( + [Parameter(Mandatory=$true)] + [string]$Message, + [Parameter(Mandatory=$false)] + [ValidateSet("INFO", "WARNING", "ERROR")] + [string]$Level = "INFO" + ) + $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" + $logmessage = "[$timestamp] [$Level] $Message" + Add-Content -Path $logfilelocation\$logfilename -Value $logmessage + } + + function Write-Summary { + [CmdletBinding()] + Param ( + [Parameter(Mandatory=$true)] + [string]$Message + ) + Add-Content -Path $logfilelocation\$summaryfilename -Value $Message + } +#endregion + +#region prerequisites check + #Create log directory if not present and initiate logfile + if (!(test-path $logfilelocation)) {mkdir $logfilelocation} + Initiate-Log + + #Check if the required Powershell Modules are available + $modules = @("ActiveDirectory") + foreach ($module in $modules) { + if (!(Get-Module -Name $module -ListAvailable)) { + Write-Host "The $module module is not installed. Please install it and try again." + Write-Log -Message "The $module module is not installed. Please install it and try again." -Level ERROR + exit 1 + } + else { + Import-Module $module + } + } +#endregion + +#region user information gathering + $pw = Read-Host -Prompt "Specify the password for the test users:" -AsSecureString + + # Generate fake names for each department + Write-Host "Collecting fake infromation for user creation" + Write-Log -Message "Collecting fake infromation for user creation" -Level "INFO" + $users = @() + foreach ($department in $Departments) { + for ($i = 0; $i -lt $accounts; $i++) { + $uri = "https://randomuser.me/api/?results=1&inc=name,phone&nat=us" + $response = Invoke-WebRequest -Uri $uri -Method Get + $json = $response.Content | ConvertFrom-Json + $firstName = $json.results[0].name.first + $lastName = $json.results[0].name.last + $phoneNumber = $json.results[0].phone + $username = $("$($firstName[0]).$lastName").ToLower() + $users += [PSCustomObject]@{ + Department = $department + FirstName = $firstName + LastName = $lastName + Username = $username + PhoneNumber = $phoneNumber + } + } + } +#endregion + +#region user creation + foreach ($user in $users) { + $displayName = "$($user.FirstName) $($user.LastName)" + $samAccountName = $user.Username + $userPrincipalName = "$($user.Username)@$((Get-ADDomainController).domain)" + $Department = $user.Department + $UsersArgs = @{ + GivenName = $user.FirstName + Surname = $user.LastName + Name = $displayName + Displayname = "$displayName | Test User" + SamAccountName = $samAccountName + UserPrincipalName = $userPrincipalName + OfficePhone = $user.PhoneNumber + Department = $Department + AccountPassword = $pw + PasswordNeverExpires = $true + CannotChangePassword = $true + PasswordNotRequired = $false + ChangePasswordAtLogon = $false + Enabled = $true + Path = $userOu + } + New-ADUser @UsersArgs + Write-Host "Created user $($user.Username) in department $($user.Department)" + Write-Log -Message "Created user $($user.Username) in department $($user.Department)" -Level INFO + } +#endregion \ No newline at end of file