Powershell:命令行参数中的冒号 [英] Powershell: Colon in commandlet parameters

查看:104
本文介绍了Powershell:命令行参数中的冒号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与需要冒号的 Powershell commandlet 开关参数有什么关系?

What's the deal with Powershell commandlet switch parameters that require a colon?

考虑 Exchange 2010 管理外壳 cmdlet Move-ActiveMailboxDatabase.Confirm 开关是一个 System.Management.Automation.SwitchParameter 并且必须像这样使用,

Consider Exchange 2010 management shell cmdlet Move-ActiveMailboxDatabase. The Confirm switch is a System.Management.Automation.SwitchParameter and must be used like so,

Move-ActiveMailboxDatabase -Confirm:$false

如果没有冒号,命令将无法像这样识别不确认开关,

Without the colon the command fails to recognize the don't confirm switch like so,

Move-ActiveMailboxDatabase -Confirm $false

这是为什么?冒号在那里有什么区别?为什么 Exchange2010 似乎是我唯一注意到的这种行为?

Why is that? What's the difference the colon makes there? Why Exchange2010 seems to be about the only thing I've noticed this behavior?

我浏览了 Powershell in Action 和 Powershell 2.0,但没有找到有关此语法的任何信息.不过,这些书中记录了范围解析和 .Net 对象访问用途.

I've browsed through Powershell in Action and Powershell 2.0, but didn't find anything about this syntax. Scope resolution and .Net object access uses are documented on those books though.

我的 Google-fu 发现了一个 文章声称它明确转发开关参数值,但没有解释这是关于什么的.

My Google-fu found an article which claims that it explicitly forwards switch parameter values, but fails to explain what that is about.

推荐答案

当你这样做时:

Move-ActiveMailboxDatabase -Confirm $false

您不是说 Confirm 参数接受 $false.您说的是 -Confirm 并且还将一个(单独的)参数传递给值为 $false 的 cmdlet.

you are not saying Confirm parameter accepts the $false. You are saying -Confirm and also passing an (separate) argument to the cmdlet with value $false.

由于Confirm 是一个开关,所以只要有-Confirm 就表示它是真的.没有 -Confirm 意味着它是假的.

Since Confirm is a switch, just the presence of -Confirm means it is true. Absence of -Confirm means it is false.

让我给你一个脚本示例:

Let me give you a script example:

param([switch]$test)

write-host Test is $test

如果你只运行没有任何参数/参数的脚本,即 .\script.ps1 你会得到输出:

If you just run the script without any arguments / paramters i.e .\script.ps1 you get output:

Test is False

如果你以 .\script.ps1 -test 运行它,输出是

If you run it as .\script.ps1 -test, the output is

Test is True

如果你以 .\script.ps1 -test $false 运行它,输出是

If you run it as .\script.ps1 -test $false, the output is

Test is True

如果你以 .\script.ps1 -test:$false 运行它,输出是

If you run it as .\script.ps1 -test:$false the output is

Test is False

在开关变量本身的值必须从使用 : 的另一个变量确定的情况下.

It is in scenarios where the value for a switch variable itself has to be determined from another variable that the : is used.

以脚本为例:

param ([boolean]$in)

function func([switch] $test){
write-host Test is $test
}

func -test:$in

这里如果你把它作为 .\script.ps1 -in $false 运行,你会得到

Here if you run it as .\script.ps1 -in $false, you get

Test is false

如果你不能使用 :,你必须把它写成:

If you weren't able to use the :, you would have had to write it as:

if($in){  func -test}
else { func }

这篇关于Powershell:命令行参数中的冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