Im doing a Do Until loop by Restricting user input to email address or domain\ID in PowerShell using Read-Host. The condition is not working.
Original syntax:
do { $answer = Read-Host "yes or no" } until ("yes","no" -contains $answer)
Input:
$DL1 = "[email protected]"
$DL2 = "domain\HH"
$Restriction1 = $DL1.EndsWith('@domain.com')
$Restriction2 = $DL2.StartsWith('domain\')
Output:
True
Actual command:
Do {$DLO= Read-Host "Enter ID (Email Address or Domain\ID)"}
until ($DLO.EndsWith('@domain.com'),$DLO.StartsWith('Domain\') -match 'true' )
Why the condition is not working?
the reason your version fails is that you changed the basic structure of the
while
test from your source. plus, you ignored the fact that.StartsWith()
and.EndsWith()
are both case sensitive. that last means thatdomain
andDomain
are NOT the same thing. [grin]instead of untangling that, i rewrote the idea in a way that seems more obvious to me.
what it does ...
the one that ends with a slash won't work without escaping since that is a reserved character. the one that ends with
.com
won't always work without escaping since the dot is "any character" in the regex pattern language.while
loopnull or empty
stringif
tests for a domain idelseif
tests for an email addresselse
writes a warning & sets the input $Var to a blank string$RHInput
variablethe code ...
output while in the
while
...output after the
while
loop ...