我正在使用PowerShell 5.1,并试图确定为什么Write-Information消息不显示在Start-Transcript创建的记录日志中,除非我将$InformationPreference设置为SilentlyContinue。我希望在控制台中显示这些消息,并将它们写入日志文件。
我在这里看了看:variables?view=powershell-5.1#informationpreference
然后,我决定创建这个脚本来测试所编写的内容以及编写的时间。请参阅Testing explicit behavior with transcripts -------------下面的首选项部分
Clear-Host
$ErrorActionPreference = "Stop"
try {
Write-Host "Starting transcript"
Start-Transcript -Force -Path "$PSScriptRoot\default.txt"
<#
In PowerShell 5.1 the default behavior is as follows:
$DebugPreference = SilentlyContinue
$InformationPreference = SilentlyContinue
$ProgressPreference = Continue
$VerbosePreference = SilentlyContinue
See the following for more information:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-5.1
#>
# I am not testing Write-Output as I am not worried about programmatic/pipeline stuff, just contextual messages for end-users or logging
Write-Host "`nTesting default behavior with transcripts --------------------------------`n"
# Setting these just in case I launch this script in a session where a previous script might have modified the preference variables
$DebugPreference = "SilentlyContinue"
$InformationPreference = "SilentlyContinue"
$ProgressPreference = "Continue"
$VerbosePreference = "SilentlyContinue"
Write-Host "Calling Write-Host"
Write-Debug "Calling Write-Debug"
Write-Error "Calling Write-Error" -ErrorAction "Continue"
Write-Information "Calling Write-Information"
Write-Progress "Calling Write-Progress"
Write-Verbose "Calling Write-Verbose"
Stop-Transcript
Start-Transcript -Force -Path "$PSScriptRoot\everything_continue.txt"
Write-Host "`nTesting explicit behavior with transcripts --------------------------------`n"
# Turn everything on
$DebugPreference = "Continue"
$InformationPreference = "Continue" # Setting this to SilentlyContinue makes it show up in the log but not the console. Setting this to 'Continue' makes it show up in the console but not the log.
$ProgressPreference = "Continue"
$VerbosePreference = "Continue"
Write-Host "Calling Write-Host"
Write-Debug "Calling Write-Debug"
Write-Error "Calling Write-Error" -ErrorAction "Continue"
Write-Information "Calling Write-Information"
Write-Progress "Calling Write-Progress"
Write-Verbose "Calling Write-Verbose"
Stop-Transcript
Write-Host "`nResults -------------------------------------------------------------------`n"
# See what actually gets captured and written by the transcriber
$messageTypes = @("Write-Debug", "Write-Error", "Write-Host", "Write-Information", "Write-Verbose")
Write-Host "Default" -ForegroundColor Cyan
$lines = Get-Content "$PSScriptRoot\default.txt"
foreach ($message in $messageTypes) {
if ($lines -like "*Calling $message*") {
Write-Host " $message PRESENT" -ForegroundColor Green
}
else {
Write-Host " $message MISSING" -ForegroundColor Red
}
}
Write-Host "Everything Continue" -ForegroundColor Cyan
$lines = Get-Content "$PSScriptRoot\everything_continue.txt"
foreach ($message in $messageTypes) {
if ($lines -like "*Calling $message*") {
Write-Host " $message PRESENT" -ForegroundColor Green
}
else {
Write-Host " $message MISSING" -ForegroundColor Red
}
}
}
catch {
Write-Host "----------------------------------------------------------------------------------------------------"
Write-Host $_.Exception
Write-Host $_.ScriptStackTrace
Write-Host "----------------------------------------------------------------------------------------------------"
try { Stop-Transcript } catch { }
throw $_
}发布于 2019-03-16 16:28:55
您所看到的是bug in PowerShell PowerShell ( v5.1.17134.590),它已在PowerShell Core中修复(至少v6.1.0 --尽管其他与抄本相关的问题仍然存在;请参见这个GitHub问题)。
我鼓励您在Windows PowerShell UserVoice论坛上报告它(请注意,PowerShell GitHub-repo 问题论坛只针对PowerShell Core中也存在的错误)。
下面是如何验证该bug是否存在于PowerShell版本中的PowerShell:
使用下面的代码创建一个脚本并运行它:
'--- Direct output'
$null = Start-Transcript ($tempFile = [io.path]::GetTempFileName())
# Note that 'SilentlyContinue' is also the default value.
$InformationPreference = 'SilentlyContinue'
# Produces no output.
Write-Information '1-information'
# Prints '2-Information' to the console.
Write-Information '2-information' -InformationAction Continue
$null = Stop-Transcript
'--- Write-Information output transcribed:'
Select-String '-information' $tempFile | Select-Object -ExpandProperty Line
Remove-Item $tempFile有了bug (Windows PowerShell),您将看到:
--- Direct output
2-information
--- Write-Information output transcribed:
INFO: 1-information也就是说,与预期行为相反的发生了:该记录记录了它不应该拥有的调用(因为它没有产生输出),并且没有记录它应该拥有的调用。
此外,日志记录的输出以INFO:作为前缀,这也是PowerShell核心中的一个不一致之处。
除了在希望记录在记录记录中的输出的情况下使用Write-Host调用以外,没有完整的解决办法,但是不管首选项变量$InformationPreference的值如何,都会被记录为( Write-Host正式提供了-InformationAction公共参数,但被忽略了)。
修复了bug (PowerShell核心)之后,您将看到:
--- Direct output
2-information
--- Write-Information output transcribed:
2-information成绩单现在与直接输出一致。
https://stackoverflow.com/questions/55191548
复制相似问题