我有一个 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".
我该如何阻止这种荒谬的行为呢?我希望输出在行尾正确渲染换行符,而不是在文件名中间。
UPDATE
下面的评论表明这是 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".
正如您所看到的,它也会将文件路径分割成几行,即使它被发送到文件而不是控制台窗口。