Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2314

Parameter passing to a script

$
0
0

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:

  1. without parameter -d: C:\xyz\Script\ParamTest.ps1
  2. with parameter -d but no value (use the default): C:\xyz\Script\ParamTest.ps1 -d
  3. with parameter-d  and 1 value: C:\xyz\Script\ParamTest.ps1 -d 10
  4. with parameter-d  and 2 value2: C:\xyz\Script\ParamTest.ps1 -d 10,20

Whereby for:

  1. the external program should not be called,
  2. the external program will be called with default values,
  3. the external program will be called with the value passed and a default value
  4. 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


Viewing all articles
Browse latest Browse all 2314


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>