Function 具有字符串消息功能,可回显屏幕,更改消息颜色

Function 具有字符串消息功能,可回显屏幕,更改消息颜色,function,powershell,parameters,colors,write-host,Function,Powershell,Parameters,Colors,Write Host,大家早上好, 解决了的 这两种反应齐头并进。非常感谢怀疑论者和Wasif Hasan的例子 我有一个日志函数,它有一个消息参数。通过该函数,它以绿色文本写入消息。有没有办法更改日志中某些消息的颜色?下面是函数 Function Get-Logger { param( [Parameter(Mandatory=$True)] [String]$message ) $TimeStamp = Get-Date -Format "MM-dd-yyy

大家早上好, 解决了的 这两种反应齐头并进。非常感谢怀疑论者和Wasif Hasan的例子

我有一个日志函数,它有一个消息参数。通过该函数,它以绿色文本写入消息。有没有办法更改日志中某些消息的颜色?下面是函数

Function Get-Logger { 
    param(
       [Parameter(Mandatory=$True)]
       [String]$message
    )

    $TimeStamp = Get-Date -Format "MM-dd-yyy hh:mm:ss"

    Write-Host $TimeStamp -NoNewline
    Write-Host `t $message -ForegroundColor Green
    $logMessage = "[$TimeStamp]  $message"
    $logMessage | Out-File -Append -LiteralPath $VerboseLogFile
}
例如,当调用log函数时,它会将消息返回为绿色文本,这很好。但是,如果我想使用日志功能将节标题的文本更改为黄色,有没有办法做到这一点?下面是我想说的

Get-Logger "Hello Word Starting" -Foregroundcolor yellow -nonewline
像这样

Function Get-Logger { 
    param(
       [Parameter(Mandatory=$True)][String]$message,
       [validatescript({[enum]::getvalues([system.consolecolor]) -contains $_})][string]$messagecolor,
       [switch]$nonewline
    )

    $TimeStamp = Get-Date -Format "MM-dd-yyy hh:mm:ss"
    If ($nonewline){
        Write-Host `t $message -ForegroundColor $($messagecolor) -nonewline
    }
    Else {
        Write-Host `t $message -ForegroundColor $($messagecolor)
    }
    $logMessage = "[$TimeStamp]  $message"
    $logMessage | Out-File -Append -LiteralPath $VerboseLogFile
}
然后:

像这样

Function Get-Logger { 
    param(
       [Parameter(Mandatory=$True)][String]$message,
       [validatescript({[enum]::getvalues([system.consolecolor]) -contains $_})][string]$messagecolor,
       [switch]$nonewline
    )

    $TimeStamp = Get-Date -Format "MM-dd-yyy hh:mm:ss"
    If ($nonewline){
        Write-Host `t $message -ForegroundColor $($messagecolor) -nonewline
    }
    Else {
        Write-Host `t $message -ForegroundColor $($messagecolor)
    }
    $logMessage = "[$TimeStamp]  $message"
    $logMessage | Out-File -Append -LiteralPath $VerboseLogFile
}
然后:


您需要添加另一个非WLINE开关。因此,在param块中添加以下内容:

[switch]$nonewline
在功能体中,执行以下操作:

If ($nonewline){
  Write-Host `t $message -ForegroundColor $($messagecolour) -nonewline
}
Else {
  Write-Host `t $message -ForegroundColor $($messagecolour)
}
现在可以在param块上添加validatescript以验证颜色:

[validatescript({[enum]::getvalues([system.consolecolor]) -contains $_})][string]$messagecolor

感谢@怀疑论者

,您需要添加另一个非在线开关。因此,在param块中添加以下内容:

[switch]$nonewline
在功能体中,执行以下操作:

If ($nonewline){
  Write-Host `t $message -ForegroundColor $($messagecolour) -nonewline
}
Else {
  Write-Host `t $message -ForegroundColor $($messagecolour)
}
现在可以在param块上添加validatescript以验证颜色:

[validatescript({[enum]::getvalues([system.consolecolor]) -contains $_})][string]$messagecolor

感谢@怀疑论者

的支持,我正试图从中获得更多的灵活性,这样我就不必在函数中确定整个可接受的颜色范围。另外,-nonewline部分仍然显示为一个错误,您可以使用代码从PS中提取默认颜色,然后将其用作验证集-我想我在某个地方有一个示例,但对于这样一个小函数来说,它相对复杂。如果希望-nonewline可选,则必须将其添加为函数的参数并为其编写代码。编辑:正如Wasan在下面显示的那样:这确实有效,我正试图从中获得更多的灵活性,这样我就不必在函数中确定整个可接受的颜色范围。另外,-nonewline部分仍然显示为一个错误,您可以使用代码从PS中提取默认颜色,然后将其用作验证集-我想我在某个地方有一个示例,但对于这样一个小函数来说,它相对复杂。如果希望-nonewline可选,则必须将其添加为函数的参数并为其编写代码。编辑:正如Wasan在下面显示的:给你-这将检索颜色:[enum]::GetValues[System.ConsoleColor]@怀疑论者谢谢你的帮助!编辑了答案并添加了ValidateScript。给你-这将检索颜色:[enum]::GetValues[System.ConsoleColor]@怀疑论谢谢你的帮助!编辑答案并添加ValidateScript。