PowerShell Tee-Object 未捕获文件中的调试行

2023-12-12

我有一个通过自动化运行的 PowerShell 脚本,因此我需要将脚本的输出捕获到文件中,但我还想捕获运行的命令,为输出提供一些上下文(我会使用set -x在 Linux shell 脚本中)。不过,我不知道如何将这些命令捕获到 Windows 中的输出文件中;任何帮助深表感谢!

# script.ps1

Set-PSDebug -Trace 1
Write-Host "Hello, World!"

我如何调用脚本(从cmd):

$ powershell -command "./script.ps1 *>&1 | Tee-Object -FilePath ./output.txt"

终端输出:

DEBUG:    2+  >>>> Write-Host "Hello, World!"
Hello, World!

输出.txt 文件内容:

Hello, World!

您可以看到输出文件缺少终端中显示的调试行。理想情况下,我希望调试行仅位于输出文件中(而不是终端输出),但是在两个地方都有调试线也很好。

请注意,PowerShell 5.1 是我安装的版本。


Unfortunately, PowerShell's tracing output (activated via Set-PSDebug) operates outside PowerShell's system of output streams, so such output cannot be captured from inside a PowerShell session.[1]

However, an outside caller of PowerShell's CLI[2], such as cmd.exe in your case, does receive tracing output via stdout (the standard output stream)[3], so you can use cmd.exe's redirection to capture everything in a file and print it to the console afterwards.

一个简化的例子(来自cmd.exe):

C:\>powershell -c "Set-PSDebug -Trace 1; Write-Host 'Hello, World!'" > output.txt & type output.txt

DEBUG:    1+ Set-PSDebug -Trace 1;  >>>> Write-Host 'Hello, World!'
Hello, World!

[1] Perhaps surprisingly, despite the word "Debug" in the cmdlet name, tracing output does not go through stream number 5, the debugging stream, the way that Write-Debug output does.

[2] Note that you can use this technique of calling the CLI, invariably as a child process, from inside an existing PowerShell session as well, in which case that existing session acts as the outside caller. However, note that passing the command to execute via a script block ({ ... }), which is normally preferable, would not work in this case, because a script block-based invocation uses PowerShell's remoting infrastructure behind the scenes, which preserves the PowerShell-specific output streams (and also type fidelity, with limitations), so that the tracing output again goes straight to the console.

