Get back to Read-Host if invalid

Jonathan Yang 101 Reputation points
2021-08-26T00:16:28.173+00:00

Hi -

I'm trying loop Read-Host "Select ID" each type the value is not correct, but I could not make it work.

$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
$SelectArrayIndex = Read-Host "Select ID"
While ($SelectArrayIndex -gt $ISOFileIndex) {
$SelectArrayIndex = Read-Host "Select ID"
Write-Host "Invalid ID" -ForegroundColor Red
If ($SelectArrayIndex -lt $ISOFileIndex) {
Write-Host $ISOFile[$SelectArrayIndex].Name was selected -ForegroundColor Yellow
Break
}
}

I just copied the structure of While somewhere and I think I'm missing something to make it work.

Help please.

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 36,291 Reputation points
    2021-08-27T01:23:43.817+00:00

    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
    

3 additional answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2021-08-26T01:38:36.42+00:00

    Does this help?

    While ($true)  {
            $SelectArrayIndex = Read-Host "Select an ID between 2 and 6"
            if (($SelectArrayIndex  -gt 1) -and ($SelectArrayIndex  -lt 7)) {
                Write-Host "Thank you for selecting a correct ID."
                break
            }
            Write-Host "Invalid ID" -ForegroundColor Red
    }
    Write-Host "You selected $SelectArrayIndex"
    

  2. Rich Matheisen 47,901 Reputation points
    2021-08-27T01:34:48.017+00:00

    Your code just needed a little tweaking:

    $ArrayIndex = 0 #Equivalent to the index value to each line in the array
    $ISOFile = Get-ChildItem -Path $ISOFileDirectory -Include *.iso |
        Select-Object -Property Name, @{ Name = "ID" ; Expression = { $global:ArrayIndex; $global:ArrayIndex++ } }
    $ISOFileCount = $ISOFile.Count
    While ($true) {
        $SelectArrayIndex = Read-Host "Select an ID from 1 to $ISOFileCount"
        if (($SelectArrayIndex -ge 1) -and ($SelectArrayIndex -le $ISOFileCount)) {
            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
    
    0 comments No comments

  3. Limitless Technology 39,916 Reputation points
    2021-09-01T11:48:12.497+00:00

    Hi there ,

    You can have try at this an by changing certain parameters .

    $ArrayIndex = 0 #Equivalent to the index value to each line in the array
    $ISOFile = Get-ChildItem -Path $ISOFileDirectory -Include *.iso |
    Select-Object -Property Name, @{ Name = "ID" ; Expression = { $global:ArrayIndex; $global:ArrayIndex++ } }
    $ISOFileCount = $ISOFile.Count
    While ($true) {
    $SelectArrayIndex = Read-Host "Select an ID from 1 to $ISOFileCount"
    if (($SelectArrayIndex -ge 1) -and ($SelectArrayIndex -le $ISOFileCount)) {
    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

    Hope this Answers all your queries , if not please do repost back .
    If an Answer is helpful, please click "Accept Answer" and upvote it : )


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.