将 Write-Host 语句重定向到文件 [英] Redirect Write-Host statements to a file
问题描述
我有一个正在调试的 PowerShell 脚本,想重定向所有 Write-Host 语句到文件.有没有简单的方法可以做到这一点?
I have a PowerShell script that I am debugging and would like to redirect all Write-Host statements to a file. Is there an easy way to do that?
推荐答案
直到 PowerShell 4.0,
Write-Host
将对象发送到主机.它不返回任何对象.
Until PowerShell 4.0,
Write-Host
sends the objects to the host. It does not return any objects.
从 PowerShell 5.0 和更新版本开始,Write-Host
是 Write-Information
的包装器,它允许输出到信息流并使用 重定向它6>文件名
.
Beginning with PowerShell 5.0 and newer, Write-Host
is a wrapper for Write-Information
, which allows to output to the information stream and redirect it with 6>> file_name
.
http://technet.microsoft.com/en-us/library/hh849877.aspx
但是,如果您有很多 Write-Host
语句,请将它们全部替换为 Write-Log
,这样您就可以决定是输出到控制台、文件还是事件日志,或所有三个.
However, if you have a lot of Write-Host
statements, replace them all with Write-Log
, which lets you decide whether output to console, file or event log, or all three.
还要检查:
添加内容
立>- 重定向运算符,例如
>、>>, 2>, 2>, 2>&1
写日志
- 三通对象
Start-Transcript
.
这篇关于将 Write-Host 语句重定向到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!