[3] Perhaps surprisingly, all of PowerShell's output streams - as well as to-console-only output such as produced by tracing - are by default mapped to an outside caller's stdout stream - including errors. While you can at least separate out errors on demand via a stderr redirection - 2> - on the caller side, all remaining streams are invariably sent to stdout - see the bottom section of this answer and GitHub issue #7989 for more information.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PowerShell Tee-Object 未捕获文件中的调试行 的相关文章

  • 区分注册表项和值路径

    是否有相当于 System IO DirectoryInfo and System IO FileInfo 用于区分注册表项和值 我想评估一条路径并为用户记录该路径的最终目标是什么 到目前为止 这就是我所拥有的 而且有点丑陋 path Re
  • 如何通过 PowerShell 获取注册表项的所有权?

    我尝试通过 PowerShell 获取注册表项的所有权 但它悄无声息地失败了 我正在使用以下代码 get the username of the current user uname System Security Principal Wi
  • powershell 中的展开存档无法提取嵌套文件夹和文件

    我有以下简单的 powershell 将 zip 文件夹 包含其他文件夹和仅日志文件 提取到目的地 FolderPath C Temp Whatever Expand Archive Path FolderPath logs zip Des
  • 获取我的 databricks 工作区中所有笔记本的列表

    如何获取工作区中所有笔记本的列表并将其名称以及完整路径存储在 csv 文件中 我已尝试使用 Databricks CLI 选项 但似乎没有递归操作 databricks 工作区列表 https docs databricks com dev
  • 域用户的AD密码有效期

    我正在使用以下命令从其他域获取用户信息 并且我成功获取了该数据 如何添加过滤器以获取以下查询中用户的密码到期日期 获取 ADuser 用户名 服务器 abc com 这不是超级简单 你必须计算它 Get ADUser username se
  • 如何读取 Dockerfile 中的 Powershell 变量?

    我正在建造适用于 Windows 的 Docker 桌面图像 我尝试将变量传递给 Powershell 命令 但它不起作用 Dockerfile escape FROM microsoft windowsservercore SHELL p
  • 如何使用 PowerShell 检查文件是否早于特定时间?

    我怎样才能办理登机手续电源外壳查看 fullPath 中的文件是否早于 5 天 10 小时 5 分钟 老 我的意思是如果创建或修改不晚于 5 天 10 小时 5 分钟 这是一种非常简洁但易读的方法 lastWrite get item fu
  • 使用 Powershell 命令进行文件计数

    如何使用以下命令计算特定文件夹 以及所有子文件夹 中的所有文件电源外壳命令 Get ChildItem 和 Get ChildItem
  • 使用 powershell 从 Tfs 获取工作项存储

    如何使用 powershell 从 TFS 获取 WorkItemStore 我尝试过以下方法 function get tfs param string ServerName http MyServer 8080 tfs begin pr
  • 带有note属性的字符串可以修改吗

    有没有什么方法可以修改字符串而不丢失它所具有的任何注释属性 t something Add Member noteproperty one 1 PassThru t one 1 t else t one
  • 如何从 .ps1 文件运行 powershell 脚本?

    我正在尝试自动执行一个简单的 PS 脚本 以删除某个 txt 文件 显然 我是 powershell 新手 当我在 shell 中运行代码时 它工作完美 但是当我将代码保存为 ps1 并双击它 或远程执行它 时 它只是弹出一个窗口 什么也不
  • 输入编码:接受UTF-8

    我需要在 PowerShell 下获取本机应用程序的输出 问题是 输出是用 UTF 8 无 BOM 编码的 PowerShell 无法识别它 只是将那些时髦的 UTF 字符直接转换为 Unicode 我发现 PowerShell 有 Out
  • 写入输出-InputObject(,'测试')-NoEnumerate

    我认为这是一个仅适用于 PowerShell Windows 5 1 的错误 The NoEnumerate当通过命名提供输入时 开关不起作用 InputObject争论 以下函数返回False Function Test Write Ou
  • Powershell get-item VersionInfo.ProductVersion 不正确/与 WMI 不同

    我试图理解为什么 Powershell 会返回 DLL 文件的版本号 而不是 Windows 资源管理器中的文件属性页和 WMI 查询显示的版本号 如果这不符合编码问题的正确条件 我提前表示歉意 场景 运行以下 powershell 命令
  • 无法找到类型 [Microsoft.TeamFoundation.Build.Workflow.WorkflowHelpers]

    我正在尝试对来自指定进程参数的 Powershell 的构建进行排队 我收到以下错误 知道我做错了什么吗 无法找到类型 Microsoft TeamFoundation Build Workflow WorkflowHelpers 确保包含
  • 在 Powershell 中获取小文件的“磁盘大小”

    我正在使用一个遗留系统 该系统有大量来自外部系统的导入 其中大多数通过下载文件 根据上下文大小不同 处理它 然后将文件存储在 SAN 卷上的其他位置 格式为 NTFS 和安装在 WS2008R2 盒子上 我们遇到的问题是 由于簇的大小 大量
  • 在Powershell中显示目录结构及其大小

    尝试使用 dir 命令来显示子文件夹和文件的大小 在谷歌搜索 powershell目录大小 后 我发现了两个有用的链接 确定文件夹的大小http technet microsoft com en us library ff730945 as
  • 我怎么知道PowerShell函数参数被省略了

    考虑这样的函数 function Test foo bar 我们可以称之为 Test foo null Test 我如何知道何时省略了 foo 以及何时为 null 如果您使用的是 Powershell V2 或更高版本 则可以使用 PSB
  • Powershell - 将字符串拆分为由开始和结束字符串划分的数组

    我有一个多行字符串 来自 json 例如 somekey somevalue somekey somevalue somekey somevalue somekey somenumber somekey null 我想将字符串拆分为一个数组
  • Powershell pscustomobject 格式-表新行而不是一行

    我有一个非常大的员工 JSON 响应 我正在尝试将其转换为表格式 导出为 CSV 并最终插入到 SQL Server 中 我能够确定如何从 json 文件中获取所有变量 但是现在我将所有值插入到每列的一行中 而不是为每个员工插入一个新行 另

