我有一个PowerShell脚本,但是在继续下一行之前,有些输出会在任意点被剪掉。这真是太烦人了。
例如,我可以使用Write-Host,只要我愿意,行就会继续运行(注意,在Team和Team中运行的行添加了一些前缀信息--不过,也可以观察到将输出传输到文件中的相同效果):
[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中生成此输出(它添加了一些前缀信息):
[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".正如您所看到的,它还将文件路径拆分为多行,即使它被发送到一个文件,而不是控制台窗口。
发布于 2017-01-06 16:19:53
以下解决方案(十多岁了,所以最近可能存在一种更聪明的方法?)适用于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发布于 2017-01-07 01:59:34
您可以将其他流重定向到输出流,以便为详细输出和其他类型的输出提供自定义处理:
& {
#Script code here
Copy-Item -Path $fullSourcePath -Destination $destPath -Recurse -Verbose -ErrorAction Stop
} *>&1 | % {
function IsStreamType {
param($Object, $Type)
$Property = $_.PSObject.Properties[$Type]
$null -ne $Property -and $Property.Value -is [bool] -and $Property.Value
}
} {
switch(,$_) {
{
$_ -is [System.Management.Automation.DebugRecord] -and
(IsStreamType $_ WriteDebugStream)
} {
"Debug message: $($_.Message)"
}
{
$_ -is [System.Management.Automation.VerboseRecord] -and
(IsStreamType $_ WriteVerboseStream)
} {
"Verbose message: $($_.Message)"
}
{
$_ -is [System.Management.Automation.WarningRecord] -and
(IsStreamType $_ WriteWarningStream)
} {
"Warning message: $($_.Message)"
}
default { ,$_ }
}
}我使用,$_而不是普通$_作为预防措施,以防止PowerShell展开行为,以防$_碰巧是集合对象。
https://stackoverflow.com/questions/41504786
复制相似问题