如何将开关参数传递给另一个 PowerShell 脚本?

问题描述:

我有两个 PowerShell 脚本,它们具有开关参数:

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  }

如何将 switch 参数传递给另一个 PowerShell 脚本,而无需编写大的 if..then..elsecase 语句?

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

我不想将compile-tool1.ps1的参数$VHDL2008转换成bool类型,因为,两个脚本都是前端脚本(由用户使用).后者是多个 compile-tool*.ps1 脚本的高级包装器.

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.

您可以使用 colon-syntax 在开关上指定 $true$false:

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

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

所以只需传递实际值:

compile-tool1.ps1 -VHDL2008:$VHDL2008