Hello,
I am creating a script that creates user accounts from information input by the user. I have added some validation to the variables and want the script to force the user to input a valid value before they continue to the next stage input prompt. How would i do this?
$error.clear()
while ($error.count -eq 0)
{
$Username = Read-Host "Enter Full name of new staff member (e.g. John Smith, Lady Grey, George Bush)"
$FirstName = Read-Host "Enter First name of new staff member"
$Surname = Read-Host "Enter Surname of new staff member"
$DispName = $Username
$Desc = Read-Host "Enter Job title of new staff member"
#$Title = Read-Host "Enter Title of new staff member (Mr, Mrs, Miss etc)"
$Depar = Read-Host "Enter Department of new staff member"
$telnumber = Read-Host "Enter staff members telephone extension (e.g 1754)"
if($error.count -eq 1)
{
Write-Host -BackgroundColor Red -ForegroundColor Black "An error occured during user input. Please try again"
continue
}
else
{
$SamName = Read-Host "Enter Username name of new staff member in the format first initial and surname (e.g jsmith, lgrey, gbush)"
$UserArray = ("{0},{1},{2},{3},{4},{5},{6},{7}" -f $Username,$FirstName,$Surname,$DispName,$Desc,$Depar,$Telnumber,$SamName)
# Add input to csv file.
Add-Content -Path $Path -Value $UserArray
Write-Host -BackgroundColor Green -ForegroundColor Black "User succesfully Input"
In this example i have placed the "if "statement after the $telnumber value as i have only added the validation to this variable.At present if you input an incorrect value it simply shows an error message and my error message and exits the script. I want it so that the input has to be correct before they can continue (to the $Samname variable). I would also appreciate some help with how i could do error checking after every variable without having to put an “if” statement in between every variable input!
Thanks in advance.