Hi @all,
I’m new to PowerShell and currently I’m struggling with parameter passing to my first script.
Depending on a passed parameter (-d) I want to start an external (.exe) program.
The following combinations should be possible:
- without parameter -d: C:\xyz\Script\ParamTest.ps1
- with parameter -d but no value (use the default): C:\xyz\Script\ParamTest.ps1 -d
- with parameter-d and 1 value: C:\xyz\Script\ParamTest.ps1 -d 10
- with parameter-d and 2 value2: C:\xyz\Script\ParamTest.ps1 -d 10,20
Whereby for:
- the external program should not be called,
- the external program will be called with default values,
- the external program will be called with the value passed and a default value
- the external program will be called with the values passed
I’ve read several pages on the Internet and - based on these information – I’ve tried several combinations for Param(…):
Variant | w/o -d | -d | -d 10 | -d 10,20 |
[Parameter(Mandatory=$false)] [array]$d = @(-1,-1) | Ok | X | Ok | Ok |
[ValidateCount(0,2)] | Ok | X | Ok | Ok |
[AllowEmptyCollection()] | Ok | X | Ok | Ok |
[AllowEmptyArray()] | X | X | X | X |
[AllowNull()] | Ok | X | Ok | Ok |
[ValidateScript({return($true)})] | Ok | X | Ok | Ok |
Ok = worked
X = didn’t work (Exception)
The problem is always the specification of –d without any value (= Exception).The use of [ScriptBlock], DynamicParam and ParameterSet seems not to solve the a.m. problem either (?).
Is somebody out there with an idea about how to solve that problem?
Kind regards