Looking for a something in a PowerShell object using a text file

I feel like a good chunk of my time writing scripts is not spent writing code, my scripts/one-liners are usually pretty simple, but spent looking through an object for the property I want to report on or change.  At times I’ve spent 15-20 minutes doing Internet searches and long series of:

$> $x = get-vm somename
$> $x | fl *
$> $x | select -expand extensiondata
$> $x | select -expand extensiondata | select -expand guest
$> $x | select -expand extensiondata | select -expand config

and so on.

After doing that for a couple of years I woke up one morning and felt like an idiot.  It occurred to me that all I needed to do is use Export-Clixml to dump the object into an XML file and then search it using your tool of choice.

$> Get-View -VIObject (Get-VMHost -Name esxhostname) | Export-Clixml -Path vmhost_object.xml

Then open vmhost_object.xml with Notepad++, a web browser, search with Select-String, whatever works for you.  If you open the file with a browser then you can collapse/expand nodes and in general just browse around pretty easily.

That’s a whole lot less typing and so much faster.

Advertisements