Try catch throw question

Welcome Forums General PowerShell Q&A Try catch throw question

Viewing 7 reply threads
  • Author
    Posts
    • #194972
      Participant
      Topics: 24
      Replies: 59
      Points: 204
      Rank: Participant

      Greetings experts!

      Can’t figure out how to return to reloop through the try catch throw when a incorrect value is entered. Currently upon entering an incorrect value the validation test displays an error message. A correct works with no issues.

      Thank you in advance for your inputs.

      Norm

    • #194978
      Moderator
      Topics: 5
      Replies: 237
      Points: 1,320
      Helping Hand
      Rank: Community Hero

      Hello Norm,

      In the validation script, you don’t need to catch the error, if it is not validated as per the logic then the script itself throws an error like this…

      Thank you.

       

    • #194981
      Participant
      Topics: 24
      Replies: 59
      Points: 204
      Rank: Participant

      Hello Kiran,
      Thank you for your reply. As usual not making myself clear if I enter the following:
      10:31AM D:\PowerShell_Test\powercli> FOO -DCChoice xxxx wvhq
      FOO : Cannot validate argument on parameter ‘DCChoice’. Datacenter 12/23/2019 10:32:04 AM Get-Datacenter Datacenter with name ‘xxxx’ was not found using the specified
      filter(s). not found. Please enter a valid Datacenter Name.
      At line:1 char:16
      + FOO -DCChoice xxxx wvhq
      + ~~~~
      + CategoryInfo : InvalidData: (:) [FOO], ParameterBindingValidationException
      + FullyQualifiedErrorId : ParameterArgumentValidationError,FOO

      Would like to have the validation test restart giving the user another opportunity to enter a correct value without having to restart the function. Hope this makes some sense. Maybe what I’m asking for is not possible

      Norm

    • #195002
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

      If you want a re-prompt, that is normally done outside the function like so:

    • #195005
      Participant
      Topics: 24
      Replies: 59
      Points: 204
      Rank: Participant

      Rob,
      Don’t understand how to incorporate your response into the function, are you saying I have to somehow call the function in the try catch?

      Thank you for your input

      Norm

    • #195023
      Participant
      Topics: 0
      Replies: 28
      Points: 144
      Helping Hand
      Rank: Participant

      Normally I don’t do the retry within the function, I ask the function to return a status message as a hashtable and performs the retry in the script.
      Based on your scenario, probably I would try something like this.

    • #195026
      Moderator
      Topics: 5
      Replies: 237
      Points: 1,320
      Helping Hand
      Rank: Community Hero

      Instead of using the validate script you can use dynamic validate set, get all the valid values and use them in the validate set, please refer to the link below…

      https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-6#dynamic-validateset-values

    • #195272
      Participant
      Topics: 24
      Replies: 59
      Points: 204
      Rank: Participant

      Hello rejikodiyil

      What you have is what I’m except the RETRY doesn’t work when I press retry nothing happens the abort option works perfectly. DO you have sometime to help me with the retry option,is there a way to return to start over again allowing the user to retry entering the correct datacenter?

      Thank you for your input

      Norm

Viewing 7 reply threads
  • The topic ‘Try catch throw question’ is closed to new replies.