powershell 设置_极客学院:学习在PowerShell中进行格式设置,过滤和比较

powershell 设置_极客学院:学习在PowerShell中进行格式设置,过滤和比较

powershell 设置

powershell 设置_极客学院:学习在PowerShell中进行格式设置,过滤和比较

In this edition of Geek School, we look at formatting, filtering and comparing objects in the Pipeline.

在此版本的Geek School中,我们着眼于格式化,过滤和比较管道中的对象。

Be sure to read the previous articles in the series:

确保阅读本系列中的先前文章:

And stay tuned for the rest of the series all week.

并继续关注本系列的其余部分。

默认格式 (Default Formatting)

When I first started out with PowerShell, I thought everything was magic, but the truth is it just takes a little bit of time to understand what is going on underneath the hood. The same is true for the PowerShell formatting system. In fact, if you run the Get-Service cmdlet, the output generated only shows you 3 properties: Status, Name and DisplayName.

当我刚开始使用PowerShell时,我以为一切都是魔术,但事实是,了解引擎盖内部的情况只需要一点时间。 PowerShell格式化系统也是如此。 实际上,如果运行Get-Service cmdlet,则生成的输出仅显示3个属性:Status,Name和DisplayName。

powershell 设置_极客学院:学习在PowerShell中进行格式设置,过滤和比较

But if you pipe Get-Service to Get-Member, you see that the ServiceController objects have a lot more than just these three properties, so what is going on?

但是,如果通过管道将Get-Service传递到Get-Member,则会看到ServiceController对象不仅具有这三个属性,而且还发生了什么呢?

powershell 设置_极客学院:学习在PowerShell中进行格式设置,过滤和比较

The answer lies within a hidden file that defines how most of the built-in cmdlets display their output. To get an understanding, type the following into the shell and hit enter.

答案在一个隐藏文件中,该文件定义了大多数内置cmdlet如何显示其输出。 为了理解,请在shell中键入以下内容,然后按Enter。

notepad C:\Windows\System32\WindowsPowerShell\v1.0\DotNetTypes.format.ps1xml

记事本C:\ Windows \ System32 \ WindowsPowerShell \ v1.0 \ DotNetTypes.format.ps1xml

If we use notepad’s Find function, we can quickly skip to the section that details the output of the Get-Service cmdlet by searching for the ServiceController type.

如果使用记事本的Find函数,则可以通过搜索ServiceController类型来快速跳到详细介绍Get-Service cmdlet输出的部分。

powershell 设置_极客学院:学习在PowerShell中进行格式设置,过滤和比较

Suddenly, you can see that underneath the hood PowerShell is formatting any objects in the Pipeline that are of the ServiceController type and creating a table with three columns: Status, Name, and DisplayName. But what if the type you are dealing with doesn’t have an entry in that file, or any other format file for that matter? Well then, it’s quite simple actually. If the object coming out of the pipeline has 5 or more properties, PowerShell displays all of the object’s properties in a list; if it has less than 5 properties, it displays them in a table.

突然,您可以看到PowerShell在引擎盖下格式化了管道中ServiceController类型的所有对象,并创建了一个包含三列的表:Status,Name和DisplayName。 但是,如果要处理的类型在该文件或与此相关的任何其他格式文件中都没有条目怎么办? 那么,实际上很简单。 如果从管道中流出的对象具有5个或更多属性,则PowerShell将在列表中显示该对象的所有属性;否则,将在列表中显示该对象的所有属性。 如果属性少于5个,则将其显示在表格中。

格式化数据 (Formatting Your Data)

If you are not happy with the default formatting of an object or type, you can roll your own formatting. There are three cmdlets you need to know to do this.

如果您对对象或类型的默认格式不满意,可以滚动自己的格式。 您需要了解三个cmdlet才能执行此操作。

  • Format-List

    格式清单
  • Format-Table

    格式表
  • Format-Wide

    格式范围
powershell 设置_极客学院:学习在PowerShell中进行格式设置,过滤和比较

Format-Wide simply takes a collection of objects and displays a single property of each object. By default, it will look for a name property; if your objects don’t contain a name property, it will use the first property of the object once the properties have been sorted alphabetically.

Format-Wide只是获取对象的集合并显示每个对象的单个属性。 默认情况下,它将查找name属性; 如果您的对象不包含name属性,则按字母顺序对属性进行排序后,它将使用该对象的第一个属性。

Get-Service | Format-Wide

