Automation

 View Only
  • 1.  Get VM Name from a snapshot object

    Posted Mar 19, 2010 11:46 AM

    This must be a esy one, but I don't get the solution.

    I would get all VM-Names with at least one snapshot. And because the get-snapshot cmdlet doesn't work well in 4.0u1 I don't want to use it.

    I get all snapshots with:

     $rootsnap = Get-View -ViewType VirtualMachine |%{$_.Snapshot.RootSnapshotList} 

    Now I would get the VM-Name. I tried it with:

      
    foreach ($snap in $rootsnap){
     $vmid = $snap.vm.type + "-" + $snap.vm.value
     get-vm -id $vmid
    }
    

    this doesnt works, because sometimes the $snap is empty.



  • 2.  RE: Get VM Name from a snapshot object
    Best Answer

    Posted Mar 19, 2010 11:56 AM

    You can solve the guests with no snapshots with a where cmdlet.

    filter Get-SnapshotFast{
    	$_.Name
    	if($_.ChildSnapshotList){
    		$_.ChildSnapShotList | Get-SnapshotFast
    	}
    }
    
    Get-View -ViewType VirtualMachine | where {$_.Snapshot.RootSnapshotList} | %{
    	Write-Host "VM name" $_.Name
    	Write-Host "Snapshots`n" ($_.Snapshot.RootSnapshotList | Get-SnapshotFast)
    }
    

    Note that I use a recursive function to run through the snapshot tree.

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 3.  RE: Get VM Name from a snapshot object

    Posted Mar 19, 2010 01:52 PM

    Thank you, this helps me a lot to understand. Now my final version is:

    $report = @()
    
    filter Get-Desc{
     $_.Name
     if($_.ChildSnapshotList){
      $_.ChildSnapShotList | Get-Desc
     }
    }
    
    filter Get-Date{
     $_.CreateTime
     if($_.ChildSnapshotList){
      $_.ChildSnapShotList | Get-Date
     }
    }
    
    Get-View -ViewType VirtualMachine | where {$_.Snapshot.RootSnapshotList} | %{
     $row = "" | select VMname, Desc, Created
     $row.VMname = $_.Name
     $row.Desc = $_.Snapshot.RootSnapshotList | Get-Desc
     $row.Created = $_.Snapshot.RootSnapshotList | Get-Date
     $report += $row
    }
    
    $report | ft -auto | out-string
    

    Do I've to make two filter function or can I combine this?



  • 4.  RE: Get VM Name from a snapshot object

    Posted Mar 19, 2010 02:07 PM

    You can combine this into one recursive function.

    The new Get-SnapshotFast filter will return all VirtualMachineSnapshotTree objects for a specific VM.

    When you use a 2nd loop pver all these objects, you have the report you want.

    $report = @()
    
    filter Get-SnapshotFast{
    	$_
    	if($_.ChildSnapshotList){
    		$_.ChildSnapShotList | Get-SnapshotFast
    	}
    }
    
    Get-View -ViewType VirtualMachine | where {$_.Snapshot.RootSnapshotList} | %{
    	$vm = $_
    	$_.Snapshot.RootSnapshotList | Get-SnapshotFast | %{
    		$row = "" | select VMname, Desc, Created 
    		$row.VMname = $vm.Name 
    		$row.Desc = $_.Description
    		$row.Created = $_.CreateTime
    		$report += $row
    	}
    }
    
    $report | ft -auto | out-string
    

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 5.  RE: Get VM Name from a snapshot object

    Posted Mar 19, 2010 02:37 PM

    That's nice, now I'm happy.

    Thank you!