DZ1
Hot Shot
Hot Shot

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

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.

0 Kudos
4 Replies
shaggy_041086
Enthusiast
Enthusiast

I use this for snapshots -

$Report = @()

Get-VM | get-snapshot | %{

        $Snap = "" | Select VM,Name,Created,Description,Size

        $Snap.VM = $_.vm.name

        $Snap.Name = $_.name

        $Snap.Created = $_.created

        $Snap.Description = $_.description

        $Snap.Size = [Math]::Round(($_.sizemb/1024),2)

        $Report += $Snap

}

And then, would use Export-Csv or use Send-MailMessage on $Report.

What you can do to list them only once is, use "-Unique".

For eg:-

$Report | Sort VM -Unique

EKardinal
Enthusiast
Enthusiast

Hi,

you can use the ExtensionData of $VM as well.

Get-Cluster "Cluster" | Get-VM | Where-Object { $_.ExtensionData.Snapshot.RootSnapshotList.Count -gt 0 } | Select-Object -Property `
    Name,
    @{N
="Snapshot", E=@{$_.ExtensionData.Snapshot.RootSnapshotList[0].Name}},
    @{N
="Snapshot Description", E=@{$_.ExtensionData.Snapshot.RootSnapshotList[0].Description}}


Lists all VMs with one or more snapshots and their first snapshot name and description.

Regards

Emanuel

DZ1
Hot Shot
Hot Shot

Thanks for the help, I thought there may have been a quicker way to pull the info, and sort it the way that I want.  I think I've been spoiled by how Powershell can have some powerful one-liners, I forget that everything can't always be short.  Thanks for the help. 

0 Kudos
DZ1
Hot Shot
Hot Shot

Sorry, it looks like only a reply to one of you, but thank you both.

0 Kudos