輸出中的 PowerShell 空白 (PowerShell gaps in output)


問題描述

輸出中的 PowerShell 空白 (PowerShell gaps in output)

當我在腳本中一個接一個地運行兩個命令時,我經常會出現很大的差距,正如您在這兩個命令中看到的那樣(我已將它們與“;”連接起來,因此可以將問題視為一個‑liner,它只是一個 Get‑Netipaddress 後跟一個 gwmi)它們之間的最後一個三行間隙。有時我希望輸出中包含更緊湊的信息。有沒有辦法告訴 PowerShell 停止在輸出之間引入巨大的差距?

Get‑Netipaddress | where AddressFamily ‑eq IPv4 | select IPAddress,InterfaceIndex,InterfaceAlias | sort InterfaceIndex ; gwmi win32_logicaldisk | Format‑Table DeviceId, VolumeName, @{n="Size(GB)";e={[math]::Round($_.Size/1GB,2)}},@{n="Free(GB)";e={[math]::Round($_.FreeSpace/1GB,2)}}

參考解法

方法 1:

You can do the following, but I'm not sure I recommend it because it ruins your ability to further process any of the originally returned objects.

($(Get‑Netipaddress | Where AddressFamily ‑eq IPv4 | Select IPAddress,InterfaceIndex,InterfaceAlias | Sort InterfaceIndex ; gwmi win32_logicaldisk | Format‑Table DeviceId, VolumeName, @{n="Size(GB)";e={[math]::Round($_.Size/1GB,2)}},@{n="Free(GB)";e={[math]::Round($_.FreeSpace/1GB,2)}}) | Out‑String) ‑replace '(?m)^\r?\n'

方法 2:

There's always also the option of writing a format function yourself. Something like the below perhaps:

function Format‑TableCompact {
    [CmdletBinding()]  
    Param (
        [parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)] 
        [PsObject]$InputObject,

        [switch]$AppendNewline
    )
    # If the data is sent through the pipeline, use $input to collect is as array
    if ($PSCmdlet.MyInvocation.ExpectingInput) { $InputObject = @($Input) }
    # or use : $InputObject = $Input | ForEach‑Object { $_ }

    $result = ($InputObject | Format‑Table ‑AutoSize | Out‑String).Trim()
    if($AppendNewline) { $result += [Environment]::NewLine }
    $result
}

This will output the object as table without any leading or trailing newlines, so called using

Get‑Netipaddress | where AddressFamily ‑eq IPv4 | select IPAddress,InterfaceIndex,InterfaceAlias | sort InterfaceIndex | Format‑TableCompact
gwmi win32_logicaldisk | Format‑Table DeviceId, VolumeName, @{n="Size(GB)";e={[math]::Round($_.Size/1GB,2)}},@{n="Free(GB)";e={[math]::Round($_.FreeSpace/1GB,2)}} | Format‑TableCompact

it will slam both tables directly against eachother.

In this case however I would opt for having at least one newline gap between the tables so I would use the ‑AppendNewline switch on the first table to output:

Get‑Netipaddress | where AddressFamily ‑eq IPv4 | select IPAddress,InterfaceIndex,InterfaceAlias | sort InterfaceIndex | Format‑TableCompact ‑AppendNewline
gwmi win32_logicaldisk | Format‑Table DeviceId, VolumeName, @{n="Size(GB)";e={[math]::Round($_.Size/1GB,2)}},@{n="Free(GB)";e={[math]::Round($_.FreeSpace/1GB,2)}} | Format‑TableCompact

(by YorSubsAdminOfThingsTheo)

參考文件

  1. PowerShell gaps in output (CC BY‑SA 2.5/3.0/4.0)

#Console #powershell #output






相關問題

grails 日誌消息未顯示在 STS 3.0 控制台上 (grails log messages not displaying on STS 3.0 console)

在 AppFog 上,如何在終端(node.js)上查看控制台日誌 (on AppFog, how to see the console logs on the terminal (node.js))

控制台應用程序中的多個參數未正確解析 (Multiple args in Console Application not parsing correctly)

WPF 應用程序沒有輸出到控制台? (No output to console from a WPF application?)

用 .NET 4.5 編寫的 Windows 服務中的 Console.Out 和 Console.Error 競爭條件錯誤 (Console.Out and Console.Error race condition error in a Windows service written in .NET 4.5)

Visual Basic 2013 - 控制台輸入令牌? (Visual Basic 2013 - Console Input Tokens?)

帶有 stringbuilder 日曆表的控制台日曆 (Console Calendar with stringbuilder calendar sheet)

增加和減少混淆 (Incrementing and Decrementing Confusion)

有沒有辦法在標準 C++ 中直接從鍵盤讀取輸入? (Is there a way to read input directly from the keyboard in standard C++?)

是否可以在方案中編寫控制台應用程序? (Is it possible to write console applications in scheme?)

犀牛的控制台輸入功能? (console input function for rhino?)

輸出中的 PowerShell 空白 (PowerShell gaps in output)







留言討論