【问题标题】:How to indent command output in powershell如何在powershell中缩进命令输出
【发布时间】:2021-09-28 22:08:23
【问题描述】:

我正在尝试以漂亮的缩进形式显示 ping 命令输出...但是,似乎无法正确格式化它....

我想要这样的输出:

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

但是 Powershell 似乎不想让我在不弄乱输出的情况下这样做

这是我正在使用的代码块

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

哪种有效,但没有缩进......

当我尝试不同的变体来获得缩进时,它们都失败了......

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

结果:这种工作,但只有第一行缩进...输出的其余部分没有...

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

结果: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 ""
}

结果:输入的前 3 行左对齐,其余行按预期缩进...

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

结果:就像我在word中点击了右对齐按钮,输出却乱七八糟...

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

结果:选项卡从未出现,输出左对齐,没有缩进...

有人可以指出我的方式的错误...过去几天一直让我发疯...

提前致谢!

x

【问题讨论】:

    标签: powershell indentation write-host


    【解决方案1】:

    大多数命令行工具一次输出一行,所以删除 Out-String 并在每行前面加上所需数量的空格:

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

    如果工具输出多行字符串(或者如果您真的很喜欢 Out-String),您可以使用 -replace 正则表达式运算符在字符串开头和之后添加前导空格任何换行符 - 无论您有多个单行字符串还是一个(或多个)多行字符串,这都会起作用:

    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
    

    正则表达式模式(?&lt;=^|\n) 匹配紧接在字符串开头 (^) 或文字换行符 (\n) 之后的任何位置。 "`t" 是文字 TAB 的转义序列,因此我们告诉 PowerShell 在输入字符串中的任何此类位置插入一个制表符。

    【讨论】:

    • 我喜欢 Mathias 的解释。我尝试了他的版本,以及以下版本(对我有用): $ping = & { ping -n 2 google.com } 2>&1; $平 | % { "`t$_" }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    • 2019-05-07
    相关资源
    最近更新 更多