Do Until loop by Restricting user input to email address or domain\ID in PowerShell using Read-Host

330 Views Asked by At

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?

1

There are 1 best solutions below

0
On

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 ] : [email protected]
You entered an email address [ [email protected] ]

# 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
[email protected]

# 2nd run thru
ziggity\testing