官术网_书友最值得收藏!

Using filters and exporting data

In order to understand how powerful Windows PowerShell is, you need to look at the filtering capabilities it offers. With an understanding of objects and pipelining, you can retrieve the exact information you are looking for. The Usage of Pipeline section talks about how pipelines can be used to pass one or more objects, which are the output of one command, and feed them into another cmdlet. In Windows PowerShell, there are commands that will help you to refine and get the desired output. Some of these cmdlets that we will look in this section are Where-Object, Sort-Object, Select-Object, and Select-String.

The Where-Object cmdlet helps you to filter the output returned by other cmdlets. For example, the Get-service cmdlet without any parameter retrieves information about the services installed on the computer on which it is called:

Now, let's filter the output of Get-Service to find the list of services that are stopped:

Windows PowerShell supports the following logical operators that we are going to use in our next example of filtering the output of a Get-Service cmdlet:

We will now find services that are running whose name starts with Windows using the –and operator:

By default, Windows PowerShell will sort the output of commands using an alphabetical order. You can then use the Sort-object cmdlet to sort it using any way you like. For example, the following example will first sort the output of the Get-Process cmdlet by the Process ID and then the CPU time:

At times, you might need to filter your results based on certain specific property names. This is where the Select-Object cmdlet comes in handy. It selects the stated properties of a single or group of objects. You can use Select-Object to select objects that are unique in a group of objects It supports parameters such as First, Last, Skip, and Index. You can select one or multiple object properties using the –Property parameter. Let's take an example of the output of the Get-Process cmdlet, and let's say you are troubleshooting a performance issue on a server and are only interested to know about the CPU time and process name properties. The output of the Get-Process cmdlet will be piped to Select-Object with the –property parameter set to the process name and CPU. The Format-list cmdlet will format the output displayed in the console:

PS C:\> Get-Process | Select-Object -Property Processname, CPU | format-list

Now, let's use another parameter called –Unique of Select-Object. This will select a single member from a number or objects with same properties and values. In the following example, you will notice that the –Unique parameter is case sensitive and reports "c" and "C" as unique.

Another useful filtering cmdlet is Select-String, used to select text patterns in strings and files. If you are familiar with UNIX shells, it is used as Grep or Findstr in Windows. It can display all the matches or stop after the first match depending on the usage of the cmdlet. You can search files of Unicode text using this command as well.

First, we will create a file using the redirection operator (>) to store the list of services running on a server to a text file:

PS C:\> Get-Service > service.txt

Now, we will find the text "Network" and "Windows" to get a list of all the services that have the word in it. We will then use the –notmatch parameter to list all the services except this word:

Note that the search performed here is a case insensitive search; you can use the —case sensitive parameter to make it case sensitive, which means you won't get any output if you use the parameter with the previous command as there are no matches found.

主站蜘蛛池模板: 互助| 手机| 海门市| 盐城市| 丘北县| 辉县市| 洛宁县| 涿州市| 汝南县| 钦州市| 济阳县| 玉溪市| 阿巴嘎旗| 玛纳斯县| 介休市| 黄石市| 余干县| 电白县| 东方市| 卓资县| 长垣县| 巴林右旗| 岳西县| 佛坪县| 绥滨县| 青浦区| 仲巴县| 金寨县| 江油市| 郸城县| 和田县| 保山市| 若羌县| 和政县| 汉阴县| 洛川县| 景谷| 黑山县| 绥宁县| 闻喜县| 刚察县|