Robert Strauch
Robert Strauch

Reputation: 12876

Handling boolean values for String parameters in Powershell

Using Powershell I'd like to call my script as follows:

myscript.ps1 -device1 enable -device2 disable -device3 enable

Each device paramenter is defined as String followed by a bool value.

param(
  [string] $device1,
  [string] $device2,
  [string] $device3
)

Does PowerShell support this with some predefined functions or parameters or would you implement this in a totally different way? I'd like to avoid a parsing for enable and disable.

Upvotes: 2

Views: 484

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 58931

I would implement this using a switch:

param(
  [switch] $device1,
  [switch] $device2,
  [switch] $device3
)

So you can invoke your script using:

myscript.ps1 -device1 -device3

Upvotes: 4

Related Questions