《Learn Windows PowerShell in a Month of Lunches Third Edition》读书笔记——CHAPTER 10 Formatting

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_41104353/article/details/82251730

10.2 Working with the default formatting

默认的格式在.format.ps1xml文件里。比如关于process对象的格式在 DotNetTypes.format.ps1xml 文件里,我们可以使用如下命令查看该文件:

PS C:\>cd $pshome
PS C:\>notepad dotnettypes.format.ps1xml

要找到某一具体对象的格式规则,我们得知道该对象的类型以及其对应的格式文件。比如对于 get-process 这个命令的输出格式,我们使用 get-process | gm 查看其类型,然后复制其类型 System.Diagnostics.Process ,然后在刚刚打开的文件中搜索 System.Diagnostics.Process 。注意,搜索到的第一个可能是一个 ProcessModule 对象,而不是我们想要的 Process 对象,我们要找到的是 Process 对象。

当我们运行 Get-Process cmdlet的时候,会发生如下事情:
1. cmdlet将对象的类型 System.Diagnostics.Process 放入pipeline中。
2. 在pipeline的结尾处,有一个隐藏的cmdlet Out-Default ,它的任务是接受pipeline中的对象。
3. Out-Default 将对象传给 Out-Host
4. 大多数 Out- 开头的cmdlet没有处理标准对象的能,他们的任务是将对象送给格式化系统。
5. 格式化系统查看对象的类型,遵循一套内置的格式化规则来产生格式化指令,然后将这指令传给 Out-Host
6. Out-Host 收到这些指令的时候,就会按照这些指令在屏幕上构造输出的排版。

上述也会发生在我们人为定义 Out- cmdlet的时候。比如 Get-Process | Out-File procs.txt 就是将 Out-Host 换成 Out-File

第5步中的标准化规则有三个,系统会逐个尝试,直到成功找到。
1. first formatting rule:The system looks to see whether the type of object it’s dealing with has a predefined view.
2. second formatting rule:It looks to see whether anyone has declared a default display property set for that type of object.
3. third formatting rule:is about the kind of output to create.If the formatting system displa
ys four or fewer properties, it uses a table. If there are five or more properties, it uses a list.

比如 Get-WmiObject Win32_OperatingSystem 产生的对象类型是 System.Management.ManagementObject#root\cimv2\Win32_OperatingSystem ,我们在所有 .format.ps1xml 中搜索 Win32_OperatingSystem 都不会找到预定义的视图。这时,系统会使用 the second formatting rule ,发现此对象可以在 Types.ps1xml 中找到。

PowerShell共有四个关于格式化的cmdlet,它们分别如下:

10.3 Formatting tables

首先是 Format-Table 别名 Ft

10.4 Formatting lists

Sometimes you need to display more information than will fit horizontally in a table, which can make a list useful. Format-List is the cmdlet you’ll turn to, or you can use its alias, Fl

10.5 Formatting wide lists

The last cmdlet, Format-Wide (or its alias, Fw), displays a wide list. It’s able to display only the values of a single property, so its -property parameter accepts only one property name, not a list, and it can’t accept wildcards.
By default, Format-Wide looks for an object’s Name property, because Name is a commonly used property and usually contains useful information. The display generally defaults to two columns, but a -columns parameter can be used to specify more columns: Get-Process | Format-Wide name -col 4
.
最后一个cmdlet 是Format-Custom
Format-Custom is mainly used to display various predefined custom views.

10.7 Going out: to a file, a printer, or the host

If a command line ends in a Format- cmdlet, the formatting instructions created by the Format- cmdlet go to Out-Default, which forwards them to Out-Host, which displays them on the screen:
Get-Service | Format-Wide
You could also manually pipe the formatting instructions to Out-Host, which accomplishes exactly the same thing:
Get-Service | Format-Wide | Out-Host
Alternatively, you can pipe formatting instructions to either Out-File or Out-Printer to direct formatted output to a file or to hard copy.

扫描二维码关注公众号,回复: 3207048 查看本文章

10.8 Another out: GridViews

Out-GridView 绕过了格式化子系统。
Out-GridView can’t receive the output of a Format- cmdlet—it can receive only the regular objects output by other cmdlets.

10.9 Common points of confusion

10.9.1 Always format right

Your Format- cmdlet should be the last thing on the command line, with Out-File or Out-Printer as the only exceptions.

10.9.2 One type of object at a time, please

The next thing to avoid is putting multiple kinds of objects into the pipeline.
因此运行 Get-Process; Get-Service 是不可取的

猜你喜欢

转载自blog.csdn.net/sinat_41104353/article/details/82251730
今日推荐