写入信息不会显示在Start-Transcript转录的文件中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了写入信息不会显示在Start-Transcript转录的文件中相关的知识,希望对你有一定的参考价值。
我正在使用PowerShell 5.1,我试图确定为什么Write-Information
消息没有显示在Start-Transcript
创建的脚本日志中,除非我将$InformationPreference
设置为SilentlyContinue
。我想在控制台中显示消息并将它们写入日志文件。
然后我决定创建这个脚本来测试写入的内容和时间。请参阅Testing explicit behavior with transcripts -------------
下面的首选项部分
Clear-Host
$ErrorActionPreference = "Stop"
try {
Write-Host "Starting transcript"
Start-Transcript -Force -Path "$PSScriptRootdefault.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://docs.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 "$PSScriptRooteverything_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 "$PSScriptRootdefault.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 "$PSScriptRooteverything_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 $_
}
您所看到的是Windows PowerShell中的一个错误(自v5.1.17134.590起)已在PowerShell Core中修复(至少从v6.1.0开始 - 尽管其他与转录相关的问题仍然存在;请参阅this GitHub issue)。
我建议您在Windows PowerShell UserVoice论坛中报告它(请注意,PowerShell GitHub-repo issues forum仅用于PowerShell Core中也存在的错误)。
以下是验证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 Core中修复。
没有完整的解决方法,除了您可以使用Write-Host
调用,如果您希望输出记录在记录中 - 但这些调用将无条件记录,而不管首选变量$InformationPreference
的值(而Write-Host
正式提供-InformationAction
常见参数,它被忽略)。
修复了错误(PowerShell Core)后,您会看到:
--- Direct output
2-information
--- Write-Information output transcribed:
2-information
成绩单现在与直接输出一致。
以上是关于写入信息不会显示在Start-Transcript转录的文件中的主要内容,如果未能解决你的问题,请参考以下文章