问题描述
我有一个 PowerShell 脚本,但在继续下一行之前,某些输出在任意点被剪切.这太烦人了.
I have a PowerShell script, but some of the output gets cut at an arbitrary point before continuing on the next line. This is incredibly annoying.
例如,我可以使用 Write-Host
并且只要我想要,该行就会继续(注意,在 Team City 中运行,Team City 添加了一些前缀信息 - 但是可以观察到将输出通过管道传输到文件的相同效果):
For example, I can use Write-Host
and the line will continue on as long as I want it (note, that is running in Team City and Team City adds some prefix information - however the same effect can be observed piping the output to a file):
[10:04:45] [Step 5/7] - Found Windows Service at G:\TeamCityData\TeamCityBuildAgent-1\work\282b8abc9094651e\Artefacts\windows-services\WindowsService.Dummy\WindowsService.DummyService.exe
其他时候,输出似乎会在任意点人为地截断,就像它包裹在一个窗口(不存在)内一样.
Other times, the output will seem to artificially truncate at an arbitrary point, like it was wrapping within a window (which isn't there).
所以,这一行:
Copy-Item -Path $fullSourcePath -Destination $destPath -Recurse -Verbose -ErrorAction Stop
在 Team City 中生成此输出(添加一些前缀信息):
Produces this output in Team City (which adds some prefix information):
[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".
我该如何制止这种荒谬行为?我希望输出在行尾正确呈现换行符,而不是在文件名中间.
How do I stop this absurdity? I want the output to render line breaks correctly at the end of the line, not in the middle of a filename.
更新
下面的评论表明这是 TeamCity 捕获输出方式的问题.但是,如果我直接在 PowerShell 控制台中执行类似的命令并将输出通过管道传输到文件,则会发生同样的问题.
A comment below suggested this was an issue with the way TeamCity was capturing the output. However, the same issue happens if I do a similar command directly in a PowerShell console and pipe the output to a file.
命令:
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".
如您所见,它还会跨行拆分文件路径,即使它被发送到文件,而不是控制台窗口.
As you can see, it also splits the file path across lines, even although it is being sent to a file, not the console window.
推荐答案
以下解决方案(已经超过 10 年了,所以最近可能存在更智能的方法?) 在这两个PowerShell 和 PowerShell ISE:
The following solution (more than 10 years old so there might exist a smarter way recently?) works in both PowerShell and 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 在输出中放置了不适当的换行位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!