关于重定向
简短说明
介绍如何将 PowerShell 的输出重定向到文本文件。
长说明
默认情况下,PowerShell 会将输出发送到 PowerShell 主机。 通常,这是控制台应用程序。 但是,可以将输出定向到文本文件,并将错误输出重定向到常规输出流。
可以使用以下方法重定向输出:
Out-File
使用 cmdlet,它将命令输出发送到文本文件。 通常,当需要使用其参数(如Encoding
、Width
Force
、或NoClobber
参数)时,请使用Out-File
该 cmdlet。Tee-Object
使用 cmdlet,它将命令输出发送到文本文件,然后将其发送到管道。使用 PowerShell 重定向运算符。 将重定向运算符与文件目标配合使用在功能上等效于没有额外参数的管道
Out-File
。
有关流的详细信息,请参阅 about_Output_Streams。
可重定向输出流
PowerShell 支持重定向以下输出流。
流# | 说明 | 已引入的版本 | 写入 Cmdlet |
---|---|---|---|
1 | 成功 流 | PowerShell 2.0 | Write-Output |
2 | 错误 流 | PowerShell 2.0 | Write-Error |
3 | 警告 流 | PowerShell 3.0 | Write-Warning |
4 | 详细 流 | PowerShell 3.0 | Write-Verbose |
5 | 调试 流 | PowerShell 3.0 | Write-Debug |
6 | 信息 流 | PowerShell 5.0 | Write-Information |
* | 所有流 | PowerShell 3.0 |
注意
PowerShell 中还有 一个进度 流,但它不支持重定向。
PowerShell 重定向运算符
PowerShell 重定向运算符如下所示,其中 n
表示流号。 成功流 ( 1
) 为默认值(如果未指定流)。
运算符 | 说明 | 语法 |
---|---|---|
> |
将指定的流发送到文件。 | n> |
>> |
将 指定的流追加到文件中。 | n>> |
>&1 |
将 指定的流重定向到 Success 流。 | n>&1 |
注意
与某些 Unix shell 不同,只能将其他流重定向到 Success 流。
示例
示例 1:将错误和输出重定向到文件
此示例在 dir
一个成功项上运行,另一个项将出错。
dir 'C:\', 'fakepath' 2>&1 > .\dir.log
它用于 2>&1
将 错误 流重定向到 Success 流,并将 >
生成的 Success 流发送到名为 的文件 dir.log
示例 2:将所有成功流数据发送到文件
This example sends all Success stream data to a file called script.log
.
.\script.ps1 > script.log
示例 3:将成功、警告和错误流发送到文件
此示例演示如何合并重定向运算符以实现所需的结果。
&{
Write-Warning "hello"
Write-Error "hello"
Write-Output "hi"
} 3>&1 2>&1 > P:\Temp\redirection.log
3>&1
将 警告 流重定向到 成功 流。2>&1
将 错误 流重定向到 成功 流 (,该流现在还包括所有 警告 流数据)>
将 成功 流 (重定向,该流现在包含 警告 流和 错误 流) 到名为C:\temp\redirection.log
) 的文件
示例 4:将所有流重定向到文件
此示例将调用脚本的所有流输出发送到调用 script.ps1
的文件 script.log
.\script.ps1 *> script.log
示例 5:禁止显示所有Write-Host和信息流数据
此示例禁止显示所有信息流数据。 若要详细了解 信息 流 cmdlet,请参阅 Write-Host 和 Write-Information
&{
Write-Host "Hello"
Write-Information "Hello" -InformationAction Continue
} 6> $null
示例 6:显示操作首选项的效果
操作首选项变量和参数可以更改写入到特定流的内容。 此示例中的脚本演示如何影响写入错误流的内容的值$ErrorActionPreference
。
$ErrorActionPreference = 'Continue'
$ErrorActionPreference > log.txt
get-item /not-here 2>&1 >> log.txt
$ErrorActionPreference = 'SilentlyContinue'
$ErrorActionPreference >> log.txt
get-item /not-here 2>&1 >> log.txt
$ErrorActionPreference = 'Stop'
$ErrorActionPreference >> log.txt
Try {
get-item /not-here 2>&1 >> log.txt
}
catch {
"`tError caught!" >> log.txt
}
$ErrorActionPreference = 'Ignore'
$ErrorActionPreference >> log.txt
get-item /not-here 2>&1 >> log.txt
$ErrorActionPreference = 'Inquire'
$ErrorActionPreference >> log.txt
get-item /not-here 2>&1 >> log.txt
$ErrorActionPreference = 'Continue'
When we run this script we get prompted when $ErrorActionPreference
is set to Inquire
.
PS C:\temp> .\test.ps1
Confirm
Cannot find path 'C:\not-here' because it does not exist.
[Y] Yes [A] Yes to All [H] Halt Command [S] Suspend [?] Help (default is "Y"): H
Get-Item: C:\temp\test.ps1:23
Line |
23 | get-item /not-here 2>&1 >> log.txt
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| The running command stopped because the user selected the Stop option.
检查日志文件时,会看到以下内容:
PS C:\temp> Get-Content .\log.txt
Continue
Get-Item: C:\temp\test.ps1:3
Line |
3 | get-item /not-here 2>&1 >> log.txt
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Cannot find path 'C:\not-here' because it does not exist.
SilentlyContinue
Stop
Error caught!
Ignore
Inquire
说明
不 (追加数据的重定向运算符, >
n>
) 覆盖指定文件的当前内容,而不发出警告。
但是,如果文件是只读、隐藏或系统文件,则重定向 会失败。 追加重定向运算符 (>>
和 n>>
) 不写入只读文件,但它们会将内容追加到系统或隐藏文件中。
若要强制将内容重定向到只读、隐藏或系统文件,请使用 Out-File
cmdlet 及其 Force
参数。
写入文件时,重定向运算符使用 UTF8NoBOM
编码。 如果文件具有不同的编码,则输出的格式可能不正确。 若要以不同的编码写入文件,请使用带其Encoding
参数的 Out-File
cmdlet。
与比较运算符的潜在混淆
运算符 >
不会与 大于 比较运算符混淆, (通常用其他编程语言) 表示 >
。
根据所比较的对象,使用 >
输出可能看起来正确 (,因为 36 不大于 42) 。
PS> if (36 > 42) { "true" } else { "false" }
false
但是,对本地文件系统的检查可以看到,调用 42
的文件是用内容 36
编写的。
PS> dir
Mode LastWriteTime Length Name
---- ------------- ------ ----
------ 1/02/20 10:10 am 3 42
PS> cat 42
36
尝试使用小于) 的反向比较 <
(会产生系统错误:
PS> if (36 < 42) { "true" } else { "false" }
At line:1 char:8
+ if (36 < 42) { "true" } else { "false" }
+ ~
The '<' operator is reserved for future use.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : RedirectionNotSupported
如果数值比较是必需的操作, -lt
-gt
则应使用。 请参阅: -gt
比较运算符