I have two PowerShell scripts, which have switch parameters:
我有两个PowerShell脚本,它们有switch参数:
compile-tool1.ps1:
编译tool1.ps1:
[CmdletBinding()]
param(
[switch]$VHDL2008
)
Write-Host "VHDL-2008 is enabled: $VHDL2008"
compile.ps1:
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?
如何在不编写大的if..then..else或case语句的情况下将switch参数传递给另一个PowerShell脚本?
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.
我不想将compile-tool1.ps1的参数$ VHDL2008转换为bool类型,因为这两个脚本都是前端脚本(由用户使用)。后者是多个编译工具* .ps1脚本的高级包装器。
2 个解决方案
#1
26
You can specify $true
or $false
on a switch using the colon-syntax:
您可以使用冒号语法在交换机上指定$ true或$ false:
compile-tool1.ps1 -VHDL2008:$true
compile-tool1.ps1 -VHDL2008:$false
So just pass the actual value:
所以只需传递实际值:
compile-tool1.ps1 -VHDL2008:$VHDL2008
#2
5
Try
尝试
compile-tool1.ps1 -VHDL2008:$VHDL2008.IsPresent
#1
26
You can specify $true
or $false
on a switch using the colon-syntax:
您可以使用冒号语法在交换机上指定$ true或$ false:
compile-tool1.ps1 -VHDL2008:$true
compile-tool1.ps1 -VHDL2008:$false
So just pass the actual value:
所以只需传递实际值:
compile-tool1.ps1 -VHDL2008:$VHDL2008
#2
5
Try
尝试
compile-tool1.ps1 -VHDL2008:$VHDL2008.IsPresent