Tag Archives: CLI

Convert a String to a PowerShell Property Name

talkbubbleOver the last few years I’ve written and presented a bit on the idea of turning command line tools into PowerShell tools. We have a lot of great CLI based tools that are still worth using. What I’ve done is come up with tools and techniques for turning their output into an object that can be used in the PowerShell pipeline. Often all I need to do is parse and clean up command line output. But one thing that has always nagged me is what to use for property names.

For example, I can pretty easily turn output from the ARP.EXE command into objects. Here’s what I start with.
arp-a

What I want to do is take the column headings and turn them into properties. The problem is I don’t like spaces in property names. Plus, I would need to know in advance the command line heading so I could use something like a custom hashtable to rename. I was after something a bit more convenient and something that would work with almost any command line output, although I think tabular output works best. Thus I came up with a short function I call Convert-StringProperty.

Here’s how it works. I’ll take the raw ARP output and skip the first couple of lines.

The $raw variable has the data I want to turn into objects.

The first line contains the property names but I want them without the spaces. As a separate step, outside of the function, I need to split the first line. I’m going to do that with a regular expression pattern that matches 2 or more white spaces.

I can take the first line, item [0], remove leading and trailing spaces and split it. This will give me three strings: Internet Address, Physical Address, and Type. Each of these is then piped to my Convert-StringProperty.

The function will look at each string and split it again based on a delimiter, which by default is a space. But you can specify something different if you run into CLI names like INTERNET_ADDRESS. Each word is then processed with a capital first letter. The end result is camel case so “Internet Address” becomes “InternetAddress”.

Once I know what my property names will be, I can continue parsing the command line output and create a custom object.

You still need to come up with code to process your command line tool, but you can use this function to define proper PowerShell properties. Here’s one more example.

This takes command output like this:

And turns it into PowerShell output like this:

In another article I’ll share with you another tool that takes advantage of this function. Enjoy.

Waynes World of Tips

I’ve blogged in the past about Wayne Martin and his outstanding list of command line tips. These are one line commands, some complex some simple, that you can use to accomplish a wide range of task. The overall number of tips is to 425 and Wayne recently reorganized them into 7 categories to make it easier for people to digest. There’s very little scripting with any of these commands. Most use native or freely available command line tools. But because they are executed from a command line you could incorporate them into a script. I encourage you to check them out.

The single list:

http://waynes-world-it.blogspot.com/2008/09/useful-command-lines.html

The same commands split into categories:

http://waynes-world-it.blogspot.com/2008/09/useful-active-directory-command-line.html

http://waynes-world-it.blogspot.com/2008/09/useful-dns-dhcp-and-wins-command-line.html

http://waynes-world-it.blogspot.com/2008/09/useful-general-command-line-operations.html

http://waynes-world-it.blogspot.com/2008/09/useful-vmware-esx-and-vc-command-line.html

http://waynes-world-it.blogspot.com/2008/09/useful-windows-mscs-cluster-command.html

http://waynes-world-it.blogspot.com/2008/09/useful-windows-printer-command-line.html

http://waynes-world-it.blogspot.com/2008/09/useful-ntfs-and-security-command-line.html

More CLI One-Liners

In the past I’ve posted a collection of command one-liners that get a ton of work done with (relatively) minimal effort. Many of these have come from Wayne Martin. I’ve been behind in my blogging while book writing, but I wanted to make sure you checked out his latest list at:

http://waynes-world-it.blogspot.com/2008/04/useful-command-lines.html

Technorati Tags: , , FSMO, , DSquery, , , ,