【发布时间】:2019-01-17 19:40:56
【问题描述】:
我正在尝试在具有 2 个参数的 powershell 中创建一个 cmdlet 函数。我希望这两个参数之一是ConsoleColor,但 ISE 抱怨并说函数参数列表中有一个 Missing ')'。 但我找不到这个缺失的 )。
这是我的功能:
function Log {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline=$true, ValueFromRemainingArguments=$true)]
[AllowNull()]
[AllowEmptyString()]
[AllowEmptyCollection()]
[string[]]$messages,
# If I remove the following parameter, everything works fine
[System.ConsoleColor]$color = Default # ISE Complains here before `=`
)
if (($messages -eq $null) -or ($messages.Length -eq 0)) {
$messages = @("")
}
foreach ($msg in $messages) {
Write-Host $msg -ForegroundColor $color
$msg | Out-File $logFile -Append
}
}
我在 powershell 方面不是很好,所以它可能是一些我还不知道的愚蠢的东西。
【问题讨论】:
-
在此上下文中分配给 $color 的默认值是什么?它似乎不是有效的 System.ConsoleColor 标识符,所以我对它的来源很感兴趣。
-
default是 PowerShell 语言关键字,仅在switch语句中有效。 PowerShell 不知道 C#default关键字,也没有类似的概念。 -
????我知道这很愚蠢......谢谢
标签: .net function powershell powershell-cmdlet