我要做的是使用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])"
这确实输出了我想要的方式,但是我的项目的这部分将会很长,所以如果可能的话我想简化这个。
以下是输出示例:
答案 0 :(得分:1)
虽然我一般都不会使用写主机,但你可以简单地使用here-strings。
示例:
#some setup - this would obviously come from your data layer
$candidates = 12345
$contacts = 4444
$clients = 1234
$jobs = 12
$contracts = 555
$pplacements = 42
#create the here-string
$here_string = @"
------------------------------------------------------------
Record Counts
------------------------------------------------------------
Candidates = $candidates
Contacts = $contacts
Clients = $clients
Jobs = $jobs
Contracts = $contracts
Perm Placements = $pplacements
"@
#clear the console and write the string
Clear-Host
$here_string
这是示例输出
------------------------------------------------------------
Record Counts
------------------------------------------------------------
Candidates = 12345
Contacts = 4444
Clients = 1234
Jobs = 12
Contracts = 555
Perm Placements = 42
here-string将保留您的格式和间距,并且比您编写输出的当前方式更容易阅读和维护,但仍然允许您使用变量来检索数据。
答案 1 :(得分:1)
至于......
它确实有帮助,但我无法保持使用颜色 here-strings,但这不是一个问题。出于兴趣,为什么 你会避免使用Write-Host吗?
使用Write-Host已经成为一个备受争议的话题。甚至是PowerShell的发明者/作者。
写主机被认为是有害的 - 由PowerShell创始人Jeffrey Snover
http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful
当您编写或查看PowerShell脚本时,我希望您这样做 请记住以下经验法则:
◾使用Write-Host几乎总是错误的。
Write-Host几乎总是错误的,因为它 干扰自动化。通常有两个原因 人们使用Write-Host:
该主题中有许多其他文章。在早期版本的PoSH中,Write-Host无法在管道中使用,因为当你使用它时,数据就会从缓冲区消失。
然而,在PoSHv5中,Jeffrey Snover现在说......
使用PowerShell v5 Write-Host不再&#34;杀死小狗&#34;。数据是 捕获到信息流 https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Write-Information?view=powershell-5.1
描述
Write-Information cmdlet指定Windows PowerShell的处理方式 命令的信息流数据。
Windows PowerShell 5.0引入了新的结构化信息流 (可以用于传输的Windows PowerShell流中的数字6) 脚本与其调用者(或托管者)之间的结构化数据 环境)。 Write-Information允许您添加信息性消息 到流,并指定Windows PowerShell如何处理信息 流命令的数据。
通过这样做,您仍然可以使用颜色而不使用写主机...
PowerTip:使用Write-Host
以彩色编写PowerShell输出摘要:将彩色输出写入Windows PowerShell控制台,而不使用Write-Host cmdlet。
嘿,脚本专家!问题如何在不使用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的函数,我将其保存在我的配置文件中,以便于访问和使用。
答案 2 :(得分:0)
因此,我决定继续使用Write-Host,因为我只是传达结果,并且一旦将数据写入控制台就不需要对数据做更多的事情了。我最后编写了一个函数来执行此操作,因为我无法从here-string获得正确的输出,这里是:
function fn_OutputColumns {
param ( $columnCount, $outputData )
$outputDataLength = @()
$increase = [math]::Round($outputData.count / 2, [System.MidpointRounding]::AwayFromZero)
if ( $outputData.Count % 2 -ne 0 ) { $oddColumn = 1 } else { $oddColumn = 0 }
foreach ( $item in $outputData ) {
$outputDataLength += $item.Length
}
for ( $i = 0; $i -lt $increase; $i++ ) {
if ( $oddColumn -eq 1 ) {
do {
Write-Host ( " " * 20 ) "$($outputData[$i][1])" ( " " * ( 20 - $($outputData[$i][1]).length ) ) " = $($outputData[$i][0])" -nonewline
Write-Host ( " " * ( 24 - $($outputData[$i + $increase][0]).tostring().length ) ) $($outputData[$i + $increase][1]) ( " " * ( 15 - $($outputData[$i + $increase][1]).length ) ) " = $($outputData[$i][0])"
$i++
} while ( $i -ne $increase - 1 )
Write-Host ( " " * 20 ) "$($outputData[$i][1])" ( " " * ( 20 - $($outputData[$i][1]).length ) ) " = $($outputData[$i][0])"
$i++
}
else {
Write-Host ( " " * 20 ) "$($outputData[$i][1])" ( " " * ( 20 - $($outputData[$i][1]).length ) ) " = $($outputData[$i][0])" -nonewline
Write-Host ( " " * ( 24 - $($outputData[$i + $increase][0]).tostring().length ) ) $($outputData[$i + $increase][1]) ( " " * ( 15 - $($outputData[$i + $increase][1]).length ) ) " = $($outputData[$i][0])"
}
}
}
这样,每当我想以这种格式显示结果时,我就可以用一个数据数组调用一个函数(也会产生奇数的结果)。