Parsing HP CLI Output

I've been working on an environment audit for one of my customers (expect to see some of those scripts popping up in the not-too-distant future).  In addition to auditing ESXi host configurations, I've been looking at their HP C7000 Chassis configurations and comparing them against enterprise standards.  Since there are a lot of chassis in this environment, I've been leveraging scripts to collect data via plink.

It's not always easy to parse data from an SSH session into PowerShell objects, but HP gave us a great tool in their CLI (way better than adjusting column size, which is what I was looking for when I came across this).  It's already well documented, but it saved me so much time that I figured that it was worth mentioning anyway... the HP CLI commands support this great option: -output=script2.  There's actually a couple of "script#" options, but I'm particularly fond of #2.  So, what's it do?  Well, it turns this style of human friendly output:

->show network
=============================================================================
Name        Type  Status  Shared      VLAN   Native    Private   Preferred
                          Uplink Set  ID     VLAN                Speed
=============================================================================
INTERNAL_V  Enet  OK      -- --       -- --  -- --     Disabled  Auto
C1
-----------------------------------------------------------------------------
INTERNAL_V  Enet  OK      -- --       -- --  -- --     Disabled  Auto
C2


... into this style of actually usable output:

->show network -output=script2
Name;Type;Status;Shared Uplink Set;VLAN ID;Native VLAN;Private;Preferred Speed
INTERNAL_VC1;Enet;OK;-- --;-- --;-- --;Disabled;Auto
INTERNAL_VC2;Enet;OK;-- --;-- --;-- --;Disabled;Auto

That's right!  It formats the output of the commands out of the "graphical" table format (with those terrible line breaks) into a semicolon delimited list!  From there, it's trivial to parse it into an array of PowerShell objects with the following command (assuming that you've stored your HP CLI output in the $CLIOutput variable):

$CLIOutput | convertfrom-csv -delimiter ";"

And there you go!  No messy line counting or searching for indexes based on strings that you expect to be there, just nice, clean PowerShell objects!  Bear in mind that some of the properties on these objects will have spaces in the names, so to reference them you may need to use quotes (such as $CLIOutput."Preferred Speed").

Comments

Popular posts from this blog

Orphaned VMDK Files

Migrating from one vCenter to Another, Improved

Copying VM Folders and Permissions from One vCenter to Another