VMware Cloud Community
piercj2
Enthusiast
Enthusiast
Jump to solution

Working with Hash Tables

HI Guys,

I've a script that takes a list of VM's and collects various bits of information on them

  • Name, Host Name, Num CPU, Num Disks, IP Info, etc...

The script works very well but, I still need to load the data into Excel and manipulate it manually before the data can be used with other scripts. I'm wondering if there's a way to eliminate this manual work ?

Specifically, for each VM, the script determines the number of hard disks and returns the following for each disk

  1. HDDx Name
  2. HDDx Datastore
  3. HDDx Path (folder and name of .vmdk within the Datastore)

In the Hash table, I can be left with dozens of rows for all the VM's and each VM could have multiple entries for each of the above 3 items.

E.g. HDD1 DataStore, HDD2 DataStore, HDD3 Datastore ... ... ...

What I want to do is take all the "HDDx DataStore"  entries from the hash table and return just the unique entries so that I can then go off and get specific info on these specific Datastores, number of VM's, names of VM's etc...

I've tried using Wild Cards, Where Statements, etc... I can't get it to work.

Attached is the fill script that I 'm using.

Below is the closest that I've come to getting this to work but, it's still not adding all the DataStore information in the second hash table $dsReport

Within the Attached script, I've $dsReport lines highlighted

$report=@()
$dsReport=@()
...
...
$hdd = 1
$Computer | Get-HardDisk | foreach {
     $GeneralProp.Add("HDD$($hdd) Name",$_.Name)
     $GeneralProp.Add("HDD$($hdd) Datastore",$_.FileName.split(" ")[0].TrimStart("[").TrimEnd("]"))
     $GeneralProp.Add("HDD$($hdd) Path",$_.FileName.split(" ")[1].TrimStart("[").TrimEnd("]"))
     $GeneralProp.Add("HDD$($hdd) Capacity",$_.CapacityGb)
     $dsProp=@{Name = $_.FileName.split(" ")[0].TrimStart("[").TrimEnd("]")}
     $hdd++
}

...

...

$dsReport | select Name -Unique

Any/All help is appreciated

Thanks


Tags (1)
0 Kudos
1 Solution

Accepted Solutions
zik
Enthusiast
Enthusiast
Jump to solution

Something like this?

get-vm | foreach {

  $disks = @()

  $_ | get-harddisk | foreach {

    $dsname = $_.Filename.split(" ")[0].TrimStart("[").TrimEnd("]")

    $disks += $_ | Add-Member -MemberType NoteProperty -Name DatastoreName -Value $dsname -PassThru

  }

  $disks | Group-Object -Property DatastoreName

}

View solution in original post

0 Kudos
2 Replies
zik
Enthusiast
Enthusiast
Jump to solution

Something like this?

get-vm | foreach {

  $disks = @()

  $_ | get-harddisk | foreach {

    $dsname = $_.Filename.split(" ")[0].TrimStart("[").TrimEnd("]")

    $disks += $_ | Add-Member -MemberType NoteProperty -Name DatastoreName -Value $dsname -PassThru

  }

  $disks | Group-Object -Property DatastoreName

}

0 Kudos
piercj2
Enthusiast
Enthusiast
Jump to solution

Thanks Zik, that worked perfectly.

I was missing two items from my efforts

  1. I was updating the $disks Hash table outside of the foreach loop (so I was not adding all the info to it)
  2. I wasn't piping to Group-Property (an ingenious approach by you)

Thanks again


0 Kudos