如何通过 PowerShell 中的 splatting 将开关参数作为变量传递?

Mas*_*son 7 variables powershell switch-parameter parameter-splatting

如果您有多个参数在调用命令或脚本时需要一个值,我知道您可以像这样传递它:

$parameters = @{
    name = "John"
    last_name = "Doe"
}
Run Code Online (Sandbox Code Playgroud)

但是,如果命令或脚本实际上只是希望-T指示诸如标志之类的东西,但参数本身不需要值。如何在变量中设置它?

$optionalT = ""
if ($itNeedsTheT) $optionalT = "-T"

command $optionalT
Run Code Online (Sandbox Code Playgroud)

如果我这样做,它会抱怨以下消息:

Unknown argument 'T' on command line.
Run Code Online (Sandbox Code Playgroud)

mkl*_*nt0 7

tl;博士

# Pass the $itNeedsT Boolean - which indicates whether the -T switch should
# be passed - as the switch's *value*.
command -T:$itNeedsTheT  
Run Code Online (Sandbox Code Playgroud)

如果$itNeedsTheT$false,则上述内容与省略 相同-T-通常(请继续阅读以了解详细信息)。

请注意需要使用:将开关名称与值分开。


正如boxdog在评论中指出的那样,在与splatting ( @parameters)一起使用的哈希表中,您使用布尔值来表示开关参数(类型为标志的类似参数[switch])。

# Dynamically determine if -Recurse should be turned on.
$recurseIfTrue = $true

# Define the hashtable for splatting...
$parameters = @{
  Path = '.'
  Recurse = $recurseIfTrue  # turn the -Recurse switch on or off
}

# ... and pass it to the target command.
# *Loosely speaking*, the following command is the same as either:
#   Get-ChildItem -Path '.' -Recurse  # if $recuseIfTrue was $true
# or:
#   Get-ChildItem -Path '.'           # if $recuseIfTrue was $false
Get-ChildItem @parameters
Run Code Online (Sandbox Code Playgroud)

也就是说,粗略地说:

  • 使用$true通过所述开关
  • 使用$false通过开关。

这允许您保留一个无条件包含 switch 参数的哈希表定义,但其值可以通过编程方式确定。

警告

严格来说,哈希表条目Recurse = $true转换为参数-Recurse:$trueRecurse = $false并不会转换为省略参数,而是转换为传递-Recurse:$false.

大多数情况下,省略一个开关-Foo并将其传递给值$false- 即-Foo:$false- 是等效的

但是,命令可以检测到差异,有时会采取不同的行动

一个值得注意的例子是-Confirmcommon (switch) 参数:省略 -Confirm意味着$ConfirmPreference偏好变量被尊重,而-Confirm:$false意味着偏好变量应该被覆盖(并且应该请求确认)。

如果您想自己在 PowerShell 脚本或函数中进行这种区分$PSBoundParameters.ContainsKey('Foo'),除了检查$Foo( -Foo) 开关参数变量的值之外,您还可以调用。

如果您正在处理这样的命令并且想要以编程方式强制省略开关参数,则别无选择,只能在单独的步骤中有条件地为此开关添加条目:

# Dynamically determine if -Recurse should be turned on.
$recurseIfTrue = $true

# A 'Recurse' key now can NOT be included unconditionally,
# if you want to *omit* -Recurse in case $recurseIfTrue is $false
$parameters = @{
  Path = '.'
}

# Add a 'Recurse' entry only if the switch should be passed.
if ($recurseIfTrue) {
  $parameters.Recurse = $true
}

Get-ChildItem @parameters
Run Code Online (Sandbox Code Playgroud)

最后,请注意,作为通过 splatting 以编程方式指定开关值的替代方法,您可以直接将动态值传递给开关

# Dynamically determine if -Recurse should be turned on.
$recurseIfTrue = $true

Get-ChildItem -Path . -Recurse:$recurseIfTrue
Run Code Online (Sandbox Code Playgroud)

请注意需要使用:将开关名称与其值分开

这是必要的,因为使用习惯的空格将参数名称与其值分开会导致 PowerShell 将 Boolean 解释为下一个参数,因为 switch 参数通常不采用values

尽管很少使用,但这种:基于语法的语法适用于所有参数类型。


Ben*_*est 5

splatting时,使用非条件参数创建哈希表(值可以是可变的),但在创建哈希表后添加可选参数:

$parameters = @{
  Name = "John"
  LastName = "Doe"
  Age = $age
  Enabled = $true
}

if( $favoriteThing ){
  $parameters.FavoriteThing = $favoriteThing
}

command @parameters
Run Code Online (Sandbox Code Playgroud)

如果在 splatting 中处理开关,您可以将其视为布尔参数,如上所示,只需给它一个值$true或 ,$false具体取决于您是否希望在命令上启用开关。您可以看到一个非 splat 示例,将-Confirm标志设置为$false

Install-Package some_package -Confirm:$false
Run Code Online (Sandbox Code Playgroud)