4 Replies Latest reply on Sep 9, 2013 11:37 AM by DZ1

    Is there a better way to choose the first name in a list?

    DZ1 Hot Shot

      I want to retrieve some VMs and list their snapshots and the descriptions and names of those snapshots, as soon as I started to type this question, another solution came up, but I'll go over the first. 

       

      Initially, I tried this:

       

      Get-Cluster "Cluster" | Get-VM | foreach {

      if (($_ | Get-Snapshot).count -gt 1) {

      (($_ | Get-Snapshot).vm | select -first 1),

      ($_ | Get-Snapshot).name,

      ($_ | Get-Snapshot).description

       

      Basically, I wanted to make sure that if a VM had multiple snapshots, the name of the VM was only listed one time, so that's why I used Select-Object -first 1, then I thought of this:

       

      Get-Cluster "Cluster" | Get-VM | select `

      Name,

      @{N="Snapshots"; E={($_ | Get-Snapshot).name}},

      @{N="Snapshots description"; E={($_ | Get-Snapshot).description}} | fl

       

      That works as well, and it's somewhat easier, but the results get listed like an array if the VM has multiple snapshots {Snap1, Snap2, Snap3...} 

       

      I'm familiar with using -ExpandProperty thanks to this board, but since I want multiple properties, I don't think I can use this, well, when I tried it, I received an error.  I guess I could create a new object, but I thought there was a quick way to get this info. 

       

      To sum up, this procedure isn't really related to snapshots, it's really just finding the best method for listing the name once when retrieving values where the name will appear multiple times.  Thanks for any input.