简体   繁体   English

Powershell 2.0写入输出以显示并追加到文件

[英]Powershell 2.0 Write output to display and append to file

I would like to display a message in PowerShell and append the message to a log file. 我想在PowerShell中显示一条消息并将该消息附加到日志文件中。 In PowerShell 4.0 you can use Tee-Object -Append but I don't think they have the -Append option in PowerShell 2.0. 在PowerShell 4.0中,可以使用Tee-Object -Append,但是我不认为它们在PowerShell 2.0中具有-Append选项。 Is there an simple way to write a message to the screen and log file at the same time? 有没有一种简单的方法可以将消息同时写入屏幕和日志文件?

Unfortunately I can't upgrade to PowerShell 4.0. 不幸的是,我无法升级到PowerShell 4.0。

You can use the Start-Transcript and Stop-Transcript cmdlets (remember to change the dropdown to 3.0 so you can see the 2.0 features). 您可以使用Start-TranscriptStop-Transcript cmdlet (请记住将下拉列表更改为3.0,以便可以看到2.0的功能)。 You might need to collapse your output file descriptors using redirection if you wish to capture verbose, warning, error, etc. 如果希望捕获详细,警告,错误等信息,则可能需要使用重定向折叠输出文件描述符。

尽管有些长,但是可以使用:

    | %{write-host $_; out-file -filepath "$LOG" -inputobject $_ -append}

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

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