如何在执行期间将 PowerShell 的输出重定向到文件 [英] How to redirect the output of a PowerShell to a file during its execution

查看:59
本文介绍了如何在执行期间将 PowerShell 的输出重定向到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PowerShell 脚本,我想将其输出重定向到一个文件.问题是我无法更改调用此脚本的方式.所以我不能这样做:

 .\MyScript.ps1 >输出.txt

如何在执行期间重定向 PowerShell 脚本的输出?

解决方案

也许 Start-Transcript 对你有用.如果它已经在运行,首先停止它,然后启动它,完成后停止它.

<前>$ErrorActionPreference="SilentlyContinue"停止成绩单 |出空$ErrorActionPreference = "继续"Start-Transcript -path C:\output.txt -append# 做一些事情停止成绩单

您也可以在处理东西时运行它,并保存您的命令行会话以供日后参考.

如果您想在尝试停止未转录的转录本时完全抑制错误,您可以这样做:

$ErrorActionPreference="SilentlyContinue"停止成绩单 |出空$ErrorActionPreference = "Continue" # 或 "Stop"

I have a PowerShell script for which I would like to redirect the output to a file. The problem is that I cannot change the way this script is called. So I cannot do:

 .\MyScript.ps1 > output.txt

How do I redirect the output of a PowerShell script during its execution?

解决方案

Maybe Start-Transcript would work for you. First stop it if it's already running, then start it, and stop it when done.

$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\output.txt -append
# Do some stuff
Stop-Transcript

You can also have this running while working on stuff and have it saving your command line sessions for later reference.

If you want to completely suppress the error when attempting to stop a transcript that is not transcribing, you could do this:

$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue" # or "Stop"

这篇关于如何在执行期间将 PowerShell 的输出重定向到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