gpt4 book ai didi

powershell 在输出中放置了不适当的换行位置

转载 作者:行者123 更新时间:2023-12-03 14:36:56 28 4
gpt4 key购买 nike

我有一个 PowerShell 脚本,但是在继续下一行之前,某些输出会在任意点被剪切。这是令人难以置信的烦人。

例如,我可以使用 Write-Host 并且只要我需要,该行就会继续运行(注意,在 Team City 中运行,Team City 添加了一些前缀信息 - 但是可以观察到相同的效果将输出管道传输到文件) :

[10:04:45] [Step 5/7]  - Found Windows Service at G:\TeamCityData\TeamCityBuildAgent-1\work\282b8abc9094651e\Artefacts\windows-services\WindowsService.Dummy\WindowsService.DummyService.exe

其他时候,输出似乎在任意点人为地截断,就像它包裹在一个窗口(不存在)中一样。

所以,这一行:
Copy-Item -Path $fullSourcePath -Destination $destPath -Recurse -Verbose -ErrorAction Stop

在 Team City 中生成此输出(添加了一些前缀信息):
[10:04:46] [Step 5/7] VERBOSE: Performing the operation "Copy File" on target "Item: 
[10:04:46] [Step 5/7] G:\TeamCityData\TeamCityBuildAgent-1\work\282b8abc9094651e\Artefacts\windows-services\WindowsService.Dummy\WindowsServi
[10:04:46] [Step 5/7] ce.DummyService.exe Destination:
[10:04:46] [Step 5/7] \\SERVER001\scheduled-tasks\ProductFolder\Dev\DummyWindowsService\WindowsService.DummyService.exe".
[10:04:46] [Step 5/7] VERBOSE: Performing the operation "Copy File" on target "Item:
[10:04:46] [Step 5/7] G:\TeamCityData\TeamCityBuildAgent-1\work\282b8abc9094651e\Artefacts\windows-services\WindowsService.Dummy\WindowsServi
[10:04:46] [Step 5/7] ce.DummyService.exe.config Destination:
[10:04:46] [Step 5/7] \\SERVER001\scheduled-tasks\ProductFolder\Dev\DummyWindowsService\WindowsService.DummyService.exe.config".

我如何停止这种荒谬的行为?我希望输出在行尾正确呈现换行符,而不是在文件名中间。

更新

下面的评论表明这是 TeamCity 捕获输出方式的问题。但是,如果我直接在 PowerShell 控制台中执行类似的命令并将输出通过管道传输到文件,则会发生同样的问题。

命令:
copy-item -Path F:\logs -Destination .\ -Recurse -Verbose *> F:\logs\copy-item-output-1.txt

产生这样的输出:
Performing the operation "Copy File" on target "Item: F:\logs\20161103-140649-ProductName.Program.log 
Destination: F:\Destination\1234567890abcdefghijklmnopqrstuvwxyz\this-is-a-long-path-name-to-show-wrapping-issues-with-copy-it
em\logs\20161103-140649-ProductName.Program.log".

如您所见,它还会跨行拆分文件路径,即使它被发送到文件,而不是控制台窗口。

最佳答案

以下解决方案(more than 10 years old so there might exist a smarter way recently?)适用于 PowerShell 和 PowerShell ISE:

$pshost = get-host
$pswindow = $pshost.ui.rawui
$newsize = $pswindow.buffersize
### do not change $newsize.height
$newsize.width = 3000 ### [int] type; max. value unknown at present
$pswindow.buffersize = $newsize

关于powershell 在输出中放置了不适当的换行位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41504786/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com