我有一个在 Jenkins 构建中执行的 Powershell 构建步骤,控制台将输出包装在第 80 列(这似乎是默认值)。有没有办法防止这种列换行,并让 Jenkins 使用更适合我们期望的输出的列宽?
我遇到了同样的问题。我发现通过这个link你可以增加控制台的宽度和高度。不过,我的最大宽度限制为 128 W 和 62 H,否则会出现错误。
所以我最终得到了这个:
$pshost = get-host
$pswindow = $pshost.ui.rawui
$newsize = $pswindow.buffersize
$newsize.height = 3000
$newsize.width = 128
$pswindow.buffersize = $newsize
$newsize = $pswindow.windowsize
$newsize.height = 62
$newsize.width = 128
$pswindow.windowsize = $newsize
由于这个宽度不够,当我输出对象数组时,我将其通过管道传输到 format-table cmdlet,并使用 -Wrap 开关。
例如
Get-EventLog -LogName Application -Newest 10 | Format-Table -Wrap
产生以下输出:
虽然 Avner 的答案是正确的,但我发现每次你的 jenkins 管道遇到新的 powershell 步骤时都必须重新定义窗口和缓冲区大小。
避免这种情况的方法是为 powershell.exe 设置默认窗口和缓冲区大小。 mkelement0 在他的回答中解释了一种方法。
以编程方式设置
窗口大小默认值:powershell.exe
以下 PSv5+ 代码片段将
启动的控制台窗口的默认窗口大小设置为 100 列 x 50 行。powershell.exe
请注意,屏幕 buffer 值是从整体默认设置中继承,直接存储在
中,这增加了复杂性。HKCU:\Console
# Determine the target registry key path.
$keyPath = 'HKCU:\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe'
# Get the existing key or create it on demand.
$key = Get-Item $keyPath -ErrorAction SilentlyContinue
if (-not $key) { $key = New-Item $keyPath }
# Determine the new size values.
[uint32] $cols = 100; [uint32] $lines = 50
# Convert to a DWORD for writing to the registry.
[uint32] $dwordWinSize = ($cols + ($lines -shl 16))
# Note: Screen *buffer* values are inherited from
# HKCU:\Console, and if the inherited buffer width is larger
# than the window width, the window width is apparently set to
# the larger size.
# Therefore, we must also set the ScreenBufferSize value, passing through
# its inherited height value while setting its width value to the same
# value as the window width.
[uint32] $dwordScreenBuf = Get-ItemPropertyValue HKCU:\Console ScreenBufferSize -EA SilentlyContinue
if (-not $dwordScreenBuf) { # No buffer size to inherit.
# Height is 3000 lines by default.
# Note that if we didn't set this explicitly, the buffer height would
# default to the same value as the window height.
$dwordScreenBuf = 3000 -shl 16
}
# Set the buffer width (low word) to the same width as the window
# (so that there's no horizontal scrolling).
$dwordScreenBuf = $cols + (($dwordScreenBuf -shr 16) -shl 16)
# Write the new values to the registry.
Set-ItemProperty -Type DWord $key.PSPath WindowSize $dwordWinSize
Set-ItemProperty -Type DWord $key.PSPath ScreenBufferSize $dwordScreenBuf
对于任何使用具有 powershell 核心的 Linux 工作人员遇到此问题的人,建议的解决方案不起作用。您无法设置缓冲区大小。当尝试输出格式化数据(即格式表)时,特别会出现此问题。
参见:https://github.com/PowerShell/PowerShell/issues/20110
要解决此问题,您可以使用
out-string -width 400