0

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 = "HH@domain.com"
$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?

James Z
  • 12,209
  • 10
  • 24
  • 44
HGB 85
  • 3
  • 2
  • why do you expect the result to be the STRING `true`? also, what do you get with two booleans connected with a comma? – Lee_Dailey May 13 '20 at 23:43
  • also also, the `.StartsWith()` method is _case sensitive_ ... and your `$Restriction2` test will be false since `domain` does not match `Domain`. – Lee_Dailey May 13 '20 at 23:49
  • Expectation is to accept only email address (eg.arun@gmail.com) or ID (eg. gmail/123456) where gmail.com is the domain. If it is a different domain - would prefer to have do loop continued until user provide correct values – HGB 85 May 13 '20 at 23:57
  • Im not sure what condition to be used. Ask would be to accept the value if either $Restriction1 or $Restriction2 is true inorder to process the input to next section – HGB 85 May 14 '20 at 00:01
  • yes, but your code is _deeply odd_. [*grin*] [1] the test has two booleans connected by a command and then tests them with `-match "true"`. testing for the STRING `true` instead of the boolean `$True` is quite odd. [2] two booleans connected by a comma is VERY ODD. you would normally test with either an `-and` or with an `-or` depending on if you want both or only one to be true. [3] the items you are testing are _case sensitive_ and your earlier test mixes `domain` and `Domain` ... those cannot match in a case sensitive test. – Lee_Dailey May 14 '20 at 00:01
  • The loop is not working if I use -or while it should loop back until I either provide input as xyz@domain.com or domain\xyz Can you advise what I am missing – HGB 85 May 14 '20 at 00:07
  • Template: that I am referring and changing is do { $answer = Read-Host "yes or no" } until ("yes","no" -ccontains $answer) – HGB 85 May 14 '20 at 00:12
  • please take a look at my Answer. i think it covers what you need. it is longer, but it seems more obvious to me. – Lee_Dailey May 14 '20 at 04:04
  • Thank you so much :-) eeeeeeee. I will test it today and confirm back – HGB 85 May 14 '20 at 12:43
  • Hi Lee Dailey, Can I have this in Do Until format? – HGB 85 May 14 '20 at 15:55
  • is there a problem with the `while` version? does it not work? ///// the only difference between `do/until` and `while` is that the test is done _first_ with the `while` and _after_ the loop with the `do/until`. so ... yes, it can be rewritten as such. however, i severely dislike the `do/until` structure, so i will leave rewriting it to you. [*grin*] – Lee_Dailey May 14 '20 at 16:53

1 Answers1

0

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 that domain and Domain 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 ...

  • creates the constants
  • creates regex-escaped versions of the test strings
    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.
  • sets the input variable to a blank string
  • starts a while loop
  • test for a null or empty string
  • gets the user input
  • if tests for a domain id
  • elseif tests for an email address
  • else writes a warning & sets the input $Var to a blank string
  • after the while loop exits, shows the content of the $RHInput variable

the code ...

$DomainName = 'Ziggity'
$EmailSuffix = '@{0}.com' -f $DomainName
$ES_Regex = [regex]::Escape($EmailSuffix)
$UserNamePrefix = '{0}\' -f $DomainName
$UNP_Regex = [regex]::Escape($UserNamePrefix)

$RHInput = ''
while ([string]::IsNullOrEmpty($RHInput))
    {
    $RHInput = Read-Host ('Enter an ID [ UserName@{0} or {1}UserName ] ' -f $EmailSuffix, $UserNamePrefix)
    if ($RHInput -match "^$UNP_Regex")
        {
        'You entered a domain ID [ {0} ]' -f $RHInput
        }
        elseif ($RHInput -match "$ES_Regex$")
        {
        'You entered an email address [ {0} ]' -f $RHInput
        }
        else
        {
        Write-Warning (    '[ {0} ] is not a valid email address OR a valid domain ID.' -f $RHInput)

        $RHInput = ''
        }
    }

''
$RHInput

output while in the while ...

# 1st run thru
Enter an ID [ UserName@@Ziggity.com or Ziggity\UserName ] : testing@ziggity.com
You entered an email address [ testing@ziggity.com ]

# 2nd run thru
Enter an ID [ UserName@@Ziggity.com or Ziggity\UserName ] : testing\tutu
WARNING: [ testing\tutu ] is not a valid email address OR a valid domain ID.
Enter an ID [ UserName@@Ziggity.com or Ziggity\UserName ] : ziggity\testing
You entered a domain ID [ ziggity\testing ]

output after the while loop ...

# 1st run thru
testing@ziggity.com

# 2nd run thru
ziggity\testing
Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26