- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将 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
通过 exec 语句,stdout 和 stderror 都被重定向到日志文件。
bash 中的 exec 命令非常好,因为在脚本开始时设置一次重定向。如果可能,我想避免在每个命令上显式设置重定向。
在 PowerShell 中是否有与上述等效的东西?
最佳答案
PowerShell 2,3,x 有 Transcript cmdlets尝试将 PowerShell 窗口的所有输出记录到文本文件中。有some limitations .
下面是演示这一点的示例代码:
$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
除了 hostname.exe
的输出外,上面的所有内容都将被捕获到 script_log.txt 中,因为它是一个外部可执行文件。
有一些解决方法:
powershell.exe -noprofile -file script.ps1 > script.log
这会捕获所有内容,包括 hostname.exe
的输出,但它是在脚本之外完成的。
另一个是针对每个外部命令,通过主机 API 管道输出:
& hostname.exe | Out-Default
这是在脚本中完成的,但是您会丢失 shell 窗口中 exe 的任何文本着色。
关于shell - PowerShell 相当于 bash `exec >>(tee -a $logfile); exec 2>>(tee -a $logfile >&2)`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15979060/
我有一个关于 this answer 的问题,如下引用,由 friedo 回答此处的另一个问题。 (我无权对此发表评论,所以我将其作为一个问题提出。) "You can use File::Tee.
我有一个关于 this answer 的问题,在下面引用,由 friedo 在这里回答另一个问题。 (我无权对此发表评论,所以我将此作为问题提出。) "You can use File::Tee. u
您好,我一直在用 C 编写一个 linux shell。我想将我的输出重定向到文件和终端,我发现 tee 是可行的方法。我去了 tee 的 linux 手册页,发现 tee 可以用作函数调用以在 C
有没有办法在发送到文件之前处理来自 tee 的文本? 例如,如果程序输出以下行: stack 11 stack 22 stack 33 serverfault serverfault stack 44
下面是一些关于itertools.tee的测试: li = [x for x in range(10)] ite = iter(li) ========================
我正在将 bash 脚本日志记录移植到 Powershell,它在文件顶部有以下内容: # redirect stderr and stdout backupdir="/backup" logfile
我尝试将 echo 命令保存到日志文件: echo "XXXXX" | tee -a ./directory_with_logs/my_script.log 当文件 my_script.log 存在时
我正在尝试使用 tee 将我的流输出为 1 分钟的片段并同时输出到一个文件中。这是我的命令: ffmpeg -i "rtsp://${cameraIp}:554/axis-media/media.am
我想在 ksh 脚本(使用 exec)中创建一个管道,该管道连接到 tee,并将输出发送到管道。 当前: #Redirect EVERYTHING exec 3>&1 #Save STDOUT as
tee 从标准输入读取并写入标准输出和文件。 some_command |& tee log tee 可以写入压缩文件吗? some_command |& tee -some_option log.b
这个问题已经有答案了: Can you redirect Tee-Object to standard out? (2 个回答) 已关闭去年。 我生成一个 csv 文件: myscript.ps1 |
我有以下代码。 $summary = . { while ($true) { # Generating huge list of psobject } } | Tee-
这个问题已经有答案了: Can you redirect Tee-Object to standard out? (2 个回答) 已关闭去年。 我生成一个 csv 文件: myscript.ps1 |
有人可以帮我解决这个问题吗?我目前正在尝试将查询写入文件,最终将用 notee 关闭它;称呼。我以前使用过发球电话,但由于某种原因,今天我遇到了问题。 这是有问题的语法: tee c:/trash/t
我是 MySQL(或一般 SQL)新手我试图让 MySQL 使用 TEE 命令将时间戳写入带有存储过程的文件中(我不认为我可以使用“select into outfile”,因为我不想删除该文件,我想
我目前正在使用以下内容来捕获进入终端的所有内容并将其放入日志文件中 exec 4&2>&>(tee -a $LOG_FILE) 但是,我不想让颜色转义码/困惑进入日志文件。所以我有这样的东西,有点管用
这个问题在这里已经有了答案: Force line-buffering of stdout in a pipeline (7 个答案) 关闭 9 年前。 我正在运行这样的命令: python myc
我正在尝试在 ubuntu 15.04 上将 tee 命令与 rendercheck 测试一起使用,tee 命令可以很好地处理 6 个 rendercheck 测试,例如: ./renderchec
我想通过使用 while 循环和读取来模拟 shell 脚本中 tee 命令的行为,或者是否可以查看命令的内容。 最佳答案 不确定你在问什么,但为了一个简单的例子,试试这个 - file=$1
我想要这样的东西 $> ps -ax | tee -a processes.txt 在 UNIX C 编程环境中,意味着不通过 shell 脚本。 基本上有一个 API,这样我就可以在 STDIN 和
我是一名优秀的程序员,十分优秀!