Search code examples
c#powershellpowershell-module

Binary powershell module command formatting


I'm writing a binary powershell module that will replace another command line tool that just outputs text.

I have a handful of commands that follow the powershell verb guide (eg. Add-MyModuleUser -UserName "My Username") but I would like to also have an overloaded command that takes the object responses of these and format them like the tool I'm replacing which accepts input in the following format:

myModule addUserName "My Username"

My instinct says to have a command called Invoke-MyModule with an alias to myModule, so I could call Invoke-MyModule addUserName "My Username" which would call Add-MyModuleUser and format that output with like the original tool.

Question: How can I get a powershell command to accept input like this?

Note: I was reading about ParameterSets which seem promising, I was thinking I could declare the parameter set to string, so the first word in the string sets the ParameterSet for the rest of the parameters that are entered.

Thanks!


Solution

  • If the remaining parameters are based on the value of a parameter rather than the presence of a parameter, you probably need to use "Dynamic Parameters". See "get-help about_functions_advanced_parameters", and the dynamic parameter content is towards the end.

    Parametersets would be fine if you used a series of switch parameters (-AddUserName, for example) rather than the value "addUserName". ParameterSets are chosen by the presence of parameters that are exclusive to a particular set.