PowerShell 技能连载 - 隐藏参数

In the previous tip we explained how you can dump all the legal values for a PowerShell attribute. Today we’ll take a look at the [Parameter()] attribute and its value DontShow. Take a look at this function:
在前一个技能中我们介绍了如何导出一个 PowerShell 属性的所有合法值。今天我们将关注 [Parameter()] 属性和它的值 DontShow。我们来看看这个函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function Test-Something
{
param
(
[string]
[Parameter(Mandatory)]
$Name,

[Parameter(DontShow)]
[Switch]
$Internal
)

"You entered: $name"
if ($Internal)
{
Write-Warning "We are in secret test mode!"
}
}

当您运行这个函数时,IntelliSense 只暴露 -Name 参数。-Internal switch 参数并没有显示,然而您仍然可以使用它。它只是一个隐藏的参数:

1
2
3
4
5
6
PS> Test-Something -Name tobias
You entered: tobias

PS> Test-Something -Name tobias -Internal
You entered: tobias
WARNING: We are in secret test mode!

PowerShell 技能连载 - 隐藏参数

http://blog.vichamp.com/2018/11/02/hiding-parameters/

作者

吴波

发布于

2018-11-02

更新于

2022-07-06

许可协议

评论