enum failing when used as a type in param()

Welcome Forums General PowerShell Q&A enum failing when used as a type in param()

  • This topic has 14 replies, 3 voices, and was last updated 6 months ago by
    Participant
    .
Viewing 14 reply threads
  • Author
    Posts
    • #241607
      Participant
      Topics: 52
      Replies: 211
      Points: 889
      Helping Hand
      Rank: Major Contributor

      I’ve a Function that I use an enum to help check values passed to one of the parameters.

      I’ve declared the parameter in the function like this:

      The function is called like this:

      But when I run it, I get this error, see below, if I remove the enum and set the parameter to string, it works ok.

      What am I doing wrong?

       

    • #241613
      Senior Moderator
      Topics: 9
      Replies: 1445
      Points: 5,410
      Helping Hand
      Rank: Community MVP

      I am suspecting a type related issue. Can you try converting $subsystem to string as the passed value will be type casted to allowedsubsystem type the function which could be something not supported by the cmdlet that you are using. So you can try converting it to string.

      And then use $SubSystemString while calling the actual cmdlet.

    • #241616
      Participant
      Topics: 52
      Replies: 211
      Points: 889
      Helping Hand
      Rank: Major Contributor

      I did this, see below, but still failed…..same error.

    • #241808
      Moderator
      Topics: 5
      Replies: 244
      Points: 1,387
      Helping Hand
      Rank: Community Hero
    • #241841
      Participant
      Topics: 52
      Replies: 211
      Points: 889
      Helping Hand
      Rank: Major Contributor

      The command works ok, I’ve shown that by removing the enum from the paramter declaration.

      This works

      This fails

      The enum used is:

      I have the parameters declared like this:

    • #241862
      Participant
      Topics: 52
      Replies: 211
      Points: 889
      Helping Hand
      Rank: Major Contributor

      @ Kiran,

      I’ve not used ValidateSet in the parameter declaration, as i don’t want to have to put all these options in every Function. That, if I understand correctly, is what enum is for, so I can declare these options once, and there after refer to it in functions as required.

      The option of using ArgumentCompleter is not an option in this case, if I understand correctly what it’s for and how it works.

    • #241865
      Moderator
      Topics: 5
      Replies: 244
      Points: 1,387
      Helping Hand
      Rank: Community Hero

      You can also use the ValidateSet attribute like this as well…

    • #241919
      Senior Moderator
      Topics: 9
      Replies: 1445
      Points: 5,410
      Helping Hand
      Rank: Community MVP

      Whit I meant is to receive the input as enum then convert it to string before passing to actual cmdlet.

      Can you post the line 181 as mentioned by the stacktrace

    • #241928
      Participant
      Topics: 52
      Replies: 211
      Points: 889
      Helping Hand
      Rank: Major Contributor

      @ Kiran,

      Apparently ‘System.Management.Automation.IValidateSetValuesGenerator’ is only available in PS 6+

    • #241931
      Participant
      Topics: 52
      Replies: 211
      Points: 889
      Helping Hand
      Rank: Major Contributor

      @ Kvprasoon

      Line 181 in the try in the try .. catch that holds the create

      This is the process step of the function

       

    • #242018
      Senior Moderator
      Topics: 9
      Replies: 1445
      Points: 5,410
      Helping Hand
      Rank: Community MVP

      Thanks for sharing the snippet, so what I was trying to convey is to do the below.

    • #242099
      Participant
      Topics: 52
      Replies: 211
      Points: 889
      Helping Hand
      Rank: Major Contributor

      Yes, got that, and tried it, and it failed with the same error.

    • #242102
      Participant
      Topics: 52
      Replies: 211
      Points: 889
      Helping Hand
      Rank: Major Contributor

      @ kvprasoon,

      Not sure what happened the first time I attempted that but I’ve retired it multiple times now and each time it’s worked and created the step.

      Nice to get it to working, but it’d be great for me to understand why this was an issue…? The parameter was accepted fine without the enum being used.

    • #242198
      Senior Moderator
      Topics: 9
      Replies: 1445
      Points: 5,410
      Helping Hand
      Rank: Community MVP

      Form what I understood, $NewStep.SubSystem requires the type to be specific and can only type cast from a string type, so custom enum type cannot be converted to the expected Microsoft.SqlServer.Management.Smo.Agent.AgentSubSystem type.

    • #242309
      Participant
      Topics: 52
      Replies: 211
      Points: 889
      Helping Hand
      Rank: Major Contributor

      Thanks!

      I appreciate the help and the info.

Viewing 14 reply threads
  • The topic ‘enum failing when used as a type in param()’ is closed to new replies.