<#
Author:
Date:
Description: Creates user(s) in specificed OU ((removed), (removed), or(removed))
#>
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" = "(OU)";
"2" = "(OU)";
"3" = "(OU)";
}
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,
[string]$StreetAddress,
[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.(OU) `n2.(OU) `n3.(OU)"
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:"(@mycompany.ca)")"
$UserLogon = Read-host
$myRegexLogon = "^.*@.*"
}While($userLogon -match $myRegexLogon)
}
$PrincipalName = $userLogon + "@(mycompany.ca)"
<#do
{
Write-Host "Principal Name (Example:rhayabusa@(@mycompany.ca))"
$principalName = Read-Host
$myRegexPrincipalName = "^.*(@mycompany.ca)"
}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
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"
do
{
$moreInfoResponse = Read-Host -Prompt "Would you like to add extra information such as phone/address/office etc...? (y/n)"
}While(($moreInfoResponse -ne "y") -and ($moreInfoResponse -ne "n"))
if($moreInfoResponse -eq "y")
{
$StreetAddress = Read-Host -Prompt "Street Address"
$Office = Read-Host -Prompt "Office"
New-ADUser -Name $FullName -GivenName $FirstName -Surname $LastName -SamAccountName $UserLogon -EmailAddress $Email -Description $Description -Office $Office -StreetAddress $StreetAddress -UserPrincipalName $PrincipalName -AccountPassword $Password -ChangePasswordAtLogon $PasswordExpires -Enabled $true -Path "ou=Users, ou = $selectedOU, dc=(domain),dc=(domain),dc=(domain)" -WhatIf
break
}
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=(domain),dc=(domain),dc=(domain)" -WhatIf
}
Catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
{
Write-Warning "$selectedOU, was not found"
}
Catch
{
Write-Warning "An unspecified error was caught"
Exit 1
}
}
}
}
<#
Author:
Date:
Description: Creates user(s) in specificed OU ((removed), (removed), or(removed))
#>
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" = "(OU)";
"2" = "(OU)";
"3" = "(OU)";
}
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,
[string]$StreetAddress,
[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.(OU) `n2.(OU) `n3.(OU)"
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:"(@mycompany.ca)")"
$UserLogon = Read-host
$myRegexLogon = "^.*@.*"
}While($userLogon -match $myRegexLogon)
}
$PrincipalName = $userLogon + "@(mycompany.ca)"
<#do
{
Write-Host "Principal Name (Example:rhayabusa@(@mycompany.ca))"
$principalName = Read-Host
$myRegexPrincipalName = "^.*(@mycompany.ca)"
}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
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"
do
{
$moreInfoResponse = Read-Host -Prompt "Would you like to add extra information such as phone/address/office etc...? (y/n)"
}While(($moreInfoResponse -ne "y") -and ($moreInfoResponse -ne "n"))
if($moreInfoResponse -eq "y")
{
$StreetAddress = Read-Host -Prompt "Street Address"
$Office = Read-Host -Prompt "Office"
New-ADUser -Name $FullName -GivenName $FirstName -Surname $LastName -SamAccountName $UserLogon -EmailAddress $Email -Description $Description -Office $Office -StreetAddress $StreetAddress -UserPrincipalName $PrincipalName -AccountPassword $Password -ChangePasswordAtLogon $PasswordExpires -Enabled $true -Path "ou=Users, ou = $selectedOU, dc=(domain),dc=(domain),dc=(domain)" -WhatIf
break
}
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=(domain),dc=(domain),dc=(domain)" -WhatIf
}
Catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
{
Write-Warning "$selectedOU, was not found"
}
Catch
{
Write-Warning "An unspecified error was caught"
Exit 1
}
}
}
}
Copyright © 2024, NextGenUpdate.
All Rights Reserved.