首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >写入-主机写入列(格式化输出)

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

Stack Overflow用户
提问于 2018-01-17 11:36:42
回答 3查看 11K关注 0票数 1

我正在尝试使用Write-Host将数据输出到列中。我已经找到了一种方法来做到这一点,但它相当混乱:

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

代码语言:javascript
复制
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"
}

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

代码语言:javascript
复制
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])"

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

以下是输出的示例:

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-01-17 17:09:48

StephenP就在他给你的方向上。

至于这个..。

它确实有帮助,但我不能使用

-strings保持颜色,但这并不是问题。出于兴趣,您为什么要避免使用Write-Host?

使用Write-Host一直是一个激烈争论了很长一段时间的话题。甚至是PowerShell的发明者/作者。

PowerShell创始人杰弗里·斯诺弗写

-主机被认为是有害的

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

当您编写或查看PowerShell脚本时,我希望您记住以下经验法则:

使用写主机的◾几乎总是错误的。

Write-Host几乎总是错误的做法,因为它会干扰自动化。人们使用Write-Host的原因通常有两个:

关于这个主题还有很多其他的文章。在PoSH的早期版本中,写主机不能在流水线中使用,因为一旦使用它,数据就会从缓冲区中消失。

然而,在PoSHv5中杰弗里·斯诺弗现在说...

随着PowerShell v5写主机不再“杀死小狗”。数据被捕获到信息流https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Write-Information?view=powershell-5.1

描述

Write-Information cmdlet指定information如何处理命令的信息流数据。

Windows PowerShell 5.0引入了一个新的结构化信息流( Windows PowerShell streams中的数字6),您可以使用它在脚本及其调用方(或宿主环境)之间传输结构化数据。写入-信息允许您向流中添加信息性消息,并指定message如何处理命令的信息流数据。

通过这样做,您仍然可以使用颜色,而无需使用写入主机。

PowerTip:在不使用写入主机的情况下以彩色写入PowerShell输出

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

摘要:在不使用Write-Host cmdlet的情况下,将彩色输出写入colorized控制台。

嘿,编写脚本的家伙!问题:如何在不使用写主机cmdlet的情况下将输出写入PowerShell控制台?

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

代码语言:javascript
复制
$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

这是一个有点麻烦的...but。

我写了一个名为Set-TextColor的函数,我将它保存在我的配置文件中,以便于访问和使用。

票数 2
EN

Stack Overflow用户

发布于 2018-01-17 13:53:53

虽然我通常会完全避免使用写主机,但您可以使用一些here-strings来简化事情。

示例:

代码语言:javascript
复制
# 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

这是示例输出

代码语言:javascript
复制
                ------------------------------------------------------------
                Record Counts
                ------------------------------------------------------------
                Candidates          = 12345
                Contacts            = 4444
                Clients             = 1234
                Jobs                = 12
                Contracts           = 555
                Perm Placements     = 42

here-string将保留您的格式和间距,并且比您当前编写输出的方式更容易阅读和维护,但它仍然允许您使用变量来检索数据。

票数 2
EN

Stack Overflow用户

发布于 2018-01-18 08:53:10

因此,我决定继续使用Write-Host,因为我只是传达结果,一旦数据写入控制台,就不需要对数据做更多的事情。我最后写了一个函数来做这件事,因为我不能从here-string获得正确的输出,如下所示:

代码语言:javascript
复制
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])"
        }
    }
}

这样,每当我想要以这种格式显示结果时,我都可以调用一个包含数据数组的函数(这也解释了奇数个结果)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48293200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档