How to use hash table with Powercli

Hash table is a data structure where we can store our data.Comparing to the data arrays in powercli/powershell , the main difference is with hash table we can easily handle multiple type of data such as Integer,String,etc..

Some times we need to generate reports using some of the PowerShell commands(export-csv,out-file,add-content).With below example my requirement is to generate vm report which include VMname,Datastore name,Network port group and vmawre tools status.But to gather all the information we have to use multiple get commands and then its difficult to consolidate this data.Hash table was helped me on this.I can gather values from multiple commands and assign to hash table.I have highlghted hash table related commands in my script.

import-module vmware.vimautomation.ha

connect-viserver vcsa.corp.local
$vms=get-vm

$result=@()

foreach ($vm in $vms)

    {
     $vmname=$vm.name
     $dstore=get-vm $vm.name|get-datastore
     $nicport=(get-vm $vm.name).extensiondata.network.value
     $vmtools=(get-vm $vm.name).extensiondata.guest.toolsstatus
     $properties=@{
     name=$vmname
     datastore=$dstore
     network-$nicport
     vmwaretools=$vmtools

     }

$result+=new-object psobject -property $properties
$result|select name,datastore,network,vmwaretools|export-csv d:\mycsv.csv -append -notypeinformation

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: