If a script contains a series of questions for someone to enter answers for, how do we perform a sort of error handling on the entered responses? Do we just ignore it and have the script chug along to the next task hoping no issues are created by missing, incomplete or wrong answers? Or do we try to control the chaos by looping back to the question and letting the user know that what they entered was not correct? The latter is of course the best answer, but it may not always be the answer if we don’t have a control set, or known values, that we are expecting. For the below code sample, we will only be using a controlled set of data to illustrate the issue and then illustrate the solution.
Asking a Question and the Response?
In the below code samples we will ask a series of questions of the script operator. The first sample just hopes the operator enters the correct data and that the script will function whereas the last code sample will account for the answers and provide feedback to the end user when it fails.
No Answer Checking
In this code below, we are trying to install the correct version of .NET on a server. The first line asks the operator which version of .NET they wish to install. If they enter the correct number, then .NET will be installed. However, if they do not, then the script will simply ignore the data (no matches) and go back to the main menu. First is the code and after the code is a screenshot of what happens when the correct answer is provided and an incorrect answer is provided:
[sourcecode language=”powershell” collapse=’true’]
$Decision = Read-Host -Prompt "Which version of .Net do you want to install? [4.7, 4.7.1, 4.7.2 or 4.8?] "
If ($Decision -eq '4.7') {
If ($DotNetVersion -lt '460805') {
Install-NET470
Write-Host 'Please make sure to reboot your server now.' -ForegroundColor Yellow
} else {
Write-Host 'This version of .NET is too low for this server.' -ForegroundColor Yellow
}
}
If ($Decision -eq '4.7.1') {
If ($DotNetVersion -lt '461310') {
Install-NET471
Write-Host 'Please make sure to reboot your server now.' -ForegroundColor Yellow
} else {
Write-Host 'This version of .NET is too low for this server.' -ForegroundColor Yellow
}
}
If ($Decision -eq '4.7.2') {
If ($DotNetVersion -lt '461814') {
Install-NET472
Write-Host 'Please make sure to reboot your server now.' -ForegroundColor Yellow
} else {
Write-Host ''
Write-Host 'This version of .NET is too low for this server.' -ForegroundColor Yellow
}
}
If ($Decision -eq '4.8') {
If ($DotNetVersion -lt '528049') {
Install-NET48
Write-Host 'Please make sure to reboot your server now.' -ForegroundColor Yellow
} else {
Write-Host 'This version of .NET is too low for this server.' -ForegroundColor Yellow
}
}
[/sourcecode]
Answer Checking
Now what if we add some lines, add some checks and provide feedback to a user so that they don’t waste time with wrong answers? We can do that with the code provided below. The sample is the same as above with the addition of a ‘Do While’ loop that looks to make sure the answer provided is correct. If the answer is not one of the ones on the list than an warning message is displayed and we are asked to provide another answer. Once a correct answer is entered, the script executed the request and then goes back to the main menu:
[sourcecode language=”powershell” collapse=’true’]
Do {
$Decision = Read-Host -Prompt "Which version of .Net do you want to install? [4.7, 4.7.1, 4.7.2 or 4.8? 'x' for main menu] "
If ($Decision -eq '4.7') {
$Valid = $True
If ($DotNetVersion -lt '460805') {
Install-NET470
Write-Host 'Please make sure to reboot your server now.' -ForegroundColor Yellow
} else {
Write-Host 'This version of .NET is too low for this server.' -ForegroundColor Yellow
}
}
If ($Decision -eq '4.7.1') {
$Valid = $True
If ($DotNetVersion -lt '461310') {
Install-NET471
Write-Host 'Please make sure to reboot your server now.' -ForegroundColor Yellow
} else {
Write-Host 'This version of .NET is too low for this server.' -ForegroundColor Yellow
}
}
If ($Decision -eq '4.7.2') {
$Valid = $True
If ($DotNetVersion -lt '461814') {
Install-NET472
Write-Host 'Please make sure to reboot your server now.' -ForegroundColor Yellow
} else {
Write-Host 'This version of .NET is too low for this server.' -ForegroundColor Yellow
}
}
If ($Decision -eq '4.8') {
$Valid = $True
If ($DotNetVersion -lt '528049') {
Install-NET48
Write-Host 'Please make sure to reboot your server now.' -ForegroundColor Yellow
} else {
Write-Host 'This version of .NET is too low for this server.' -ForegroundColor Yellow
}
}
If ($Decision -eq 'x') {
Write-Host 'Exiting .NET installation function …' -ForegroundColor Yellow
$Valid = $True
}
If (!$Valid) {
Write-Host '';;write-host '';Write-Host ' Invalid selection. Please choose again.' -ForegroundColor Yellow;write-host '';write-host ''
}
} While ($Valid -ne $True)
[/sourcecode]
What we notice is that a successful execution does NOT look different than the code in the first example. The difference is only when a wrong answer is entered, which is what we were coding for.
Conclusion
Providing a way to check for correct answers provides for a better experience overall and ensures that the operator pays attention to the choices. However, not all scenarios will allow this type of check. Other options may need to be put in place.