简体   繁体   English

在PowerShell中如何将错误,警告,写主机输出捕获到单个文件中?

[英]In PowerShell how to capture error, warning, write-host output into a single file?

In PowerShell how to capture error, warning, write-host output into a single file? PowerShell中如何将错误,警告,写主机输出捕获到单个文件中?

When I execute the external command/ write-warning/ write-error/ write-host I need all the information to be captured in a file. 当我执行外部命令/ write-warning / write-error / write-host时,我需要在文件中捕获所有信息。

when I redirect the write-error to a log file it will not show the same content as it's been displayed on the console. 当我将写入错误重定向到日志文件时,它将不会显示与控制台上显示的内容相同的内容。 It will have some other information which I don't need. 它将有一些我不需要的其他信息。

Is it possible to prefix error with ERROR: , warning with WARN: in the log file? 是否可以使用ERROR:错误前缀错误,使用WARN警告:在日志文件中?

Have a look at PowerShell 2.0: A Configurable and Flexible Script Logger Module by Oisin. 看看PowerShell 2.0: Oisin的可配置和灵活的脚本记录器模块 It should be possible to prefix the message with your custom string ('ERROR', 'WARN',...) 应该可以在消息前添加自定义字符串('ERROR','WARN',...)

执行此操作的简单方法是使用cmd.exe,例如:

cmd /c powershell.exe -file <path_to_script> > script.log

Perhaps Start-Transcript and Stop-Transcript will do what you want. 也许Start-Transcript和Stop-Transcript会做你想要的。 (PowerShell 2.0) (PowerShell 2.0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM