Get the size to a snapshot via Powershell in Hyper-V

2.1k Views Asked by At

Good Evening, I am developing a C# too, that is supposed to help users to control VMs on a server. Now I have created the functions to create and erase Snapshots of these VMs and want to create a function to list all Snapshots of a VM including a view selected Snapshot parameter. With:

Invoke-Command -ComputerName ServerName -ScriptBlock { Get-VMSnapshot -VMName VMName |Select-Object}

I am able to get nearly all information that are requested but the total size of the Snapshot itself.

However I could get the size with this command:

Invoke-Command -ComputerName ServerName -ScriptBlock {Get-VM -VMName VMName | gci -r -Filter *.avhd*| select @{Name="MB";Expression={$_.Length / 1Mb}}, *}

Not to get the two results together I compared the creation time of these two with a tolerance of up to 10 seconds and took the closest one. (Primitive I know), but now it becomes even more complicated.

If I now use a Snapshot to set the VM to this state, the time of the File creation changes and so my previous method stops working completely.

Why I want to know is, is there a better way to compare these two results or even better, to get the Snapshot size without such primitive comparisons?

Thanks in advance for the help.

1

There are 1 best solutions below

5
On BEST ANSWER

Below one-liners should suffice your need.

1)

Get-VM | Sort Name | Get-Snapshot | Where { $_.Name.Length -gt 0 } | Select VM,Name,Created,SizeMB

2)

Get-VM | Format-Table Name, @{Label="NumSnapshots";Expression={(Get-Snapshot -VM $_ | Measure-Object).Count}}, @{Label="TotalSnapShotSizeMB";Expression={(Get-Snapshot -VM $_ | Measure-Object -Sum SizeMB).Sum}}

Hope it helps.