- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 PowerShell 脚本,我在其中使用 Start-Process 并将输出重定向到日志文件。 cmd 处于 for 循环中,我希望每次运行时都 append 输出。现在它每次运行时都会清除日志文件。有没有办法重定向和追加?
Start-Process $cmd -ArgumentList $cmdArgs -RedirectStandardError $logFile -Wait -NoNewWindow
最佳答案
注意:要同步执行控制台应用程序,请直接调用它们(c:\path\to\some .exe ...
或 & $exePath ...
),请勿使用 Start-Process
-请参阅this answer ;这样,您可以使用 $ouput = c:\path\to\some.exe ...
直接捕获它们的输出,或者使用 >> 将它们的输出 append 到现有文件中
或通过管道传输到 Add-Content
[1] - 请参阅 about_Redirection
2>&1
与 >> $logFile
结合使用,还捕获标准错误输出以及标准输出。<& $cmd $cmdArgs 2>&1 >> $logFile
2>> $logFile
来捕获仅标准错误输出。& $cmd $cmdArgs 2>> $logFile
此答案的其余部分解决了所提出的问题:
不,(从 PowerShell 7.0 开始)无法使用 Start-Process
append 到预先存在的文件。 '-RedirectStandardOutput
和 -RedirectStandardError
参数 - 它们总是替换指定的输出文件。
This answer展示了如何直接使用底层 .NET API,允许您在内存中收集进程的输出,然后允许您将其 append 到现有文件(例如,使用
Add-Content
)。
但是,如 your own answer显示,使用辅助临时文件-RedirectStandardError
,其内容稍后 append 到整个输出文件可能是最简单的解决方案。
[1] >>
盲目应用 Out-File
的默认编码,即 Windows 中的 UTF-16LE(“Unicode”) PowerShell,以及 PowerShell [Core] 6+ 中的无 BOM UTF-8;相比之下,Add-Content
会尝试匹配目标文件预先存在的编码。
关于powershell - RedirectStandardError $logFile 并 append ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60907656/
我有一个 PowerShell 脚本,我在其中使用 Start-Process 并将输出重定向到日志文件。 cmd 处于 for 循环中,我希望每次运行时都 append 输出。现在它每次运行时都会清
我们有一个服务,它启动一个进程并在服务停止时等待进程退出/服务用户调用停止(停止/终止服务启动的进程)。 偶尔,process.waitForExit(TimeSpan)挂起。 请注意,由 Servi
我是一名优秀的程序员,十分优秀!