Sorry, I should have explicitly said that this was an example of how to validate input based on a range.
The "10" problem can be fixed by converting the input to an integer. But that doesn't solve the problem of a user entering an alpha character. It all depends on how much validation that you want in your script.
Is this any better? I added an alpha test.
$ArrayIndex = 0 #Equivalent to the index value to each line in the array
$ISOFile = Get-ChildItem -Path $ISOFileDirectory | Where-Object -Property Name -Like "*.iso" | `
Select-Object -Property Name, @{ Name = "ID" ; Expression = { $global:ArrayIndex; $global:ArrayIndex++ } }
$ISOFileIndex = $ISOFile.Count
$ISOFile | format-table
While ($true) {
$SelectArrayIndex = Read-Host "Select an ID less than $($ISOFile.Count)"
if ($SelectArrayIndex -notmatch "^\d+$") {
Write-Host "Please enter a number. Try Again" -ForegroundColor Red
continue
}
$SelectArrayIndex = [int]$SelectArrayIndex # Insure an integer
if ($SelectArrayIndex -lt $ISOFile.Count) {
Write-Host "Thank you for selecting a correct ID." -ForegroundColor Green
break
}
Write-Host "Invalid ID. Try Again" -ForegroundColor Red
}
Write-Host "You selected $SelectArrayIndex" -ForegroundColor Green