-
1. Re: Listing all snapshots per vm using get-view
LucD May 23, 2018 5:18 AM (in response to Marco_2_G)The fastest way to find VMs with snapshot is something like this
Get-View -ViewType VirtualMachine -Property Name,Snapshot -Filter @{Snapshot = ''} |select Name
-
2. Re: Listing all snapshots per vm using get-view
Marco_2_G May 23, 2018 5:39 AM (in response to LucD)Hi LucD
That gives me a list of all the vms that have snapshots.
I need to have a list of all the snapshots for each of the vms returned.
Regards,
Marco
-
3. Re: Listing all snapshots per vm using get-view
LucD May 23, 2018 6:20 AM (in response to Marco_2_G)Ok, that requires a few more lines of code.
Try like this
function Get-SnapChild{param(
[VMware.Vim.VirtualMachineSnapshotTree]$Snapshot
)
process{
$snapshot.Name
if($Snapshot.ChildSnapshotList.Count -gt 0){
$Snapshot.ChildSnapshotList | %{
Get-SnapChild -Snapshot $_
}
}
}
}
foreach($vm in Get-View -ViewType VirtualMachine -Property Name,Snapshot -Filter @{'Snapshot' = ''}){
$vm.Snapshot.RootSnapshotList | %{
Get-SnapChild -Snapshot $_ | Select @{N='VM';E={$vm.name}},@{N='Snapshot';E={$_}}
}
}
-
4. Re: Listing all snapshots per vm using get-view
Marco_2_G May 23, 2018 6:26 AM (in response to LucD)Thank you, this works. I have no idea why it works but that's more due to my being a novice when it comes to coding and powershell in particular .
I'll be staring at this code in total incomprehension some more now.
-
5. Re: Listing all snapshots per vm using get-view
LucD May 23, 2018 6:35 AM (in response to Marco_2_G)Lol!
To minimise the staring, some annotations:- To filter the VMs that have a snapshot, the Get-View Filter parameter checks if the Snapshot property is $null or not. This translates to checking for an empty string, which is Get-View/RegEx speak for -not $null
- On a VM there is the RootSnapShotList which points to the snapshot that make up the roots of all snapshot trees. For each root snapshot, we recursively traverse the snapshot tree via the Get-SnapChild function
- In the Get-SnapChild function we avoid the issue with a Foreach loop that runs once, even with an empty array, by testing for the number of entries in the ChildSnapshotList array
- when a child snapshot has children, the function calls itself again. This is the recursive part
-
6. Re: Listing all snapshots per vm using get-view
Marco_2_G May 23, 2018 6:38 AM (in response to LucD)I may now understand your code. If you have the time I'd live for you to verify this:
Am I correct in assuming that the process block is more good coding style than strictly necessary?
Anyway as far as I can tell, you take the rootsnapshotlist which has an attribute childsnapshotlist (THIS is exactly the piece of linking information I was unable to find so far) and if it has more than 0 entries you traverse through those snapshots by calling your function inside itself.
The function then returns everything it has found through the first line after process{ and all you have to do is clean the output up a bit through the select statement.
Am I understanding this somewhat correctly?
And thanks again for the help.
-
7. Re: Listing all snapshots per vm using get-view
Marco_2_G May 23, 2018 6:40 AM (in response to Marco_2_G)Oh, you were faster than I was
Thanks, I believe I've got it!
You're an invaluable source of info... but I'm sure I'm not telling you anything new.
-
8. Re: Listing all snapshots per vm using get-view
LucD May 23, 2018 6:44 AM (in response to Marco_2_G)Correct, the function could be done without the Process block
Strictly speaking Begin-Process-End blocks are when objects are passed over the pipeline to the function.The function just returns the names of all the snapshots it finds.
The Select is to combine that info with the VM name.
-
9. Re:Listing all snapshots per vm using get-view
Sudarshan_Bharti Oct 27, 2018 4:20 PM (in response to Marco_2_G)Get-View -ViewType VirtualMachine -Filter @{"snapshot" = ""} -Property Name | % {Get-VM -id $_.MoRef | Get-Snapshot} -
10. Re: Listing all snapshots per vm using get-view
ndnguyen May 20, 2019 8:46 AM (in response to LucD)How would i add Date created, created by and description? -
11. Re: Listing all snapshots per vm using get-view
LucD May 20, 2019 8:59 AM (in response to ndnguyen)Like this.
For the user that created the snapshot, you will have to search the events.See for example Re: snapshot who created it
function Get-SnapChild{
param(
[VMware.Vim.VirtualMachineSnapshotTree]$Snapshot
)
process
{
$snapshot | Select Name, Description, CreateTime
if ($Snapshot.ChildSnapshotList.Count -gt 0)
{
$Snapshot.ChildSnapshotList | % {
Get-SnapChild -Snapshot $_
}
}
}
}
foreach ($vm in Get-View -ViewType VirtualMachine -Property Name, Snapshot -Filter @{'Snapshot' = '' })
{
$vm.Snapshot.RootSnapshotList | % {
Get-SnapChild -Snapshot $_ |
Select @{N = 'VM'; E = { $vm.name } },
@{N = 'Snapshot'; E = { $_.Name } },
@{N = 'Description'; E = { $_.Description } },
@{N = 'CReated'; E = { $_.CreateTime } }
}
}
-
12. Re: Listing all snapshots per vm using get-view
ndnguyen May 20, 2019 9:23 AM (in response to LucD)Thank you very much for the quick responce. Much appriciated.