powershell - 写入信息未显示在由 Start-Transcript 转录的文件中

标签 powershell logging powershell-cmdlet powershell-v5.1

我正在使用 PowerShell 5.1,并试图确定为什么 Write-Information Start-Transcript 创建的脚本日志中不显示消息除非我设置 $InformationPreferenceSilentlyContinue .我想在控制台中显示消息并将它们写入日志文件。

我看这里:
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_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://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 "$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 $_
}

最佳答案

您看到的是 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

在存在错误 (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

成绩单现在与直接输出一致。

关于powershell - 写入信息未显示在由 Start-Transcript 转录的文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55191548/

相关文章:

linux - Linux下的分布式文件系统

java - log4j2 无法注册关闭 Hook ,因为 JVM 正在关闭

powershell - 如何使用 PowerShell 获取有关暂存 Windows Azure 网站槽的信息?

c# - 使用powershell加载C# DLL,[System.Type]::GetType返回null

azure - 超大规模数据库上 New-AzSqlDatabaseCopy 的替代方案

powershell - 您不能在空值表达式上调用方法

function - cmdlet 和函数有什么区别?

powershell - 使用门户在 Azure 订阅中添加用户组

python - 如何只打印给定记录器的日志消息?

azure - 使用New-AzureDeployment部署云服务,出现错误 "The HTTP version specified is not supported for this operation by the server"