简体   繁体   English

如何在 powershell 中缩进命令 output

[英]How to indent command output in powershell

I am trying to present a ping command output in a nice indent... But, can't seem to format it properly....我试图在一个漂亮的缩进中显示一个 ping 命令 output……但是,似乎无法正确格式化它……

I want an output like this:我想要这样的 output:

Result: 
        Pinging google.com [216.58.208.110] with 32 bytes of data:
        Reply from 216.58.208.110: bytes=32 time=223ms TTL=61
        Reply from 216.58.208.110: bytes=32 time=129ms TTL=61

        Ping statistics for 216.58.208.110:
            Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
        Approximate round trip times in milli-seconds:
            Minimum = 129ms, Maximum = 223ms, Average = 176ms

However Powershell doesn't seem to want to allow me to do it without messing up the output但是 Powershell 似乎不想让我在不弄乱 output 的情况下这样做

Here is the code block I am working with这是我正在使用的代码块

$ping = & { Invoke-Expression "ping  -n 2 google.com" } 2>&1 | Out-String
Write-Host ""
Write-Host "$ping" -ForegroundColor Green
Write-Host ""

which sort of works, but with no indentation....哪种作品,但没有缩进....

When I tried different variations trying to get the indent, well they all failed misserably....当我尝试不同的变体试图获得缩进时,他们都失败了......

$ping = & { Invoke-Expression "ping  -n 2 google.com" } 2>&1 | Out-String
Write-Host ""
Write-Host "      $ping" -ForegroundColor Green
Write-Host ""

Result: This sort of worked, but only the first line was indented... The rest of the output was not...结果:这种方法有效,但只有第一行缩进了...... output 的 rest 不是......

$ping = & { Invoke-Expression "ping  -n 2 google.com" } 2>&1 | Format-Table 
Write-Host ""
Write-Host "    $ping" -ForegroundColor Green
Write-Host ""

Result: Powershell spit out a windows error and the table never did appear....结果:Powershell 吐出 windows 错误,表格从未出现....

$ping = & { Invoke-Expression "ping  -n 2 google.com" } 2>&1
foreach ($line in $ping ) {
  Write-Host ""
  Write-Host "      $line" -ForegroundColor Green
  Write-Host ""
}

Result: the first 3 lines of the input were aligned left and the remaining lines indented as expected...结果:输入的前 3 行左对齐,其余行按预期缩进...

$ping = & { Invoke-Expression "ping  -n 2 google.com" } 2>&1 
$indent = $ping.PadLeft(50)
Write-Host ""
Write-Host "    $indent" -ForegroundColor Green
Write-Host ""

Result: It was like I hit the align right button in word and the output was messed up...结果:就像我在 word 中点击了右对齐按钮,output 被弄乱了......

$ping = & { Invoke-Expression "ping  -n 2 google.com" } 2>&1
foreach ($line in $ping ) {
    Write-Host ""
    Write-Host "`t $line" -ForegroundColor Green
    Write-Host ""
}

Result: The tab never did appear, and the output was left aligned with no indentation...结果:选项卡从未出现,output 左对齐,没有缩进...

Can someone please point out the error of my ways... It's been driving me crazy for the last few days...有人可以指出我方法的错误吗……过去几天一直让我发疯……

Thanks in advance!提前致谢!

x X

Most command line tools output one line at a time, so drop Out-String and just prefix each line with the required amount of whitespace:大多数命令行工具 output 一次一行,因此删除Out-String并在每行前面加上所需数量的空格:

$ping = & { ping -n 2 google.com } 2>&1
$ping |ForEach-Object {"    $_"}

If a tool outputs multi-line strings (or if you really like Out-String ), you could use the -replace regex operator to add leading space at the start of the string and after any newline - this will work regardless of whether you have multiple single-line strings or one (or multiple) multi-line strings:如果工具输出多行字符串(或者如果您真的喜欢 Out-String ),您可以使用-replace regex 运算符在字符串的开头和任何换行符之后添加前导空格 - 无论您是否有,这都会起作用多个单行字符串或一个(或多个)多行字符串:

PS ~> $ping = & { ping -n 2 google.com } 2>&1 |Out-String
PS ~> $ping -replace '(?<=^|\n)',"`t"

        Pinging google.com [142.251.36.14] with 32 bytes of data:
        Reply from 142.251.36.14: bytes=32 time=12ms TTL=117
        Reply from 142.251.36.14: bytes=32 time=13ms TTL=117

        Ping statistics for 142.251.36.14:
            Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
        Approximate round trip times in milli-seconds:
            Minimum = 12ms, Maximum = 13ms, Average = 12ms

The regex pattern (?<=^|\n) matches any position immediately after the start of the string ( ^ ) or a literal newline character ( \n ).正则表达式模式(?<=^|\n)匹配紧跟在字符串 ( ^ ) 开头或文字换行符 ( \n ) 之后的任何 position。 "`t" is the escape sequence for a literal TAB, so we're telling PowerShell to insert a tab at any such position in the input string. "`t"是文字 TAB 的转义序列,因此我们告诉 PowerShell 在输入字符串中的任何此类 position 处插入一个制表符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM