gpt4 book ai didi

powershell - 将主机写入列(格式化输出)

转载 作者:行者123 更新时间:2023-12-03 00:45:32 28 4
gpt4 key购买 nike

我正在尝试使用 Write-Host将数据输出到列中。我找到了一种方法来做到这一点,但它非常困惑:

首先,我使用以下函数设置控制台大小:

function fn_SetConsoleSize {
param ([int]$height, [int]$width, [string]$title)

$bheight = $height
if ($height -le 50) {
$bHeight = 51
}

$bwidth = $width
if ($width -le 150) {
$bwidth = 150
}

$pshost = Get-Host
$pswindow = $pshost.ui.rawui
$newsize = $pswindow.buffersize
$newsize.height = $bHeight
$newsize.width = $bwidth
$pswindow.buffersize = $newsize

<# Window Size #>
$newsize = $pswindow.windowsize
$newsize.height = $height
$newsize.width = $width
$pswindow.windowsize = $newsize

<# Other Console Changes #>
$pswindow.windowtitle = $title
$pswindow.foregroundcolor = "Yellow"
$pswindow.backgroundcolor = "Black"
}

然后我只使用空格设置列大小:

Write-Host ( " " * 20 ) "Candidates        = $($Counts[0])" -nonewline
Write-Host (" " * ( 32 - $($Counts[0]).tostring().length)) "Candidates = $($Counts[0])"

Write-Host ( " " * 20 ) "Contacts = $($Counts[1])" -nonewline
Write-Host (" " * ( 32 - $($Counts[1]).tostring().length)) "Contacts = $($Counts[1])"

这确实按照我想要的方式输出,但是我的项目的这一部分会很长,所以如果可能的话我想大大简化它。

这是一个输出示例:

Enter image description here

最佳答案

StephenP 为您指明了方向。

至于这个……

it does help but I wouldn't be able to keep the colours using here-strings, but that's not really an issue. Out of interest, why would you avoid using Write-Host?

长期以来,使用 Write-Host 一直是一个热门话题。甚至是 PowerShell 的发明者/作者。

Write-Host Considered Harmful - by PowerShell founder Jeffrey Snover

http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful

When you are writing or reviewing PowerShell scripts, I’d like you to remember the following rule of thumb:

◾Using Write-Host is almost always wrong.

Write-Host is almost always the wrong thing to do because it interferes with automation. There are typically two reasons why people use Write-Host:

关于该主题还有很多其他文章。在早期版本的 PoSH 中,Write-Host 不能在管道中使用,因为当你使用它时,数据就从缓冲区中消失了。

However, in PoSHv5 Jeffrey Snover now says...

With PowerShell v5 Write-Host no longer "kills puppies". data is captured into info stream https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Write-Information?view=powershell-5.1

Description

The Write-Information cmdlet specifies how Windows PowerShell handles information stream data for a command.

Windows PowerShell 5.0 introduces a new, structured information stream (number 6 in Windows PowerShell streams) that you can use to transmit structured data between a script and its callers (or hosting environment). Write-Information lets you add an informational message to the stream, and specify how Windows PowerShell handles information stream data for a command.

通过执行此操作,您仍然可以在不使用 Write 主机的情况下使用颜色......

PowerTip:在不使用 Write-Host 的情况下以彩色写入 PowerShell 输出

https://blogs.technet.microsoft.com/heyscriptingguy/2012/10/11/powertip-write-powershell-output-in-color-without-using-write-host

总结:在不使用 Write-Host cmdlet 的情况下将彩色输出写入 Windows PowerShell 控制台。

您好,脚本专家!问题 如何在不使用 Write-Host cmdlet 的情况下将输出写入 Windows PowerShell 控制台?

您好,脚本专家!回答 使用 $host.Ui.RawUi 将 ForegroundColor 设置为不同的颜色,然后使用 Write-Output cmdlet 写入输出。完成后,将 ForegroundColor 设置回其原始颜色。

$t = $host.ui.RawUI.ForegroundColor

$host.ui.RawUI.ForegroundColor = "DarkGreen"

Write-Output "this is green output"

this is green output

$host.ui.RawUI.ForegroundColor = $t

...但恕我直言,这有点麻烦。

我编写了一个名为 Set-TextColor 的函数,并将其保存在我的个人资料中以便于访问和使用。

关于powershell - 将主机写入列(格式化输出),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48293200/

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