输出中的PowerShell缺口
当我在一个脚本中一个接一个地运行两个命令时,我通常会出现很大的间隙,正如您可以看到的,这两个命令(我将它们与“;”连接起来)因此可以将问题视为一行,它只是一个输出中的PowerShell缺口,powershell,console,output,Powershell,Console,Output,当我在一个脚本中一个接一个地运行两个命令时,我通常会出现很大的间隙,正如您可以看到的,这两个命令(我将它们与“;”连接起来)因此可以将问题视为一行,它只是一个Get-Netipaddress后跟一个gwmi)它们之间有三行间隙。有时我希望在输出中包含更紧凑的信息。有没有办法告诉PowerShell停止在输出之间引入巨大差距 Get-Netipaddress | where AddressFamily -eq IPv4 | select IPAddress,InterfaceIndex,Inter
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)}}
您可以执行以下操作,但我不确定是否推荐它,因为它会破坏您进一步处理任何原始返回对象的能力
($(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'
您可以执行以下操作,但我不确定是否推荐它,因为它会破坏您进一步处理任何原始返回对象的能力
($(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'
还可以选择自己编写格式函数。 可能类似于以下内容:
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
}
这会将对象输出为表,不带任何前导或尾随换行符,即使用
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
它将两张桌子直接相撞
但是,在这种情况下,我会选择在表之间至少有一个换行符间隙,因此我会使用第一个表上的-AppendNewline
开关来输出:
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
还可以选择自己编写格式函数。 可能类似于以下内容:
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
}
这会将对象输出为表,不带任何前导或尾随换行符,即使用
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
它将两张桌子直接相撞
但是,在这种情况下,我会选择在表之间至少有一个换行符间隙,因此我会使用第一个表上的-AppendNewline
开关来输出:
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
在一行中添加命令,命令之间用分隔符分隔;不使该命令成为单个命令。。它仍然是一行中的两个语句。如果输出中有空行,您将在一行中看到用空格分隔的间隙添加命令;不使该命令成为单个命令。。它仍然是一行中的两个语句。如果输出中有空行,您将看到间隙