Can somebody please explain to me the odd results when I run this PowerShell script:
function Get-GoalQuantity {
$loop = $true
while ($loop) {
Write-Host ""
$quantity = Read-Host -Prompt "Enter Goal Quantity (1-15)"
if ($quantity -match '^\d+$') {
if (($quantity -gt 0) -and ($quantity -le 15)) {
$loop = $false
return $quantity
}
}
Write-Host "Try again"
}
}
While (1 -gt 0) {
$testQty = Get-GoalQuantity
Write-Host $testQty
}
It seems pretty straightforward, 0 and anything greater than 15 get rejected. As do any alphabetic entries. 1 and 10-15 are accepted correctly. But 2-9 are rejected, which makes no sense whatsoever. It's got something to do with it being a function. When coded as not a function it works correctly. I've checked that there are no spaces or other characters injected into the quantity value.
What PowerShell rule or oddity am I missing?
Any assistance would be appreciated.