In my script, I am creating a user to add in AD. At the end I want to stop the script from re-running again and asking me to prompt “enter first name:”. How do I stop it at the end? Scroll all the way down to see where I added “exit”, but it did not work. Could someone help? Thanks
#$title = "Create a User Account in Active Directory"
Import-Module ActiveDirectory -EA Stop
# cls removes all text from current display incuding commands and output
# sleep suspends activity in script or session for specified period of time
# Write-Host allows to use it to emit output to the information stream
sleep 5
cls
Write Host
$firstname = Read-Host "Enter First Name"
Write Host
$lastname = Read-Host " Enter Last Name"
Write Host
$fullname = Write-Host "$firstname $lastname"
Write Host
$i = 1
$logonname = $firstname.substring(0,$i) + $lastname
$OU = "OU=Testing, DC=silvertech, DC=local"
$domain = $env:userdnsdomain
$Description = Read-Host "Enter in the User Description"
DO
{
If ($(Get-ADUser -Filter {SamAccountName -eq $logonname})) {
Write-Host "WARNING: Logon name" $logonname.toUpper() "already exists!!" -ForegroundColor:Green
$i++
$logonname = $firstname.substring(0,$i) + $lastname
Write-Host
Write-Host
Write-Host "Changing Logon name to" $logonname.toUpper() -ForegroundColor:Green
Write-Host
$taken = $true
sleep 10
} else {
$taken = $false
}
} Until ($taken -eq $false)
$logonname = $logonname.toLower()
cls
#Displaying Account information.
Write-Host "======================================="
Write-Host
Write-Host "Firstname: $firstname"
Write-Host "Lastname: $lastname"
Write-Host "Display name: $fullname"
Write-Host "Logon name: $logonname"
Write-Host "OU: $OU"
Write-Host "Domain: $domain"
#Setting minimum password length to 10 characters and adding password complexity.
$PasswordLength = 10
Do
{
Write-Host
$isGood = 0
$Password = Read-Host "Enter in the Password" -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password)
$Complexity = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
if ($Complexity.Length -ge $PasswordLength) {
Write-Host
} else {
Write-Host "Password needs $PasswordLength or more Characters" -ForegroundColor:Green
}
if ($Complexity -match "[^a-zA-Z0-9]") {
$isGood++
} else {
Write-Host "Password does not contain Special Characters." -ForegroundColor:Green
}
if ($Complexity -match "[0-9]") {
$isGood++
} else {
Write-Host "Password does not contain Numbers." -ForegroundColor:Green
}
if ($Complexity -cmatch "[a-z]") {
$isGood++
} else {
Write-Host "Password does not contain Lowercase letters." -ForegroundColor:Green
}
if ($Complexity -cmatch "[A-Z]") {
$isGood++
} else {
Write-Host "Password does not contain Uppercase letters." -ForegroundColor:Green
}
} Until ($password.Length -ge $PasswordLength -and $isGood -ge 3)
Write-Host
Read-Host "Press Enter to Continue Creating the Account"
Write-Host "Creating Active Directory user account now" -ForegroundColor:Green
#Creating user account with the information you inputted.
New-ADUser -Name $fullname -GivenName $firstname -Surname $lastname -DisplayName $fullname -SamAccountName $logonname -UserPrincipalName $logonname@$Domain -AccountPassword $password -Enabled $true -Path $OU -Description $Description -Confirm:$false
sleep 2
Add-ADGroupMember -Identity "SilverTech" -Members $logonname
Add-ADGroupMember -Identity "Staff" -Members $logonname
Add-ADGroupMember -Identity "VPN Users" -Members $logonname
Set-ADUser $logonname -emailaddress "$firstname.$lastname@silvertech.com"
#silvertech
Write-Host
$ADProperties = Get-ADUser $logonname -Properties *
Sleep 3
cls
Write-Host "========================================================"
Write-Host "The account was created with the following properties:"
Write-Host
Write-Host "Firstname: $firstname"
Write-Host "Lastname: $lastname"
Write-Host "Display name: $fullname"
Write-Host "Logon name: $logonname"
Write-Host "OU: $OU"
Write-Host "Domain: $domain"
Write-Host
#Write-Host$title = "Create a User Account in Active Directory"
Import-Module ActiveDirectory -EA Stop
sleep 5
cls
Write Host
$firstname = Read-Host "Enter First Name"
Write Host
$lastname = Read-Host " Enter Last Name"
Write Host
$fullname = Read-Host "$firstname $lastname , enter display name"
Write Host
$i = 1
$logonname = $firstname.substring(0,$i) + $lastname
$OU = "OU=Testing, DC=silvertech, DC=local"
$domain = $env:userdnsdomain
$Description = Read-Host "Enter in the User Description"
DO
{
If ($(Get-ADUser -Filter {SamAccountName -eq $logonname})) {
Write-Host "WARNING: Logon name" $logonname.toUpper() "already exists!!" -ForegroundColor:Green
$i++
$logonname = $firstname.substring(0,$i) + $lastname
Write-Host
Write-Host
Write-Host "Changing Logon name to" $logonname.toUpper() -ForegroundColor:Green
Write-Host
$taken = $true
sleep 10
} else {
$taken = $false
}
} Until ($taken -eq $false)
$logonname = $logonname.toLower()
cls
#Displaying Account information.
Write-Host "======================================="
Write-Host
Write-Host "Firstname: $firstname"
Write-Host "Lastname: $lastname"
Write-Host "Display name: $fullname"
Write-Host "Logon name: $logonname"
Write-Host "OU: $OU"
Write-Host "Domain: $domain"
#Setting minimum password length to 10 characters and adding password complexity.
$PasswordLength = 10
Do
{
Write-Host
$isGood = 0
$Password = Read-Host "Enter in the Password" -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password)
$Complexity = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
if ($Complexity.Length -ge $PasswordLength) {
Write-Host
} else {
Write-Host "Password needs $PasswordLength or more Characters" -ForegroundColor:Green
}
if ($Complexity -match "[^a-zA-Z0-9]") {
$isGood++
} else {
Write-Host "Password does not contain Special Characters." -ForegroundColor:Green
}
if ($Complexity -match "[0-9]") {
$isGood++
} else {
Write-Host "Password does not contain Numbers." -ForegroundColor:Green
}
if ($Complexity -cmatch "[a-z]") {
$isGood++
} else {
Write-Host "Password does not contain Lowercase letters." -ForegroundColor:Green
}
if ($Complexity -cmatch "[A-Z]") {
$isGood++
} else {
Write-Host "Password does not contain Uppercase letters." -ForegroundColor:Green
}
} Until ($password.Length -ge $PasswordLength -and $isGood -ge 3)
Write-Host
Read-Host "Press Enter to Continue Creating the Account"
Write-Host "Creating Active Directory user account now" -ForegroundColor:Green
#Creating user account with the information you inputted.
New-ADUser -Name $fullname -GivenName $firstname -Surname $lastname -DisplayName $fullname -SamAccountName $logonname -UserPrincipalName $logonname@$Domain -AccountPassword $password -Enabled $true -Path $OU -Description $Description -Confirm:$false
sleep 2
Add-ADGroupMember -Identity "SilverTech" -Members $logonname
Add-ADGroupMember -Identity "Staff" -Members $logonname
Add-ADGroupMember -Identity "VPN Users" -Members $logonname
Set-ADUser $logonname -emailaddress "$firstname.$lastname@silvertech.com"
Write-Host
$ADProperties = Get-ADUser $logonname -Properties *
Sleep 3
cls
Write-Host "========================================================"
Write-Host "The account was created with the following properties:"
Write-Host
Write-Host "Firstname: $firstname"
Write-Host "Lastname: $lastname"
Write-Host "Display name: $fullname"
Write-Host "Logon name: $logonname"
Write-Host "OU: $OU"
Write-Host "Domain: $domain"
Write-Host
Write-Host
Exit