try
{
import-module activedirectory
}
catch
{
Write-Host "Active Directory Module Could Not Be Loaded."
Exit 1
}
$date = Get-Date
$adDomainName = (Get-ADDomain).DistinguishedName
$dnsroot = (Get-ADDomain).DNSRoot
$OrganizationalUnit = @{
"1" = "Call Center";
"2" = "Corporate";
"3" = "Properties";
}
Function Create-User
{
[CmdletBinding(SupportsShouldProcess=$true)]
param(
[Parameter(Mandatory=$true)]
[string]$FirstName,
[Parameter(Mandatory=$true)]
[string]$LastName,
[Parameter(Mandatory=$true)]
[string]$Description,
[string]$FullName,
[string]$UserLogon,
[string]$PrincipalName,
[string]$Email,
[bool]$PasswordExpires
)
if(($FirstName -ne $null) -and ($FirstName -match '[0-9]') -and ($LastName -ne $null) -and ($LastName -match '[0-9]'))
{
Write-Host "Invalid entry"
continue
}
$userLogon = $FirstName.SubString(0,1).ToLowerInvariant() + $LastName.ToLowerInvariant()
$userInput = Read-Host -prompt "In which OU would you like to place the user in?`n1.Call Centre `n2.Corporate `n3.Properties"
if($userInput -notin $OrganizationalUnit.Keys)
{
Write-Host "Invalid entry"
Write-Host "$userInput"
}
else
{
$selectedOU = $OrganizationalUnit::$userInput
If ($PSCmdlet.ShouldProcess("Destination OU: $selectedOU"))
{
try
{
$FullName = $FirstName + " " + $LastName
$UserLogon = $FirstName.Substring(0,1).ToLowerInvariant() + $LastName.ToLowerInvariant()
Write-Host "User Logon has been set to: $userLogon"
$setUserLogonManually = Read-Host -Prompt "Do you want to manually set the logon? (y/n)"
$setUserLogonManually.ToLowerInvariant()
if($setUserLogonManually -eq "y")
{
do
{
Write-Host "User Logon (Do not include:"@(removed)")"
$UserLogon = Read-host
$myRegexLogon = "^.*@.*"
}While($userLogon -match $myRegexLogon)
}
$PrincipalName = $userLogon + "@(removed)"
<#do
{
Write-Host "Principal Name (Example:(removed))"
$principalName = Read-Host
$myRegexPrincipalName = "^.*@(removed)"
}While($principalName -notmatch $myRegexPrincipalName)#>
$Email = $PrincipalName
do
{
Write-Host "Password (Must be 8 length or greater)"
$Password = Read-Host | ConvertTo-SecureString -AsPlainText -Force
}While($Password.Length -lt 8)
do
{
$response = Read-Host -prompt "Change password on Logon? (0 = false | 1 = true) 'n Setting it to 0 will require you to manually enable the account!"
if($response -eq "1")
{
$PasswordExpires = $true
}
else
{
$PasswordExpires = $false
}
}While(($response -ne "0") -and ($response -ne "1"))
Write-Host "$FullName will be placed in the following OU: $selectedOU"
New-ADUser -Name $FullName -GivenName $FirstName -Surname $LastName -SamAccountName $UserLogon -EmailAddress $Email -Description $Description -UserPrincipalName $PrincipalName -AccountPassword $Password -ChangePasswordAtLogon $PasswordExpires -Enabled $true -Path "ou=Users, ou = $selectedOU, dc=,dc=,dc="
}
Catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
{
Write-Warning "$selectedOU, was not found"
}
Catch
{
Write-Warning "An unspecified error was caught"
Exit 1
}
}
}
}
while($true)
{
Write-Host "Would you like to create a user?(y/n)"
$createUserResponse = Read-Host
$createUserResponse.ToLowerInvariant()
if($createUserResponse -ne "y")
{
break
}
else
{
Create-User
}
}