Thursday, December 15, 2011

Using export-clixml to recreate vSphere (PoSH) objects


I had been looking for a way to export objects from PoSH so I could work with them offline. I imagined a function 'export-object' that would just export out a PoSH object to a file, and then use 'import-object' to  recreate the object from file.  I often have to write reporting scripts for my clients, and it is handy to be able to have a 'get-view' output to check your script against real data. Sure it is easy enough to make sure you store what you need in variables in your session / IDE but what if you reboot, crash etc...


I first found export-csv which would take an object and export it out as csv. You can then import it back in with import-csv. Almost what I needed, but sadly it exports the first level of the objects...



First, lets create a variable we want to export for later use:


PowerCLI D:\> $AllHosts = Get-View -ViewType HostSystem
PowerCLI D:\> $AllHosts.Count
2
PowerCLI D:\> $AllHosts[0].Runtime
  
ConnectionState     : connected
PowerState          : poweredOn
StandbyMode         : none
InMaintenanceMode   : False
BootTime            : 21/11/2011 11:32:31 PM
HealthSystemRuntime : VMware.Vim.HealthSystemRuntime
DasHostState        : VMware.Vim.ClusterDasFdmHostState
TpmPcrValues        :
DynamicType         :
DynamicProperty     :


Simple enough? Now lets use export-csv / import-csv to export it to a new object.


PowerCLI D:\>
PowerCLI D:\> $AllHosts | Export-Csv -Path D:\tmp\AllHosts.csv
PowerCLI D:\> $AllHostsRebuild1 =  ConvertFrom-Csv -InputObject (get-content D:\tmp\AllHosts.csv)
PowerCLI D:\> $AllHostsRebuild1.Count
2


Two things in there, looks good.....


PowerCLI D:\> $AllHostsRebuild1[0].Runtime
VMware.Vim.HostRuntimeInfo
PowerCLI D:\>

Hang on! It is just giving me the TypeName, and not the content. Doh'!
Export-csv will only export 1 'level' of an object, which wont give me what I want.

Lets have a look at what export-clixml / import-clixml can do.


PowerCLI D:\> $AllHosts | Export-Clixml -Depth 10 -Path D:\tmp\AllHosts.clixml
PowerCLI D:\> $AllHostsRebuild2 = Import-Clixml -Path D:\tmp\AllHosts.clixml
PowerCLI D:\> $AllHostsRebuild2.Count
2
PowerCLI D:\> $AllHostsRebuild2[0].Runtime
  
ConnectionState     : connected
PowerState          : poweredOn
StandbyMode         : none
InMaintenanceMode   : False
BootTime            : 21/11/2011 11:32:31 PM
HealthSystemRuntime : VMware.Vim.HealthSystemRuntime
DasHostState        : VMware.Vim.ClusterDasFdmHostState
TpmPcrValues        :
DynamicType         :
DynamicProperty     :

PowerCLI D:\>


This looks a little more like it. Notice that export-clixml has the -depth  param? The default is 2, this will drive how far down it will export. 10 should be sufficient for most vSphere objects I think.

So using export-clixml you can have an offline copy of your vSphere inventory (or anything really)!

This will be a lifesaver for clients who don’t give you remote access (I know it is for me)

Enjoy!



4 comments:

Jeffery Hicks said...

I'd really like to see you use the xml file and rebuild something you "accidentally" deleted.

winter said...

as in use the export-clixml output as a poor mans backup?

winter said...

yay! i made script of the week!

http://www.vmware.com/vmtn/planet/vmware/

Marco said...

I'd like you visit this page for a new powercli project for Disaster & Recovery.

http://vmgmt.wordpress.com/free-dr-tool/

We need testers for the beta version of the Virtual Appliance

Labels

blogs I read

Authors

My photo

Enjoys spending time in layer 6 & 7. Passionate about delivering quality education.
Based in Sydney.

certz