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