0

I am trying to determine the order in which the user enters parameters. So, using the following example:

function RepeatParamOrder {
    [CmdletBinding()]
    param
    (
        [bool]$param1=$false,
        [bool]$param2=$false,
        [bool]$param3=$false
    )

    Write-Output <order user entered params>
}

If the user were to enter the following:

C:\>RepeatParamOrder -param3 true -param1 true

I would ideally like to capture the following order in some delimited format: (param3,param1)

The reason order is important is there are many, many parameters the user can enter and the order they enter parameters is the order a RETURN array will output values associated with those parameters. So, in this example, a 2-D array of arrays with data associated with (param3,param1) is returned and param2 is ignored since the user does not desire that information.

I have been searching well over an hour for a solution that would allow for the script to read the order parameters as entered but have had no luck finding a solution. I also searched for how to simply read the entire line C:\>RepeatParamOrder -param3 true -param1 true so that I could create a loop to parse out the information. And, while it seems basic that there should be a command that simply returns the text of the line I cannot find that solution either. I have looked through Get-Content and Read-Host but they do not seem to provide a solution (i.e. once the user enters parameter values, the function/script will do everything else without additional user input being required - such as providing order beyond the order the named parameters are already entered).

There must be an obvious solution but I am either searching the wrong keywords or misinterpreting a solution (e.g. maybe Read-Host is the solution but I am not grasping it correctly to extract information from the original user parameter input).

1 Answer 1

0

I think you're looking for the properties of $MyInvocation. Try the following:

function RepeatParamOrder {
    [CmdletBinding()]
    param
    (
        [bool]$param1=$false,
        [bool]$param2=$false,
        [bool]$param3=$false
    )

    Write-Output $MyInvocation.Line
}
2
  • Thank you! I can work with this.
    – Brian
    Commented Apr 11, 2020 at 5:50
  • You're welcome. Another option may be the $PSBoundParameters automatic variable, but I'm not sure there's any guarantee that order is preserved. Commented Apr 11, 2020 at 5:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .