function test
{
[CmdletBinding()]
param
(
[Parameter(
Mandatory = $true,
ParameterSetName = "MyArgument"
)]
[ValidateSet("Value1", "Value2")]
[string[]]
$MyArguments
)
foreach ($MyArgument in $MyArguments)
{
switch ($MyArgument)
{
Value1
{
write-host "$parameter used"
}
Value2
{
write-host "$parameter used"
}
}
}
}
test -MyArgument Value1, Value2
How to get the actual argument passed by parameter if we use switch? I tried to catch it by $MyInvocation.BoundParameters.Values and $PSCmdlet.MyInvocation.InvocationName, but failed.
I want to get instead of a variable $parameter Value1 if we call Value1, and Value2 if we call Value2, but all approaches receive all values at once.
Thanks in advance.
Write-Host "$_ used"orWrite-Host "$MyArgument used"? You're switching on$MyArgument, the value you're switching on can be represented as$_in the action block of a switch statement.$MyInvocation, Get-Command -Name $MyInvocation.MyCommand, Get-PSCallStack $MyInvocation.BoundParameters.Values, $PSCmdlet.MyInvocation.InvocationName, $ParameterList = (Get-Command -Name $MyInvocation.MyCommand).Parameters, $ParameterList["Variable"].Attributes.ValidValues