从 MSBuild 调用 PowerShell 会破坏长文本行

发布于 2024-10-19 22:22:22 字数 348 浏览 3 评论 0原文

我有一个调用 PowerShell 脚本的 MSBuild 脚本。 PS 脚本的输出包含一些长行,其中一些(但不是全部)被分成多行,例如。

wait-untilOpCompleted : Cannot bind argument to parameter 'operationId' because
   it is null.

我怎样才能阻止它这样做?我只想要一条长线,例如:

wait-untilOpCompleted : Cannot bind argument to parameter 'operationId' because it is null.

I have an MSBuild script that calls a PowerShell script. The output of the PS script contains some long lines and some of those (but not all) are broken up into multiple lines, eg.

wait-untilOpCompleted : Cannot bind argument to parameter 'operationId' because
   it is null.

How do I stop it from doing that? I just want one long line, eg.:

wait-untilOpCompleted : Cannot bind argument to parameter 'operationId' because it is null.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身骄傲 2024-10-26 22:22:22

防止 PowerShell 根据控制台宽度换行的最简单方法是覆盖脚本顶部 Out-Default 的默认实现,如下所示:

filter Out-Default { 
   $input | Out-String -Width 500 -Stream | 
            Microsoft.PowerShell.Utility\out-default 
}

The easiest way to prevent PowerShell from breaking lines based on the console's width is to override the default implementation of Out-Default at the top of your script like so:

filter Out-Default { 
   $input | Out-String -Width 500 -Stream | 
            Microsoft.PowerShell.Utility\out-default 
}
紅太極 2024-10-26 22:22:22

Keith的回答似乎较少侵扰性的。但是,如果您无法使其正常工作,您可以尝试的另一件事是手动设置主机的宽度。来自 上一个答案

# Update output buffer size to prevent clipping in Visual Studio output window.
if( $Host -and $Host.UI -and $Host.UI.RawUI ) {
  $rawUI = $Host.UI.RawUI
  $oldSize = $rawUI.BufferSize
  $typeName = $oldSize.GetType( ).FullName
  $newSize = New-Object $typeName (500, $oldSize.Height)
  $rawUI.BufferSize = $newSize
}

它只是在主机的 RawUI 输出缓冲区(不过,由于我们在多个环境中运行构建,并且我们不希望脚本仅仅因为它无法使输出更大一点而失败,因此代码相当具有防御性)。

如果您在始终设置 RawUI 的环境中运行(并且大多数都这样做),则代码可以大大简化:

$Host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size (500, 25)

Keith's answer seems less intrusive. However, if you have not been able to get it to work, another thing you might try is manually setting the width of the host. From a previous answer:

# Update output buffer size to prevent clipping in Visual Studio output window.
if( $Host -and $Host.UI -and $Host.UI.RawUI ) {
  $rawUI = $Host.UI.RawUI
  $oldSize = $rawUI.BufferSize
  $typeName = $oldSize.GetType( ).FullName
  $newSize = New-Object $typeName (500, $oldSize.Height)
  $rawUI.BufferSize = $newSize
}

It simply sets a new width of 500 characters on the host's RawUI output buffer (though, since we run our build in several environments, and we did not want the script to fail just because it could not make the output a bit larger, the code is rather defensive).

If you run in an environment that always sets RawUI (and most do), the code can be greatly simplified:

$Host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size (500, 25)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文