0

How to pass value along with parameter? Something like ./test.ps1 -controllers 01. I want the script to use hyphen and also a value is passed along for the parameter.

Here is the part of the script I wrote. But if I call the script with hyphen (.\test.ps1 -Controllers) it says A parameter cannot be found that matches parameter name 'Controllers'.

param(
   # [Parameter(Mandatory=$false, Position=0)]
    [ValidateSet('Controllers','test2','test3')]
    [String]$options

)

Also I need to pass a value to it which is then used for a property.

if ($options -eq "controllers")
{
   $callsomething.$arg1 | where {$_ -eq "$arg2" }
}
  • 2
    That would be either `.\test.ps1 -options Controllers` or `.\test.ps1 Controllers` – Mathias R. Jessen Apr 18 '20 at 16:12
  • Okay what about passing a value? Should I create another param for $arg2? or is there another way? – user13339470 Apr 18 '20 at 16:21
  • Read up on the [CmdLetBinding](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_cmdletbindingattribute?view=powershell-7) attribute. You can add this attribute to a function or to a script file. Once you do that, you can call the function or script file using the same syntax you use with built in cmdlets. This includes passing parameters by name or by position. – Walter Mitty Apr 19 '20 at 13:44
  • Also, check out [This question and answer](https://stackoverflow.com/questions/14671051/what-is-cmdletbinding-and-how-does-it-work). – Walter Mitty Apr 19 '20 at 13:51

1 Answers1

0

Lets talk about why it does not work

function Test()
    param(
        [Parameter(Mandatory=$false, Position=0)]
        [ValidateSet('Controllers','test2','test3')]
        [String]$options
    )
}

Parameters are Variables that are created and filled out at the start of the script

ValidateSet will only allow the script to run if $Options equals one of the three choices 'Controllers','test2','test3'

Lets talk about what exactly all the [] are doing

Mandatory=$false means that $options doesnt have to be anything in order for the script to run.

Position=0 means that if you entered the script without using the -options then the very first thing you put would still be options

Example

#If Position=0 then this would work
Test "Controllers"
#Also this would work
Test -options Controllers

[ValidateSet('Controllers','test2','test3')] means that if Option is used or is Mandatory then it has to equal 'Controllers','test2','test3'

It sounds like you are trying to create parameters at runtime. Well that is possible using DynamicParam.

function Test{
    [CmdletBinding()]
    param()
    DynamicParam {
        $Parameters  = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
        'Controllers','test2','test3' | Foreach-object{
            $Param  = New-Object System.Management.Automation.ParameterAttribute
            $Param.Mandatory  = $false
            $AttribColl = New-Object  System.Collections.ObjectModel.Collection[System.Attribute]
            $AttribColl.Add($Param)
            $RuntimeParam  = New-Object System.Management.Automation.RuntimeDefinedParameter("$_",  [string], $AttribColl)
            $Parameters.Add("$_",  $RuntimeParam) 
        }
        return $Parameters
    }
    begin{
        $PSBoundParameters.GetEnumerator() | ForEach-Object{
            Set-Variable $_.Key -Value $_.Value
        } 
    }
    process {

        "$Controllers $Test2 $Test3"

    }
}

DynamicParam allows you to create parameters in code. The example above turns the array 'Controllers','test2','test3' into 3 separate parameters.

Test -Controllers "Hello" -test2 "Hey" -test3 "Awesome"

returns

Hello Hey Awesome

But you said you wanted to keep the hypen and the parameter

So the line

$PSBoundParameters.GetEnumerator() | ForEach-Object{
    Set-Variable $_.Key -Value $_.Value
}

allows you to define each parameter value. a slight change like :

$PSBoundParameters.GetEnumerator() | ForEach-Object{
    Set-Variable $_.Key -Value "-$($_.Key) $($_.Value)"
}

Would return

-Controllers Hello -test2 Hey -test3 Awesome
ArcSet
  • 6,518
  • 1
  • 20
  • 34