当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用格式命令更改输出视图

作者:精品下载站 日期:2024-12-14 03:02:23 浏览:14 分类:玩电脑

使用格式命令更改输出视图


PowerShell 具有一组 cmdlet,可让您控制如何显示特定对象的属性。所有 cmdlet 的名称均以动词 Format 开头。它们允许您选择要显示的属性。

Get-Command -Verb Format -Module Microsoft.PowerShell.Utility
CommandType     Name               Version    Source
-----------     ----               -------    ------
Cmdlet          Format-Custom      6.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-Hex         6.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-List        6.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-Table       6.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Format-Wide        6.1.0.0    Microsoft.PowerShell.Utility

本文介绍Format-WideFormat-ListFormat-Table cmdlet。

PowerShell 中的每种对象类型都有默认属性,当您未选择要显示的属性时,将使用这些默认属性。每个 cmdlet 使用相同的 Property 参数来指定要显示的属性。由于Format-Wide仅显示单个属性,因此其Property参数仅采用单个值,但Format-List和Format-Table 接受属性名称列表。

在此示例中,Get-Process cmdlet 的默认输出显示我们正在运行两个 Internet Explorer 实例。

Get-Process -Name iexplore

Process 对象的默认格式显示此处显示的属性:

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
     32    25.52      10.25      13.11   12808   1 iexplore
     52    11.46      26.46       3.55   21748   1 iexplore

使用 Format-Wide 进行单项输出

默认情况下,Format-Wide cmdlet 仅显示对象的默认属性。与每个对象关联的信息显示在单个列中:

Get-Command -Verb Format | Format-Wide
Format-Custom          Format-Hex
Format-List            Format-Table
Format-Wide

您还可以指定非默认属性:

Get-Command -Verb Format | Format-Wide -Property Noun
Custom                 Hex
List                   Table
Wide

使用列控制全格式显示

使用 Format-Wide cmdlet,您一次只能显示一个属性。这使得它对于在多列中显示大型列表非常有用。

Get-Command -Verb Format | Format-Wide -Property Noun -Column 3
Custom                 Hex                  List
Table                  Wide

使用 Format-List 进行列表视图

Format-List cmdlet 以列表的形式显示对象,每个属性都标记并显示在单独的行上:

Get-Process -Name iexplore | Format-List
Id      : 12808
Handles : 578
CPU     : 13.140625
SI      : 1
Name    : iexplore

Id      : 21748
Handles : 641
CPU     : 3.59375
SI      : 1
Name    : iexplore

您可以根据需要指定任意数量的属性:

Get-Process -Name iexplore | Format-List -Property ProcessName,FileVersion,StartTime,Id
ProcessName : iexplore
FileVersion : 11.00.18362.1 (WinBuild.160101.0800)
StartTime   : 10/22/2019 11:23:58 AM
Id          : 12808

ProcessName : iexplore
FileVersion : 11.00.18362.1 (WinBuild.160101.0800)
StartTime   : 10/22/2019 11:23:57 AM
Id          : 21748

使用带有通配符的 Format-List 获取详细信息

Format-List cmdlet 允许您使用通配符作为其Property 参数的值。这使您可以显示详细信息。通常,对象包含的信息比您需要的更多,这就是为什么 PowerShell 默认情况下不显示所有属性值的原因。要显示对象的所有属性,请使用Format-List -Property * 命令。以下命令为单个进程生成 60 多行输出:

Get-Process -Name iexplore | Format-List -Property *

尽管 Format-List 命令对于显示详细信息非常有用,但如果您想要包含许多项目的输出概览,则更简单的表格视图通常更有用。

使用 Format-Table 进行表格输出

如果您使用未指定属性名称的 Format-Table cmdlet 来格式化 Get-Process 命令的输出,则您将获得与不使用 代码>格式 cmdlet。默认情况下,PowerShell 以表格格式显示进程对象。