随机推荐

  • 单击 vue 组件中的“保存”按钮后如何关闭模式?

    我的 vue 组件是这样的
  • Java listjar 目录中的文件

    有什么办法可以使用吗listFiles 在已打包到 jar 中的目录上 假设我的资源目录中有一个目录 其中包含一些文本文件 texts text1 txt and texts text2 txt 在这个 Java 程序中 我有一个需要使用的
  • 正确使用GetRawInputBuffer

    我正在尝试使用 Win32 原始输入 API 以更高精度收集原始鼠标数据 但我似乎无法理解文档和样本完全为了GetRawInputBuffer 当我的鼠标悬停在窗口上时 似乎什么也没有发生 当我单击或释放窗口标题栏时 我似乎只获得缓冲数据
  • NetBeans 不再为以前生成的代码生成 JAR 文件

    两年前 我使用 Java 编写了一个简单的上传小程序 使用 NetBeans 开发 当我第一次点击 清理和构建 时 以及之后的一段时间 它运行得非常完美 它将运行整个构建过程并生成一个 JAR 文件 然后我可以将其上传到我的 Web 服务器
  • 用于阻止特定日期(假期)的引导日期选择器配置

    有谁知道如何配置日期选择器不显示特定日期 例如 7 月 4 日 这似乎可以使用 beforeShowDay 来完成 但我并不肯定 http jsfiddle net Lr3taznx a array of dates that should
  • 如何启用cookie

    我有这个任务来读取 写入 启用 cookie 以便将用户名存储在变量中 然后写入 cookie 我的问题是代码的最后一部分似乎正在工作 但是用户名应该存储到变量中的第一部分不起作用 我可以看到当我运行代码时 前两个警报框没有显示 它应该以
  • React传单和react-leaflet-draw

    我正在尝试在传单地图上实现绘制功能 我创建了一个仅安装了react leaflet的新应用程序 使用npx create react app并安装了以下软件包 npm install React React dom 传单 npm 安装反应传
  • 复制到剪贴板在 Android 上不起作用

    使用此视图创建标准移动应用程序 public class DebugView extends View ListView
  • 为什么 sys.getrefcount 给出巨大的值?

    import sys a 10 b a print sys getrefcount a b 1 print sys getrefcount b output 22 614 我的Python解释器有问题吗 为什么这会给出像 614 这样巨大的
  • Android:Proguard 的推荐配置是什么?

    我正在为 Android 开发应用程序并使用 Proguard 来混淆代码 目前我正在使用 ProGuard 配置 optimizationpasses 5 dontusemixedcaseclassnames dontskipnonpub
  • MVC4 & IClientValidatable - 自动 AJAX 调用服务器端验证

    我正在寻找在 MVC4 中实现自定义客户端验证 我目前让它与标准属性配合得很好 例如我的模型中的这个 public class UploadedFiles StringLength 255 ErrorMessage Path is too
  • 使用 std::for_each 和 std::views::iota 的并行 for 循环

    我想使用以下方法为基于并行索引的 for 循环设置一个简单的解决方法std views 为了按顺序运行 代码如下所示 int main pseudo random numbers random device rd default rando
  • 使多维数组唯一的php

    我的数组如下所示 大批 1 gt stdClass Object id gt 225 user id gt 1 name gt Blue Quilted Leather Jacket by Minusey 499 2 gt stdClass
  • 添加行时数据表中的重复列

    我创建一个 DataTable 并将其绑定到 DataGrid 我的数据源由一个表 FooTable 组成 该表由一列 FooName 组成 以下代码运行良好 除了每次添加新行时 都会有一个重复的列填充相同的数据 我不知道如何摆脱它 请参阅
  • 使用 XPATH 或 CSS 选择器在 Selenium 中查找元素

    我试图在 C 中使用 Selenium Webdriver 查找元素 导入 已尝试以下代码但没有找到它 driver FindElement By XPath class menu bg ul li 3 Click driver FindE
  • 正确删除单例

    我有以下代码 我的类 h static MyMutex instanceMutex static MyClass getInstance static void deleteInstance 我的类 c MyMutex MyClass in
  • 卷曲发送内容长度标头时出现问题

    我需要将文件上传到 wupload 这是我的功能 public function doPost link postfields cookie ref nobody false upload false header true headers
  • 当类型参数之一应该为 Nothing 时,为什么 Scala 的隐式类不起作用?

    Update 我修改了示例以便可以编译和测试 我有一个定义丰富方法的隐式类 case class Pipe I O R f I gt O R object Pipe The problematic implicit class implic
  • Docker - 名称已被容器使用

    运行docker使用以下命令的注册表总是会抛出错误 dev tmp me docker run d name registry v1 e SETTINGS FLAVOR local e STORAGE PATH registry e SEA
  • PowerShell Tee-Object 未捕获文件中的调试行

    我有一个通过自动化运行的 PowerShell 脚本 因此我需要将脚本的输出捕获到文件中 但我还想捕获运行的命令 为输出提供一些上下文 我会使用set x在 Linux shell 脚本中 不过 我不知道如何将这些命令捕获到 Windows