PowerCLI

 View Only
  • 1.  Get Datacenter Try catch user prompt retry question

    Posted Dec 26, 2019 09:22 PM

    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

          }



  • 2.  RE: Get Datacenter Try catch user prompt retry question

    Posted Dec 26, 2019 09:33 PM

    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.



  • 3.  RE: Get Datacenter Try catch user prompt retry question

    Posted Dec 26, 2019 09:40 PM

    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



  • 4.  RE: Get Datacenter Try catch user prompt retry question
    Best Answer

    Posted Dec 26, 2019 09:43 PM

    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

    }



  • 5.  RE: Get Datacenter Try catch user prompt retry question

    Posted Dec 27, 2019 08:46 PM

    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