Prof. Powershell
Command-Line Objects
Turn any command-line tool into a PowerShell tool.
- By Jeffery Hicks
- 06/14/2011
I'm sure you've heard me extol the wonders of PowerShell as an object-based shell. When you have objects in the pipeline you can sort, group, filter, export and much, much more.
I'm sure you also know you can still run legacy command-line tools in PowerShell. However, the challenge is that these commands don't lend themselves to the pipeline approach, since they don't write objects. But actually, they do: strings. But other than send output to a file, those strings won't do much for you. So, let me give you some techniques you can use to turn command-line tools into PowerShell tools.
For the sake of this lesson, I'm going to take the output of NBTSTAT /N and turn it into a PowerShell object:
PS C:\> nbtstat /n
Local Area Connection:
Node IpAddress: [10.23.36.71] Scope Id: []
NetBIOS Local Name Table
Name Type Status
---------------------------------------
QUARK <20> UNIQUE Registered
QUARK <00> UNIQUE Registered
JDHITSOLUTIONS <00> GROUP Registered
Wireless Network Connection:
Node IpAddress: [0.0.0.0] Scope Id: []
I'm going to focus on the registered name output and step through the process. You most likely want to turn this into a function you could reuse. So first, let's get the lines of interest:
PS C:\> $data=nbtstat /n | Select-String "<"
PS C:\> $data
QUARK <20> UNIQUE Registered
QUARK <00> UNIQUE Registered
JDHITSOLUTIONS <00> GROUP Registered
I used Select-String to filter and saved the results to $data. Next, I'll split each line into an array. The tricky part is that because I used Select-String, each line is a MatchInfo object, not a string. I'll need to trim off spaces:
PS C:\> $lines=$data | foreach { $_.Line.Trim()}
PS C:\> $lines
QUARK <20> UNIQUE Registered
QUARK <00> UNIQUE Registered
JDHITSOLUTIONS <00> GROUP Registered
Now, I'll split each line on a space into an array using the -Split operator and a regular expression pattern. Then I'll then take each array element and create a new object:
PS C:\> $lines | foreach { $temp=$_ -split "\s+"
>> New-Object -TypeName PSObject -Property @{
>> Name=$temp[0]
>> NbtCode=$temp[1]
>> Type=$temp[2]
>> Status=$temp[3]
>> }
>> }
Name NbtCode Type Status
---- ------- ---- ------
QUARK <20> UNIQUE Registered
QUARK <00> UNIQUE Registered
JDHITSOLUTIONS <00> GROUP Registered
Again, I did this interactively and you'll want to encapsulate in a function. But the end result is a custom object with the properties I defined. You should be able to use these concepts and techniques for just about any command-line tool and turn it into a PowerShell tool.
About the Author
Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.