服务获取| 格式范围

powershell 设置_极客学院:学习在PowerShell中进行格式设置,过滤和比较

As you can see, it also defaults to two columns, although you can specify both which property you want to use, as well as how many columns you want to be displayed.

如您所见,尽管您既可以指定要使用的属性,也可以指定要显示的列数,但它也默认为两列。

Get-Service | Format-Wide -Property DisplayName -Column 6

服务获取| 格式范围-属性DisplayName-列6

powershell 设置_极客学院:学习在PowerShell中进行格式设置,过滤和比较

If something is formatted as a table by default, you can always switch it to list view by using the Format-List cmdlet. Let’s take a look at the output of the Get-Process cmdlet.

如果默认情况下将某些内容格式化为表格,则始终可以使用Format-List cmdlet将其切换为列表视图。 让我们看一下Get-Process cmdlet的输出。

powershell 设置_极客学院:学习在PowerShell中进行格式设置,过滤和比较

This tabular view actually suits this kind of information very well, but let’s pretend we want to view it in list form. All we really have to do is pipe it to Format-List.

这种表格视图实际上非常适合此类信息,但让我们假设我们想以列表形式查看它。 我们真正要做的就是将其通过管道传递给Format-List

Get-Process | Format-List

获取流程| 格式清单

powershell 设置_极客学院:学习在PowerShell中进行格式设置,过滤和比较

As you can see there are only four items displayed in the list by default. To view all the properties of the object, you can use a wildcard character.

如您所见,默认情况下,列表中仅显示四个项目。 要查看对象的所有属性,可以使用通配符。

Get-Process | Format-List –Property *

获取流程| 格式列表–属性*

powershell 设置_极客学院:学习在PowerShell中进行格式设置,过滤和比较

Alternatively, you can select just the properties you want.

或者,您可以仅选择所需的属性。

Get-Process | Format-List –Property name,id

获取流程| 格式列表–属性名称,id

powershell 设置_极客学院:学习在PowerShell中进行格式设置,过滤和比较

Format-Table, on the other hand, takes data and turns it into a table. Since our data from Get-Process is already in the form of a table, we can use it to easily choose properties we want displayed in the table. I used the AutoSize parameter to make all the data fit onto a single screen.

另一方面, Format-Table接收数据并将其转换为表。 由于来自Get-Process的数据已经是表格形式的,因此我们可以使用它轻松地选择要在表格中显示的属性。 我使用AutoSize参数使所有数据适合在单个屏幕上。

Get-Process | Format-Table name,id –AutoSize

获取流程| 格式表名称,id –AutoSize

powershell 设置_极客学院:学习在PowerShell中进行格式设置,过滤和比较

筛选和比较 (Filtering and Comparing)

One of the best things about using an object-based pipeline is that you can filter objects out of the pipeline at any stage using the Where-Object cmdlet.

使用基于对象的管道的最好的事情之一是,您可以在任何阶段使用Where-Object cmdlet将对象过滤出管道。

Get-Service | Where-Object {$_.Status -eq “Running”}

服务获取| 哪里对象{$ _。Status -eq“正在运行”}

powershell 设置_极客学院:学习在PowerShell中进行格式设置,过滤和比较

Using where object is actually very simple. $_ represents the current pipeline object, from which you can choose a property that you want to filter on. Here, were are only keeping objects where the Status property equals Running. There are a few comparison operators you can use in the filtering script block:

使用where对象实际上非常简单。 $ _代表当前管道对象,您可以从中选择要过滤的属性。 这里,仅保留Status属性等于Running的对象。 您可以在过滤脚本块中使用一些比较运算符:

  • eq (Equal To)

    eq(等于)
  • neq (Not Equal To)

    neq(不等于)
  • gt (Greater Than)

    gt(大于)
  • ge (Greater Than or Equal To)

    ge(大于或等于)
  • lt (Less Than)

    lt(小于)
  • le (Less Than or Equal To)

    le(小于或等于)
  • like (Wildcard String Match)

    像(通配符字符串匹配)

A full list and more information can be viewed in the about_comparison conceptual help file, however it does take some time getting used to the Where-Obeject syntax. That’s all for this time!

完整列表和更多信息可以在about_comparison概念性帮助文件中查看,但是习惯Where-Obeject语法确实需要一些时间。 到此为止!

翻译自: https://www.howtogeek.com/138324/geek-school-learning-formatting-filtering-and-comparing-in-powershell/

powershell 设置