PS C:\> ipconfig | Select-String -Pattern “IPv4” -SimpleMatch The command that uses the pipeline and the associated output are shown here (in addition, it does not matter if I use the simplematch switched parameter or not): It seems that it might be a lost cause, but if you stick with it and use the pipeline, the output is much more readable. The command and output are shown in the following figure.Īs can be seen in the preceding figure, the results are completely jumbled and unreadable. Select-String -Pattern “IPV4” -InputObject (ipconfig) I try putting parentheses around the command to force evaluation first, and I at least see something returned to the screen. PS C:\> Select-String -Pattern “IPV4” -InputObject ipconfig For example, if I want to parse the results of the ipconfig command, I might be tempted to do something like this: The online help for the cmdlet is rather extensive, and the examples do not seem to cover simple cases. This is shown in the following figure.įiguring out how to use the Select-String cmdlet can be a bit confusing. When I run the function to load it into memory, I can then access it by alias if I wish. New-Alias -Name GIA -Value Get-IPAddress -Description “HSG alias” (Get-WmiObject -class win32_NetworkAdapterConfiguration ` The Get-IPAddress function and code to create an alias are shown here: However, if I put the above command in a function and I place it in my profile or some module, I can call it as easily as typing Get-IPAddress. (gwmi win32_NetworkAdapterConfiguration -Filter ‘ipenabled = “true”‘).ipaddressĪs you can tell, typing the preceding command is a bit cumbersome when compared to typing ipconfig. I index into the array to return only the IPv4 address: Unfortunately, the ipaddress property returns an array with both the IPv4 and IPv6 address in it. I only have one network interface card that is ipenabled, so I can return the ipaddress property directly. Note that gwmi is an alias for the Get-WMIObject cmdlet. The following command returns the IP address on my local computer. But I can use WMI to obtain the IP address as well. Often, I will look around for an object that will return the same types of information that I might obtain via a legacy command-to avoid the issue of extensive text parsing.įor example, the ipconfig command will return an IP address and a log of other information. KG, if you are going to work with legacy commands very often, you will need to become competent at using the Select-String cmdlet. All in all, it was a Seattle Chamber of Commerce type of day. The day I flew into Seattle was absolutely beautiful, and I was able to see all the mountains stretching up into the sky. We even had a few sun sightings during the week. The TechReady conference in Seattle last week was great. It seems that all these commands return data, and I would love to be able to easily parse the information that the commands return. Hey, Scripting Guy! I have enjoyed reading Sean’s legacy scripting articles, but I am a bit confused. Summary: In this article, Microsoft Scripting Guy Ed Wilson teaches how to use the Windows PowerShell version of grep to parse the command line.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |