PowerShell -RedirectStandardOutput 教程
最后修改:2025 年 2 月 15 日
本教程将深入介绍 PowerShell 的 -RedirectStandardOutput 参数。它将解释如何捕获和重定向进程的标准输出。
输出流基础知识
PowerShell 进程具有三个主要的输出流:标准输出 (stdout)、标准错误 (stderr) 和标准输入 (stdin)。-RedirectStandardOutput 参数允许将 stdout 捕获到文件或变量。这对于记录或处理命令输出非常有用。
基本 -RedirectStandardOutput 用法
使用 -RedirectStandardOutput 最简单的方法是与 Start-Process 一起使用。此示例运行 ipconfig 并将输出保存到文件。该文件将包含网络配置详细信息。该参数接受文件路径作为其值。
Start-Process -FilePath "ipconfig" -RedirectStandardOutput "ipconfig.txt" -NoNewWindow -Wait
此命令运行 ipconfig 并将输出保存到 ipconfig.txt。-NoNewWindow 使进程保持在当前窗口中。-Wait 确保命令在继续之前完成。
将输出重定向到变量
您可以将进程输出捕获到变量而不是文件中。这需要使用 Out-String cmdlet 来转换输出。然后可以在脚本中进一步处理该变量。这种方法对于动态输出很有用。
$output = Start-Process -FilePath "systeminfo" -RedirectStandardOutput "temp.txt" -NoNewWindow -Wait -PassThru $result = Get-Content "temp.txt" | Out-String Remove-Item "temp.txt" $result
此示例将 systeminfo 输出捕获到临时文件中。然后将内容读入变量,并删除临时文件。-PassThru 返回进程信息。
合并标准输出和错误
要同时捕获 stdout 和 stderr,请同时使用 -RedirectStandardOutput 和 -RedirectStandardError。这可确保您在单独的文件中获得所有进程输出。这对于记录命令执行情况非常有用。
Start-Process -FilePath "ping" -ArgumentList "nonexistenthost" ` -RedirectStandardOutput "ping_out.txt" ` -RedirectStandardError "ping_err.txt" ` -NoNewWindow -Wait
此命令尝试 ping 一个不存在的主机,捕获输出和错误流。错误流将包含“Ping 请求找不到主机”消息。
附加到现有文件
默认情况下,-RedirectStandardOutput 会覆盖现有文件。要改为追加,请在脚本块中使用 >> 运算符。这对于随时间推移构建日志文件很有用。该示例显示了如何追加多个命令的输出。
$logFile = "system_log.txt" Start-Process -FilePath "hostname" -RedirectStandardOutput $logFile -NoNewWindow -Wait Start-Process -FilePath "whoami" -RedirectStandardOutput $logFile -NoNewWindow -Wait
这将创建一个包含主机名和当前用户信息的系统日志。每个命令的输出都追加到同一个文件。请注意,这实际上是覆盖,请参阅下一个示例以实现真正的追加。
使用脚本块进行真正的追加
为了实现真正的追加行为,请使用带有 Add-Content 的脚本块。此示例展示了累积输出的正确技术。输出首先被捕获到变量,然后追加。这可以避免潜在的文件锁定问题。
$logFile = "true_append.log" $tempFile = "temp_output.txt" Start-Process -FilePath "netstat" -ArgumentList "-ano" ` -RedirectStandardOutput $tempFile -NoNewWindow -Wait Add-Content -Path $logFile -Value (Get-Content $tempFile) Remove-Item $tempFile
这将 netstat 输出捕获到临时文件,然后将其追加到日志。之后会清理临时文件。此方法可确保原子追加操作。
重定向 PowerShell 命令输出
-RedirectStandardOutput 可与外部命令一起使用,但对于 PowerShell cmdlet,请使用重定向运算符。此示例同时显示了这两种方法。对于原生的 PowerShell 输出,> 运算符更简单。
# For external commands Start-Process -FilePath "dir" -RedirectStandardOutput "dir_output.txt" -NoNewWindow -Wait # For PowerShell cmdlets Get-Process > "processes.txt"
第一个命令尝试运行 dir(在类 Unix 系统上会失败)。第二个命令将 Get-Process 输出捕获到一个文件。为每种命令类型使用适当的方法。
处理大型输出
对于产生大量输出的命令,请考虑流式传输输出。此示例显示了如何逐行处理输出。它可防止内存问题与非常大的输出。该文件被读取并增量处理。
$outputFile = "large_output.txt" Start-Process -FilePath "powershell" -ArgumentList "Get-ChildItem C:\ -Recurse" ` -RedirectStandardOutput $outputFile -NoNewWindow -Wait Get-Content $outputFile | ForEach-Object { # Process each line here if ($_ -match "\.exe$") { $_ } }
这将递归列出 C:\ 上的所有文件,并将它们保存到文件中。然后逐行处理文件,筛选出 .exe 文件。这种方法内存效率很高。
超时和进程控制
重定向输出时,您可能需要处理长时间运行的进程。此示例显示了超时处理。脚本会在进程运行时间过长时终止它。这可以防止无限期挂起。
$process = Start-Process -FilePath "ping" -ArgumentList "google.com -t" ` -RedirectStandardOutput "ping_continuous.txt" -NoNewWindow -PassThru try { $process | Wait-Process -Timeout 5 -ErrorAction Stop } catch { $process | Stop-Process -Force Write-Output "Process timed out and was terminated" }
这会启动对 google.com 的连续 ping。如果运行时间超过 5 秒,它将被终止。最多超时时间的输出将被保存。对于不可靠的命令很有用。
来源
在本文中,我们深入介绍了 -RedirectStandardOutput 参数。
作者
列出 所有 PowerShell 教程。