【问题标题】:Powershell 4.0 Transcript is not capturing output of Write-Host statementsPowershell 4.0 Transcript 未捕获 Write-Host 语句的输出
【发布时间】:2015-05-19 01:42:34
【问题描述】:

我创建了以下脚本(test.ps1),并从命令行以“powershell .\test.ps1”的形式执行它

Write-Host(Start-Transcript -Path "D:\logs.txt")
$remoteScript = {
    Write-Host "Remote Log"
}
Invoke-Command -ConnectionUri $uri -Credential $creds -ScriptBlock $remoteScript
Write-Host "Local Log"
Write-Host(Stop-Transcript)

但是在执行脚本后生成的日志文件中,我没有看到远程或本地的日志语句。这曾经与 Powershell 3.0 一起使用,但最近我升级到 Powershell 4.0 并且它停止了工作。

有没有人遇到过类似的问题,或者知道任何其他方式来捕获远程和本地命令的输出?

谢谢,

高拉夫

【问题讨论】:

  • 这是指某些版本中的一个已知错误social.technet.microsoft.com/Forums/windowsserver/en-US/…
  • 谢谢@Matt。 msdn链接中提到的解决方案是使用Write-Ouput而不是Write-Host,它仅适用于“本地日志”......我无法在成绩单中获得“远程日志”,而我曾经使用以前的版本Powershell
  • 如果您继续阅读,Technet 链接还提到了某些 4.0 版本中的错误。想知道这是否也会引导您朝着正确的方向前进。
  • 该链接确实谈到这是一个错误,并且正在由 Microsoft 团队解决...但是我找不到在脚本中获取远程日志的解决方案...我希望找到一个类似于被转录的本地日志的解决方案/解决方法...我正在广泛使用 Powershell 远程处理与 Azure VM 进行交互,并且无法捕获这些日志受到严重限制...再次感谢@Matt 的回复。

标签: powershell powershell-3.0 powershell-remoting powershell-ise powershell-4.0


【解决方案1】:

这是 Microsoft 提供的用于解决此问题的修补程序:

https://support.microsoft.com/en-us/kb/3014136

这里也在 Technet 中讨论过

https://social.technet.microsoft.com/Forums/windowsserver/en-US/cecc4f32-28c8-4bdc-be63-49ce3d396625/powershell-4-starttranscript-does-not-log-writehost?forum=winserverpowershell

来自修补程序网站:

在运行 Windows Server 2012 R2 的服务器上,您在使用 PowerShell 时会遇到以下一个或多个问题:

  • 问题 1

Start-Transcript cmdlet 不捕获写入主机调用,如以下脚本示例所示:

Start-Transcript -path $env:TEMP\transcript.txt
Write-Host Hello World
Stop-Transcript

Get-Content $env:TEMP\transcript.txt
在这种情况下,“Hello World”没有按预期出现在 transcript.txt 文件中。

【讨论】:

    【解决方案2】:

    在 2012 R2 中,将 Write-Host 替换为 Write-Output 对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多