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, , , ,

Wayne’s World of IT

One of my more popular blog entries is the one I did about CMD one-liners. These are little tidbits of commands that can get a lot done. The list came from a reader and I was happy to share. Wayne has now started a blog Wayne’s World of IT which will carry on this theme of command line automation. It’s just getting started but I hope you’ll check it out. Wayne assures me he has a growing list of one-liners to publish.

Technorati Tags: , , , DSquery, , ,

Updated Group Reporter

A few of my recent Mr. Roboto columns have been about group auditing. I first published an HTA that would report on group membership last December. Astute reader Matt V. found a bug with the way nested group memberships were being reported. Or in this case, not being reported. It turns out that if a group was a member of one other group, that membership was not reported. But if the group was nested in 2 or more groups then you’d see all the nested groups. After a little debugging I found my goof. I used ADSI to get a reference to the specified group:

strDN would have a value like CN=Sales,OU=Groups,DC=Mycompany,DC=local. My mistake was in assuming that the MemberOf property would always be a collection so I was using a ForEach construct. But it is only a collection if there is more than one group. So I modified the code:

Because I used the same code in a followup article where I released a command line version of the tool, I had to modify that tool as well. The above code snippet is actually from the WSF version.

Anyway, both versions have now been updated and are available for download from the Mr. Roboto section of my script library. Thank you Matt for keeping me on my toes.

Technorati Tags: , ADSI, , ,