參考this鏈接,我試圖在我的腳本中引入詳細模式。Powershell:定義函數中的詳細開關
當我有這樣定義的函數 -
function TestVerbose
{
param(
[switch]$verbose,
[Parameter(Mandatory = $True)]
$p1
)
if($verbose)
{
Write-Verbose "Verbose Mode"
}
}
Get-Help TestVerbose
我碰到下面的錯誤 -
Get-Help : A parameter with the name 'Verbose' was defined multiple times for the command. At line:12 char:9 + Get-Help <<<< TestVerbose + CategoryInfo : MetadataError: (:) [Get-Help], MetadataException + FullyQualifiedErrorId : ParameterNameAlreadyExistsForCommand,Microsoft.PowerShell.Commands.GetHelpCommand
,但如果我這樣定義函數[移除參數強制屬性],它工作正常
function TestVerbose
{
param(
[switch]$verbose,
$p1
)
if($verbose)
{
Write-Verbose "Verbose Mode"
}
}
Get-Help TestVerbose
任何我爲什麼會這樣的行爲?我想保留強制開關,並希望用戶執行這樣我的功能 -
TestVerbose -verbose
謝謝!不知道這一點。是的,我正在使用V2。 –
我仍然希望能夠測試以查看-Verbose開關是否已激活。我在[這個問題]中找到了我的答案(http://stackoverflow.com/questions/24446680/is-it-possible-to-check-if-verbose-argument-was-given-in-powershell)。 –