Powershell 4.0 Transcript 未捕获 Write-Host 语句的输出

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

我创建了以下脚本(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,但它停止工作。

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

谢谢,

高拉夫

最佳答案

这是 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
从修补程序站点:

On a server that's running Windows Server 2012 R2, you encounter one or more of the following issues when you use PowerShell:

  • Issue 1

The Start-Transcript cmdlet does not capture write-host calls, as seen in the following script example:

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

Get-Content $env:TEMP\transcript.txt
In this case, "Hello World" does not appear in the transcript.txt file as expected.

关于Powershell 4.0 Transcript 未捕获 Write-Host 语句的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29085362/

相关文章:

powershell - 从 VSTS 版本访问 Azure 服务主体详细信息

字符串变量未在多行字符串中替换

powershell - 按值对嵌套哈希表排序

powershell - Powershell帮助系统使用情况

powershell - 如何在 Powershell 中设置低 I/O ("background") 优先级

powershell - Get-Process 到远程计算机不起作用,但 Invoke-Command 可以

powershell - 通过FTP和Powershell复制文件

powershell - Powershell:在AD中检查重复的电子邮件

powershell - 运行空间坏了。 (远程处理、WinRM 和 Sharepoint)

Azure 数据工厂 - 调用 Azure VM 中的 EXE 文件