Script to count the space occupied by the virtual machines on Hyper-V
Recently, I found a discrepancy of data about occupied by the virtual machines disk space displayed in the console of System Center Virtual Machine Manager with data obtained using Get-SCVirtualMachine and decided to find out what it is. Information about occupied disk space I received with widespread cmdlet Get-SCVirtualMachine. For this I used a loop that went through all the virtual machines and accumulate the size of their disks in a variable:
the
Similar structures are replicated many times on the forums and in all blogs, therefore, doubt as to their correctness I did not have. After the discovery of the discrepancies, I added a loop debug output: $vm.VirtualHardDisks.Size for each VM and its name. The output of the modified script is shown that some WM volume was equal to 0. I was sure that $vm.VirtualHardDisks.Size shows the amount, but it was not so — the team returned two numbers (the two VHDs). Obviously, powershell is not able to divide the array to 1TB, and therefore gave 0
the
Don't even know in this case, to say thank you flexibility PowerShell for not ERROR, or to tell him, antipassive for such a disservice. Too many mistakes to forgive.
Then I decided to move all of the disks in a loop and changed the original design slightly more complex, but more correct:
the
Great! Now the VM with two or more virtual disk was read correctly BUT! The sum of all virtualcom still do not coincide, though close to the readings on the console.
Drove your script this way and that. Looked posravnivali and saw that on some VMS the amount matches to the penny, and on some there is a significant discrepancy. Climbed in properties, and there... snapshots! They are something we have not considered.
Then again, Google is, again, questionable scripts until very exotic metodos search files by mask.
I had to write myself. At first I tried to select all disks by using Get-SCVirtualHardDisk without resorting to the brute force of the virtual machines, however, I was able to list only the disks that I received with $vm.VirtualHardDisks. So I had to dig a little deeper. The virtual machine has the property VMCheckpoints, which lists all the control points. From this property it is possible to actually VMSnapshot (I did not understand what the Snapshot differs from a Checkpoint in terms of SCVMM). This object also has a property VirtualDiskDrives, but he somehow has no properties Size, so I had to insert a spike: VirtualDiskDrives we take a property VirtualHardDiskID with this ID do Get-SCVirtualHardDisk:
the
After that, a small discrepancy still remained, and a colleague told me to count also service files .bin and .vsv, which store the service information like for example the dump of RAM. Once again looking through the list of available settings for the virtual machine, I stumbled upon... $vm.TotalSize. Who would have thought that all the work I have done and only need the sum of this setting for all virtual machines!
the
It was so ridiculous that I decided not to remove from the script the data of snapshots obtained in this work.
Filled here gallery.technet.microsoft.com/scriptcenter/Script-to-calculate-the-36a9314f
Article based on information from habrahabr.ru
the
$totalhdd = 0
$vms = Get-SCVirtualMachine #| where {$_.Name-eq "VM Name"} # For debugging
foreach ($vm in $vms) {
$totalhdd += ($vm.VirtualHardDisks.Size -as [double]) / 1TB}
}
Similar structures are replicated many times on the forums and in all blogs, therefore, doubt as to their correctness I did not have. After the discovery of the discrepancies, I added a loop debug output: $vm.VirtualHardDisks.Size for each VM and its name. The output of the modified script is shown that some WM volume was equal to 0. I was sure that $vm.VirtualHardDisks.Size shows the amount, but it was not so — the team returned two numbers (the two VHDs). Obviously, powershell is not able to divide the array to 1TB, and therefore gave 0
the
PS D:\Scripts\IT_git> $vm.VirtualHardDisks.Size
3547332608
4194304
PS D:\Scripts\IT_git> ($vm.VirtualHardDisks.Size -as [double]) / 1TB
0
Don't even know in this case, to say thank you flexibility PowerShell for not ERROR, or to tell him, antipassive for such a disservice. Too many mistakes to forgive.
Then I decided to move all of the disks in a loop and changed the original design slightly more complex, but more correct:
the
foreach ($vm in $vms) {
$vhds = $vm.VirtualHardDisks
foreach ($vhd in $vhds) { $totalhdd += ($vhd.Size -as [double]) / 1TB}
}
Great! Now the VM with two or more virtual disk was read correctly BUT! The sum of all virtualcom still do not coincide, though close to the readings on the console.
Drove your script this way and that. Looked posravnivali and saw that on some VMS the amount matches to the penny, and on some there is a significant discrepancy. Climbed in properties, and there... snapshots! They are something we have not considered.
Then again, Google is, again, questionable scripts until very exotic metodos search files by mask.
Hidden text
the Method of finding *avhdx, at the same time, radically wrong, as in the avhd are written to the current data, and the snapshot is currently in vhdx. However, him and I have met other authors
I had to write myself. At first I tried to select all disks by using Get-SCVirtualHardDisk without resorting to the brute force of the virtual machines, however, I was able to list only the disks that I received with $vm.VirtualHardDisks. So I had to dig a little deeper. The virtual machine has the property VMCheckpoints, which lists all the control points. From this property it is possible to actually VMSnapshot (I did not understand what the Snapshot differs from a Checkpoint in terms of SCVMM). This object also has a property VirtualDiskDrives, but he somehow has no properties Size, so I had to insert a spike: VirtualDiskDrives we take a property VirtualHardDiskID with this ID do Get-SCVirtualHardDisk:
the
...
# Snapshots search
foreach ($vmc in $vm.VMCheckpoints) {
foreach ($vdd in $vmc.VirtualDiskDrives) {
$vhd = Get-SCVirtualHardDisk -ID $vdd.VirtualHardDiskID
$snapshotshdd += ($vhd.Size -as [double]) / 1TB
}
}
...
After that, a small discrepancy still remained, and a colleague told me to count also service files .bin and .vsv, which store the service information like for example the dump of RAM. Once again looking through the list of available settings for the virtual machine, I stumbled upon... $vm.TotalSize. Who would have thought that all the work I have done and only need the sum of this setting for all virtual machines!
the
$totalsize = 0
$vms = Get-SCVirtualMachine #| where {$_.Name-eq "VM Name"} # For debugging
foreach ($vm in $vms) {
$totalsize += ($vm.TotalSize -as [double]) / 1TB
}
It was so ridiculous that I decided not to remove from the script the data of snapshots obtained in this work.
Filled here gallery.technet.microsoft.com/scriptcenter/Script-to-calculate-the-36a9314f
Mathematics joke