简体   繁体   English

如何在执行期间将 PowerShell 的输出重定向到文件

[英]How to redirect the output of a PowerShell to a file during its execution

I have a PowerShell script for which I would like to redirect the output to a file.我有一个 PowerShell 脚本,我想将其输出重定向到一个文件。 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?如何在执行期间重定向 PowerShell 脚本的输出?

Maybe Start-Transcript would work for you.也许Start-Transcript对你Start-Transcript 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"

Microsoft has announced on Powershell's Connections web site (2012-02-15 at 4:40 PM) that in version 3.0 they have extended the redirection as a solution to this problem. Microsoft 已在 Powershell 的 Connections 网站(2012-02-15 下午 4:40)上宣布,在 3.0 版中,他们已扩展重定向作为此问题的解决方案。

In PowerShell 3.0, we've extended output redirection to include the following streams: 
 Pipeline (1) 
 Error    (2) 
 Warning  (3) 
 Verbose  (4) 
 Debug    (5)
 All      (*)

We still use the same operators
 >    Redirect to a file and replace contents
 >>   Redirect to a file and append to existing content
 >&1  Merge with pipeline output

See the "about_Redirection" help article for details and examples.有关详细信息和示例,请参阅“about_Redirection”帮助文章。

help about_Redirection

采用:

Write "Stuff to write" | Out-File Outputfile.txt -Append

I take it you can modify MyScript.ps1 .我认为您可以修改MyScript.ps1 Then try to change it like so:然后尝试像这样改变它:

$(
    Here is your current script
) *>&1 > output.txt

I just tried this with PowerShell 3. You can use all the redirect options as in Nathan Hartley's answer .我刚刚在 PowerShell 3 中尝试了这个。您可以使用Nathan Hartley 的回答中的所有重定向选项。

One possible solution, if your situation allows it:如果您的情况允许,一种可能的解决方案是:

  1. Rename MyScript.ps1 to TheRealMyScript.ps1将 MyScript.ps1 重命名为 TheRealMyScript.ps1
  2. Create a new MyScript.ps1 that looks like:创建一个新的 MyScript.ps1,如下所示:

    .\\TheRealMyScript.ps1 > output.txt .\\TheRealMyScript.ps1 > output.txt

powershell ".\MyScript.ps1" > test.log

If you want a straight redirect of all output to a file, try using *>> :如果要将所有输出直接重定向到文件,请尝试使用*>>

# You'll receive standard output for the first command, and an error from the second command.
mkdir c:\temp -force *>> c:\my.log ;
mkdir c:\temp *>> c:\my.log ;

Since this is a straight redirect to file, it won't output to the console (often helpful).由于这是直接重定向到文件,因此不会输出到控制台(通常很有帮助)。 If you desire the console output, combined all output with *&>1 , and then pipe with Tee-Object :如果您需要控制台输出,请将所有输出与*&>1 ,然后使用Tee-Object管道:

mkdir c:\temp -force *>&1 | Tee-Object -Append -FilePath c:\my.log ;
mkdir c:\temp *>&1 | Tee-Object -Append -FilePath c:\my.log ;

# Shorter aliased version
mkdir c:\temp *>&1 | tee -Append c:\my.log ;

I believe these techniques are supported in PowerShell 3.0 or later;我相信 PowerShell 3.0 或更高版本支持这些技术; I'm testing on PowerShell 5.0.我正在 PowerShell 5.0 上进行测试。

You might want to take a look at the cmdlet Tee-Object .您可能想查看 cmdlet Tee-Object You can pipe output to Tee and it will write to the pipeline and also to a file您可以将输出通过管道传输到 Tee,它会写入管道和文件

如果您想从命令行执行此操作而不是内置到脚本本身中,请使用:

.\myscript.ps1 | Out-File c:\output.csv

To embed this in your script, you can do it like this:要将其嵌入到您的脚本中,您可以这样做:

        Write-Output $server.name | Out-File '(Your Path)\Servers.txt' -Append

That should do the trick.这应该够了吧。

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

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