VMware Cloud Community
nlong12
Enthusiast
Enthusiast
Jump to solution

Get Datacenter Try catch user prompt retry question

Greetings experts

Trying to get the following function retry option to work.  The Abort works but no matter what combinations I've tried can't get the Retry (start the entry process again) to work.  Right now the Retry option does nothing, click it over and over and nothing.   This is beyond my ability to figure out

Any input would be greatly appreciated.

Norm

function FOO1

{

[CmdletBinding()]

param(

    [Parameter(Mandatory,HelpMessage="Enter a Datacenter Name")]

    [String]$DCChoice

    )

    $exitFlag = $False

    Do

   

    {

        Try

        {

            $DC = Get-DataCenter -Name $DCChoice -ErrorAction Stop

            $exitFlag = $true

        }

        Catch

        {

            $DC = $null

            $title = "Invalid DataCenter Name"

            $prompt = "$DCChoice Invalid DataCenter Name, Would you like to [A]bort or [R]etry?"

            $choices = @('&Abort','&Retry')

            $choice = $host.ui.PromptForChoice($title,$prompt,$choices,0)      

            If($choice -eq 0){$exitFlag = $true}

            ##If($choice -ne 0){$exitFlag = $False}

        }

    }Until($exitFlag -gt 0)

    If([String]::IsNullOrEmpty($DC))

    {

       Return 'NotProcessed'

      }

      $DCChoice

      }

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

If you want to be prompted again for the parameter value (with the HelpMessage text), you will have to code that inside the function.
A missing parameter value is only prompted once when you enter the function.


You could do something like this

function FOO1

{

[CmdletBinding()]

param(

    [Parameter(Mandatory,HelpMessage="Enter a Datacenter Name")]

    [String]$DCChoice

    )


    $exitFlag = $False

    Do

    {

        Try

        {

            $DC = Get-DataCenter -Name $DCChoice -ErrorAction Stop

            $exitFlag = $true

        }

        Catch

        {

            $DC = $null

            $title = "Invalid DataCenter Name"

            $prompt = "$DCChoice Invalid DataCenter Name, Would you like to [A]bort or [R]etry?"

            $choices = @('&Abort','&Retry')

            $choice = $choices[$host.ui.PromptForChoice($title,$prompt,$choices,0)]   

            If($choice -eq '&Abort'){

                $exitFlag = $true

            }

            if($choice -eq '&Retry'){

                $dcChoice = Read-Host "Enter new datacentername"

            }

        }

    }Until($exitFlag)


    If([String]::IsNullOrEmpty($DC))

    {

       Return 'NotProcessed'

    }


    $DCChoice

}


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

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

What should the Retry option do?
Prompt for a new Datacenter name?
The code you provided will try the Get-Datacenter cmdlet again, but with the same value from the DCChoice parameter.


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

Reply
0 Kudos
nlong12
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

Yes  the retry option should prompt for a new datacenter name, wouldn't the setting of $DC to null prevented that from occurring?   You know a whole lot more than I do.

Norm

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you want to be prompted again for the parameter value (with the HelpMessage text), you will have to code that inside the function.
A missing parameter value is only prompted once when you enter the function.


You could do something like this

function FOO1

{

[CmdletBinding()]

param(

    [Parameter(Mandatory,HelpMessage="Enter a Datacenter Name")]

    [String]$DCChoice

    )


    $exitFlag = $False

    Do

    {

        Try

        {

            $DC = Get-DataCenter -Name $DCChoice -ErrorAction Stop

            $exitFlag = $true

        }

        Catch

        {

            $DC = $null

            $title = "Invalid DataCenter Name"

            $prompt = "$DCChoice Invalid DataCenter Name, Would you like to [A]bort or [R]etry?"

            $choices = @('&Abort','&Retry')

            $choice = $choices[$host.ui.PromptForChoice($title,$prompt,$choices,0)]   

            If($choice -eq '&Abort'){

                $exitFlag = $true

            }

            if($choice -eq '&Retry'){

                $dcChoice = Read-Host "Enter new datacentername"

            }

        }

    }Until($exitFlag)


    If([String]::IsNullOrEmpty($DC))

    {

       Return 'NotProcessed'

    }


    $DCChoice

}


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

Reply
0 Kudos
nlong12
Enthusiast
Enthusiast
Jump to solution

Hello Lucd,

As usual your suggestion worked perfectly.  Thank you for the tip regarding the parameter value coding suggestion.  This led me to modify another prompt in the second half of the function.

Again many thanks!!

Norm

Reply
0 Kudos