Home > database >  Powershell Script Re-Prompt User Input until valid and provide options to Enter Input again
Powershell Script Re-Prompt User Input until valid and provide options to Enter Input again

Time:10-13

I'm a Beginner with Powershell. The intention of the script is to prompt a Teams Admin to enter the email address of a User and can extract desired information. Some of the features desired features in the script work but need help with fixing all features.

So what is working is as follows:

  1. Login Successfully to MS Teams Tenant
  2. When a valid email address is entered, the result is displayed as expected
  3. When a non-valid email address is entered 1st time, it will state invalid entry and ask you to enter the correct email address, however, if u enter a non-valid email address 2nd or third time it will state "User ID Verified, printing results....", give an error and asks to check another Users profile. Now, if you select Y, it will prompt for UserID again BUT exits the script.

Request:

  1. How do you loop through the read-hosts until a valid entry is entered and when prompted to check another profile, loop read-hosts again until a valid entry is entered?

  2. How do you hide the Error given by the application (Red Writing)

 Error Screenshot

Connect-MicrosoftTeams
# Prompt for UserID
$upninput = "Enter the User Principle Name: "
$upnentry = Read-Host  $upninput

# Check if the User ID exists on MS Teams Tenant
$checkupn = Get-CsOnlineUser -Identity $upnentry  | Where-Object ($_.UserPrincipalName -eq 
$upnentry)

# Loop until correct UserID is entered
while ($upnentry -ne $checkupn){
$upnentry = Read-Host "You've entered an invalid UserID. $upninput"

Write-Host 'User ID has been verified, printing results on screen!' -ForegroundColor 
Green

# Extract information from the Users Profile 
Get-CsOnlineUser $upnentry | Format-List IsSipEnabled, Displ*, UserPri*, SipA*, LineURI, 
TeamsUpgradeE*, Enterprise*, TenantD*,  Feature*, AssignedPlan, OnlineVoiceR*, OnlineAudio*, 
TeamsCall*, CallingLine*, TeamsEmergency*
    
# Option to check another Users Profile or Return to the Menu
Do { $response = Read-Host "`nDo you want to check another Users Profile? Enter Y/N"}
until(($response -eq "Y") -or ($response -eq 'N'))
if ( $response -eq "Y" ) {"Please Enter UPN: "; $upninput}
else { Write-Host "You are done with this task!" }
break
}

CodePudding user response:

You should keep it simple and use PSHostUserInterface.PromptForChoice to ask if the user wants to query a new user. In addition, you can implement a try / catch statement if the user enters an invalid principal.

do {
    try {
        $upn = Read-Host 'Enter the User Principle Name'
        # if the UPN is invalid, this errors and goes to the `catch` block
        $csu = Get-CsOnlineUser -Identity $upn -ErrorAction Stop
        # if above didn't error, then we're here, hence display to console
        Write-Host 'User ID has been verified, printing results on screen!' -ForegroundColor Green
        $csu | Format-List IsSipEnabled, Displ*, UserPri*, SipA*, LineURI, TeamsUpgradeE*,
                           Enterprise*, TenantD*,  Feature*, AssignedPlan, OnlineVoiceR*, OnlineAudio*,
                           TeamsCall*, CallingLine*, TeamsEmergency*
    }
    catch {
        # display this warning if failed and ask to check other profile
        Write-Warning "You've entered an invalid UserID. $upn"
    }
    # loop until choice is 1 (No!)
} until($Host.UI.PromptForChoice('', 'Do you want to check another Users Profile?', ('&Yes', '&No'), 0))
  • Related