Get-Service -Name win* | Format-Table
Status   Name               DisplayName
------   ----               -----------
Running  WinDefend          Windows Defender Antivirus Service
Running  WinHttpAutoProx... WinHTTP Web Proxy Auto-Discovery Se...
Running  Winmgmt            Windows Management Instrumentation
Running  WinRM              Windows Remote Management (WS-Manag...

笔记

Get-Service 仅适用于 Windows 平台。

改进格式表输出

尽管表格视图对于显示大量信息很有用,但如果显示对于数据来说太窄,则可能难以解释。在前面的示例中,输出被截断。如果您在运行 Format-Table 命令时指定 AutoSize 参数,PowerShell 将根据实际显示的数据计算列宽。这使得列可读。

Get-Service -Name win* | Format-Table -AutoSize
Status  Name                DisplayName
------  ----                -----------
Running WinDefend           Windows Defender Antivirus Service
Running WinHttpAutoProxySvc WinHTTP Web Proxy Auto-Discovery Service
Running Winmgmt             Windows Management Instrumentation
Running WinRM               Windows Remote Management (WS-Management)

Format-Table cmdlet 可能仍会截断数据,但它仅在屏幕末尾截断。除了最后显示的属性之外,属性的大小与其最长的数据元素正确显示所需的大小相同。

Get-Service -Name win* |
    Format-Table -Property Name, Status, StartType, DisplayName, DependentServices -AutoSize
Name                 Status StartType DisplayName                               DependentServi
                                                                                ces
----                 ------ --------- -----------                               --------------
WinDefend           Running Automatic Windows Defender Antivirus Service        {}
WinHttpAutoProxySvc Running    Manual WinHTTP Web Proxy Auto-Discovery Service  {NcaSvc, iphl…
Winmgmt             Running Automatic Windows Management Instrumentation        {vmms, TPHKLO…
WinRM               Running Automatic Windows Remote Management (WS-Management) {}

Format-Table 命令假定属性按重要性顺序列出。该 cmdlet 尝试完全显示最接近开头的属性。如果 Format-Table 命令无法显示所有属性,它会从显示中删除一些列。您可以在前面的示例中的 DependentServices 属性中看到此行为。

将格式表输出包装在列中

您可以使用 Wrap 参数强制将冗长的 Format-Table 数据在其显示列中换行。使用 Wrap 参数可能不会达到您的预期,因为如果您没有指定 AutoSize,它会使用默认设置:

Get-Service -Name win* |
    Format-Table -Property Name, Status, StartType, DisplayName, DependentServices -Wrap
Name                 Status StartType DisplayName                               DependentServi
                                                                                ces
----                 ------ --------- -----------                               --------------
WinDefend           Running Automatic Windows Defender Antivirus Service        {}
WinHttpAutoProxySvc Running    Manual WinHTTP Web Proxy Auto-Discovery Service  {NcaSvc,
                                                                                iphlpsvc}
Winmgmt             Running Automatic Windows Management Instrumentation        {vmms,
                                                                                TPHKLOAD,
                                                                                SUService,
                                                                                smstsmgr…}
WinRM               Running Automatic Windows Remote Management (WS-Management) {}

单独使用Wrap 参数并不会显着降低处理速度。但是,使用AutoSize来格式化大型目录结构的递归文件列表可能需要很长时间,并且在显示第一个输出项目之前会使用大量内存。

如果您不关心系统负载,则 AutoSizeWrap 参数配合使用效果很好。初始列仍使用在一行上显示项目所需的宽度,但如有必要,最后一列将被换行。

笔记

当您首先指定最宽的列时,某些列可能不会显示。为了获得最佳结果,请首先指定最小的数据元素。

在下面的示例中,我们首先指定最宽的属性。

Get-Process -Name iexplore |
    Format-Table -Wrap -AutoSize -Property FileVersion, Path, Name, Id

即使进行换行,最后的 Id 列也会被省略:

FileVersion                          Path                                                  Nam
                                                                                           e
-----------                          ----                                                  ---
11.00.18362.1 (WinBuild.160101.0800) C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE iex
                                                                                           plo
                                                                                           re
11.00.18362.1 (WinBuild.160101.0800) C:\Program Files\Internet Explorer\iexplore.exe       iex
                                                                                           plo
                                                                                           re

组织表输出

表格输出控制的另一个有用参数是GroupBy。尤其是较长的表格列表可能很难比较。 GroupBy 参数根据属性值对输出进行分组。例如,我们可以按 StartType 对服务进行分组,以便于检查,并从属性列表中省略 StartType 值:

Get-Service -Name win* | Sort-Object StartType | Format-Table -GroupBy StartType
   StartType: Automatic
Status   Name               DisplayName
------   ----               -----------
Running  WinDefend          Windows Defender Antivirus Service
Running  Winmgmt            Windows Management Instrumentation
Running  WinRM              Windows Remote Management (WS-Managem…

   StartType: Manual
Status   Name               DisplayName
------   ----               -----------
Running  WinHttpAutoProxyS… WinHTTP Web Proxy Auto-Discovery Serv…

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