VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Invalid Selection

Hi,

I am unable to select from the multiple options as I am getting invalid selection, even If I am entering correct parameters

cls   

do

{

Write-Host "      1. test volume" 

Write-Host "      2. DS_Local_Vol_1"

Write-Host "      3. Exit"

Write-Host

$answer = read-host "Select the desired option (Ex.1)"

$answer = $choice -match '^[123]+$'

          if ( -not $answer) { write-host "Invalid selection" }

    } until ( $answer )

if ($answer -eq 1)

{

$volname = 'test'

if ($answer -eq 2)

{

$volname = 'DS_Local_Vol_1'

if ($answer -eq 3){Exit}

Please help!!!!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Your RegEx will not only match 1 but also 11, due to the + in there.

Your changing the $answer to a Boolean with the -match expression.

Try like this

do

{

    Write-Host "      1. test volume"

    Write-Host "      2. DS_Local_Vol_1"

    Write-Host "      3. Exit"

    Write-Host

    $answer = Read-Host "Select the desired option (Ex.1)"

    $correct = $answer -match '^[123]$'

    if (-not $correct) {

        write-host "Invalid selection"

    }

} until ( $correct )

if ($answer -eq 1)

{

    $volname = 'test'

}

elseif ($answer -eq 2)

{

    $volname = 'DS_Local_Vol_1'

}

elseif ($answer -eq 3){

    Exit

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Your RegEx will not only match 1 but also 11, due to the + in there.

Your changing the $answer to a Boolean with the -match expression.

Try like this

do

{

    Write-Host "      1. test volume"

    Write-Host "      2. DS_Local_Vol_1"

    Write-Host "      3. Exit"

    Write-Host

    $answer = Read-Host "Select the desired option (Ex.1)"

    $correct = $answer -match '^[123]$'

    if (-not $correct) {

        write-host "Invalid selection"

    }

} until ( $correct )

if ($answer -eq 1)

{

    $volname = 'test'

}

elseif ($answer -eq 2)

{

    $volname = 'DS_Local_Vol_1'

}

elseif ($answer -eq 3){

    Exit

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very much for your quick help Smiley Happy

0 Kudos