Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a switch parameter to another PowerShell script?

I have two PowerShell scripts, which have switch parameters:

compile-tool1.ps1:

[CmdletBinding()] param(   [switch]$VHDL2008 )  Write-Host "VHDL-2008 is enabled: $VHDL2008" 

compile.ps1:

[CmdletBinding()] param(   [switch]$VHDL2008 )  if (-not $VHDL2008) { compile-tool1.ps1            } else { compile-tool1.ps1 -VHDL2008  } 

How can I pass a switch parameter to another PowerShell script, without writing big if..then..else or case statements?

I don't want to convert the parameter $VHDL2008 of compile-tool1.ps1 to type bool, because, both scripts are front-end scripts (used by users). The latter one is a high-level wrapper for multiple compile-tool*.ps1 scripts.

like image 480
Paebbels Avatar asked Jun 24 '16 08:06

Paebbels


People also ask

How do you pass parameters in PowerShell?

A default value will not work with a mandatory parameter. You can omit the =$true for advanced parameters of type boolean [Parameter(Mandatory)] . @Andrew First of all you have to change the type of the parameter to [string] . If you then want to pass a string as parameter you can use either ' or " .

How do I create a switch parameter in PowerShell?

To create a switch parameter in a function, specify the switch type in the parameter definition. Switch parameters are easy to use and are preferred over Boolean parameters, which have a less natural syntax for PowerShell. For example, to use a switch parameter, the user types the parameter in the command.

How do I pass two parameters in PowerShell?

To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.

What is param () in PowerShell?

The PowerShell parameter is a fundamental component of any script. A parameter is a way that developers enable script users to provide input at runtime. If a PowerShell script's behavior needs to change in some way, a parameter provides an opportunity to do so without changing the underlying code.


1 Answers

You can specify $true or $false on a switch using the colon-syntax:

compile-tool1.ps1 -VHDL2008:$true compile-tool1.ps1 -VHDL2008:$false 

So just pass the actual value:

compile-tool1.ps1 -VHDL2008:$VHDL2008 
like image 80
Martin Brandl Avatar answered Sep 22 '22 23:09

Martin Brandl