Powershell 4.0 Transcript 未捕获 Write-Host 语句的输出
Powershell 4.0 Transcript is not capturing output of Write-Host statements
我创建了以下脚本 (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
来自修补程序站点:
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.
用 Write-Output
替换 Write-Host
在 2012 R2 上对我有用。
我创建了以下脚本 (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
来自修补程序站点:
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.
用 Write-Output
替换 Write-Host
在 2012 R2 上对我有用。