Home > other >  How to validate 2 inputs in Powershell and only proceed when both inputs are validated
How to validate 2 inputs in Powershell and only proceed when both inputs are validated

Time:11-18

Trying to write a script in Powershell that needs to validate 2 User Inputs before applying policies to the correct entries. With the basic script I wrote, It Validates the 1st Entry which is the User ID in the Teams Tenant. Then Validates the 2nd entry which is the telephone number using the validate regex. The problem I am having is that the policies Do not get applied when the correct information is applied. It skips that part and states a Warning and asks to check another profile.

Connect-MicrosoftTeams

 do {
    try {
    # 1st User Entry to check UserID
    $upnentry = Read-Host 'Enter the User Principle Name'
    
    # Validate in Teams Tenant if this ID exists, If Not, 
    # prompt to enter a valid entry again

    $csu = Get-CsOnlineUser -Identity $upnentry -ErrorAction Stop
    $csu | Format-List IsSipEnabled, Displ*
    Write-Host 'User ID has been verified correctly!'

   # 2nd User Entry to check for valid Telephone Number 
   
    $phoneinputblock = {
    try
    {
     [validatescript({[regex]::Match($_,'^27\d{9}$').Length -eq  11})]
     $phoneUserInput = Read-Host "`nEnter Telephone Number"
     $phoneUserInput
     }
     catch{ Write-Warning "Incorrect Format for Telephone Number!"
     & $phoneinputBlock
      }
    }
   $phoneuserInput = & $phoneinputBlock
   Write-Host 'Telephone Number is in the correct format!'

   Set-CsPhoneNumberAssignment -Identity $user -PhoneNumber 
   $phonenumberinput -PhoneNumberType DirectRouting
   Grant-CsOnlineVoiceRoutingPolicy -PolicyName VRP- International -Identity $upnentry
   Write-host "Policies applied successfully for : $upnentry"  - ForegroundColor Green
   }
   catch { Write-Warning "You've entered an invalid UserID: $upnentry"
   }
   } until($Host.UI.PromptForChoice('', 'Do you want to check another Users Profile?', 
   ('&Yes', '&No'), 0))

Error

CodePudding user response:

I would not perform the two tests inside nested try--catch blocks like that, but instead use a boolean variable to keep track of the result from the first test and only if that is true proceed with the rest.

Something like this:

Connect-MicrosoftTeams

 do {
    $testID = $false
    try {
        # 1st User Entry to check UserID
        $upnentry = Read-Host 'Enter the User Principle Name'

        # Validate in Teams Tenant if this ID exists, If Not, 
        # prompt to enter a valid entry again

        $csu = Get-CsOnlineUser -Identity $upnentry -ErrorAction Stop
        $csu | Format-List IsSipEnabled, Displ*
        Write-Host 'User ID has been verified correctly!'
        $testID = $true
    }
    catch { Write-Warning "You've entered an invalid UserID: $upnentry"}

    if ($testID) {
        # 2nd User Entry to check for valid Telephone Number 
        while ($true) {
            $phoneUserInput = Read-Host "`r`nEnter Telephone Number"
            if ([regex]::Match($phoneUserInput,'^27\d{9}$').Length -eq 11) {
                Write-Host 'Telephone Number is in the correct format!'
                break  # exit the loop
            }
            # here we have a wrong phone number, so ask again
            Write-Host "`r`nThe Telephone Number is in the wrong format. Should start with 27 followed by nine digits" -ForegroundColor Red
            if ((Read-Host "Do you want to stop here (Yes/No) ?") -match '^Y') { # exit the script
                exit
            }
        }
        # here we know both entries are correct, so proceed setting the phone number
        try {
            Set-CsPhoneNumberAssignment -Identity $upnentry -PhoneNumber $phoneUserInput -PhoneNumberType DirectRouting -ErrorAction Stop
            Grant-CsOnlineVoiceRoutingPolicy -PolicyName 'VRP- International' -Identity $upnentry -ErrorAction Stop
            Write-host "Policies applied successfully for: $upnentry"  - ForegroundColor Green
        }
        catch {
            Write-Warning "Error setting the phone number: $($_.Exception.Message)"
        }
    }
} until ($Host.UI.PromptForChoice('', 'Do you want to check another Users Profile?', ('&Yes', '&No'), 0))
  • Related