简体   繁体   English

PowerShell相当于bash`exec >>(tee -a $ logfile); exec 2 >>(tee -a $ logfile>&2)`

[英]PowerShell equivalent to bash `exec > >(tee -a $logfile); exec 2> >(tee -a $logfile >&2)`

I am porting bash script logging to Powershell, which has the following at the top of the file: 我正在将bash脚本日志记录到Powershell,它在文件的顶部有以下内容:

# redirect stderr and stdout
backupdir="/backup"
logfile=$backupdir/"std.log"
exec >  >(tee -a $logfile)
exec 2> >(tee -a $logfile >&2)
echo "directory listing:"
ls -la

With the exec statements, both stdout and stderror are redirected to the logfile. 使用exec语句,stdout和stderror都会重定向到日志文件。

The exec command in bash is really nice as redirection is setup once at the start of the script. bash中的exec命令非常好,因为重定向在脚本开始时设置一次。 I want to avoid explicitly setting up redirection on each command if possible. 我想尽可能避免在每个命令上明确设置重定向。

Is there any equivalent to the above in PowerShell? PowerShell中是否有与上述相同的内容?

PowerShell 2,3,x have the Transcript cmdlets that attempt to record all output of the PowerShell window to a text file. PowerShell 2,3,x具有Transcript cmdlet ,它尝试将PowerShell窗口的所有输出记录到文本文件中。 There are some limitations . 一些限制

  • external executable stdout, stderr are not captured 外部可执行stdout,stderr未被捕获

Here's example code which demonstrates this: 这是演示这个的示例代码:

$this_path = Split-Path -Path $MyInvocation.MyCommand.Path -Parent
$log_path = Join-Path -Path $this_path -ChildPath script_log.txt
Start-Transcript -Path $log_path
$VerbosePreference = "continue"
$ErrorActionPreference = "continue"
$DebugPreference = "continue"
$WarningPreference = "continue"
& hostname.exe
Write-Host "write-host"
Write-Verbose "write-verbose"
Write-Error "write-error"
Write-Debug "write-debug"
Write-Warning "write-warning"
Get-Date
Stop-Transcript
& notepad $log_path

Everything above will be captured in script_log.txt except for the output of hostname.exe since it is an external executable. 以上所有内容都将在script_log.txt中捕获,但hostname.exe的输出除外,因为它是外部可执行文件。

There are some work arounds: 有一些工作:

powershell.exe -noprofile -file script.ps1 > script.log

This captures everything including hostname.exe 's output but it is something done outside of the script. 这会捕获包括hostname.exe输出在内的所有内容,但这是在脚本之外完成的。

Another is for each external command, pipe output through the host API: 另一个是针对每个外部命令,通过主机API输出管道:

& hostname.exe | Out-Default

This is done in the script, but you lose any text coloring from the exe on the shell window. 这是在脚本中完成的,但是你在shell窗口上的exe中丢失了任何文本颜色。

If I really needed to selectively redirect the output streams for the entire script, I'd run it as a background job. 如果我真的需要有选择地重定向整个脚本的输出流,我会将其作为后台作业运行。 The child job will have each of it's job streams in a separate buffer that can be read from the job object, so you can pretty much do whatever you want with them, including reading them while the job is running. 子作业将每个作业流放在一个可以从作业对象中读取的单独缓冲区中,因此您几乎可以随心所欲地执行任何操作,包括在作业运行时读取它们。

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

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