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

Mar*_*tin 185 powershell scripting stdout

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

 .\MyScript.ps1 > output.txt
Run Code Online (Sandbox Code Playgroud)

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

Bra*_*tch 178

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

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

您还可以在处理内容时运行此操作并保存命令行会话以供日后参考.

如果要在尝试停止未转录的脚本时完全抑制错误,可以执行以下操作:

$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue" # or "Stop"
Run Code Online (Sandbox Code Playgroud)

  • 请注意,start-transcript不记录Write-Error,Write-Verbose或Write-Debug ...仅标准输出. (12认同)
  • @richard:现在看来这样做了.也许这是2.0的补充,不确定这些答案是否都适用于1.0. (4认同)
  • 最后,我找到了[Start-Transcript]的文档(http://technet.microsoft.com/en-us/library/hh849687.aspx).(令人沮丧的是没有标注Powershell版本号).它_says_"成绩单包括用户键入的所有命令和控制台上显示的所有输出".但是,我在Powershell 2.0中测试了Start-Transcript,发现@Richard是对的,标准错误_isn't_保存到成绩单中.这糟透了! (4认同)

Nat*_*ley 47

微软已在Powershell的Connections网站(2012-02-15 at 4:40 PM)上宣布,在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
Run Code Online (Sandbox Code Playgroud)

有关详细信息和示例,请参阅"about_Redirection"帮助文章.

help about_Redirection
Run Code Online (Sandbox Code Playgroud)

  • 不,可以离开它。给定上投票的数量,这显然是有用的。几乎可以肯定,这个问题是从能够改变脚本调用方式的人们那里获得Google的好评。 (2认同)

小智 33

使用:

Write "Stuff to write" | Out-File Outputfile.txt -Append
Run Code Online (Sandbox Code Playgroud)

  • 不能解决 OP 的问题,因为它在“执行期间”不起作用。但是我给了它 +1,因为无论如何知道如何在 Powershell 中进行管道传输很有用。 (3认同)

zda*_*dan 24

一种可能的解决方案,如果您的情况允许:

  1. 将MyScript.ps1重命名为TheRealMyScript.ps1
  2. 创建一个新的MyScript.ps1,如下所示:

    .\ TheRealMyScript.ps1> output.txt


mpl*_*ork 24

我认为你可以修改MyScript.ps1.然后尝试改变它:

$(
    Here is your current script
) *>&1 > output.txt
Run Code Online (Sandbox Code Playgroud)

我刚刚尝试使用PowerShell 3.您可以使用所有重定向选项,如Nathan Hartley的回答.

  • 你可以尝试这样的事情:`*>&1 | Out-File $log -Encoding ascii -Append -Width 132` 但如果你需要精确控制输出,Powershell 真的很难看。 (2认同)

And*_*der 17

您可能需要查看cmdlet Tee-Object.您可以将输出传输到Tee,它将写入管道以及文件

  • “输出” | 设置内容 -PassThru 和“输出”| Add-Content -PassThru 也可以像 Tee-Object 一样工作,额外的好处是您可以设置编码。 (2认同)

sui*_*eng 15

powershell ".\MyScript.ps1" > test.log
Run Code Online (Sandbox Code Playgroud)

  • 这是适用于我的脚本输出的许多选项中的*唯一*。 (2认同)

son*_*njz 11

如果要将所有输出直接重定向到文件,请尝试使用*>>:

# 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 ;
Run Code Online (Sandbox Code Playgroud)

由于这是直接重定向到文件,因此它不会输出到控制台(通常很有帮助).如果您需要控制台输出,请将所有输出组合在一起*&>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 ;
Run Code Online (Sandbox Code Playgroud)

我相信PowerShell 3.0或更高版本支持这些技术; 我在PowerShell 5.0上测试.


小智 8

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

.\myscript.ps1 | Out-File c:\output.csv
Run Code Online (Sandbox Code Playgroud)

  • 提问者明确解释脚本调用无法更改。 (7认同)
  • 与问题无关,但对我有帮助,我在谷歌上搜索以获取正确的带有 Pipe to Out-File 的语法。 (5认同)