VMware Cloud Community
vwmagic
Enthusiast
Enthusiast

To list Thick or Thin disk type in following script

I have following script which will list capacity, usage and free space for all VM disks. 

Can you please help me to add one more column, listing what disk type, is this VM disk Thick or Thin? 

 

Get-VM |
Select-Object Name,
@{N = 'Datastores'; E={(Get-Datastore -VM $_).Name -join '|'}},
@{N = "GuestCapacityGB"; E = { [math]::Round(($_.Guest.Disks | ForEach-Object { $_.Capacity / 1GB } | Measure-Object -Sum | Select-Object -ExpandProperty Sum), 2) } },
@{N = "GuestUsageGB"; E = { [math]::Round(($_.Guest.Disks | ForEach-Object { $_.CapacityGB - $_.FreeSpaceGB } | Measure-Object -Sum | Select-Object -ExpandProperty Sum), 1) } },
@{N = "FreeSpaceGB"; E = { [math]::Round(($_.Guest.Disks | ForEach-Object { $_.FreeSpace / 1GB } | Measure-Object -Sum | Select-Object -ExpandProperty Sum), 2) } },
@{N = "GuestUsage(%)"; E = {
$capacityGB = ($_.Guest.Disks | Measure-Object -Property CapacityGB -Sum).Sum
$freeGB = ($_.Guest.Disks | Measure-Object -Property FreeSpaceGB -Sum).Sum
[math]::Round((($capacityGB - $freeGB) / $capacityGB * 100), 2) }
} |
Sort-Object -Property "GuestUsage(%)" |
Export-Csv -Path c:\temp\report.csv -NoTypeInformation -UseCulture

Reply
0 Kudos
24 Replies
LucD
Leadership
Leadership

Yes, there was a typo in the Where-clause (Files instead of File)
It should have been

 

$vms = Get-View -ViewType VirtualMachine | where{$_.LayoutEx.File.Count -ne 0}


You'll have to re-run the previous snippet as well, it had the same typo

 

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vwmagic
Enthusiast
Enthusiast

I got the result and the file, but still got the following errors. That's alright, it is probably due to 0 HD on a few VMs. 
Would you please let me know questions I post earlier at 9:29am? I really appreciate your time. Those questions are important to me. Thank you!

 

 

 

Cannot index into a null array.
At line:38 char:1
+ $vmdkName = $vm.LayoutEx.File[$diskFiles[0]].Name
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray

Reply
0 Kudos
LucD
Leadership
Leadership

1. Yes, correct

2. Yes, these are Thick HD
I'm afraid you will have to go inside the Guest OS to check what is actually used.
That's where the Get-VMGuestDisk with the Harddisk parameter could be used.
But be aware that a HD can contain more than 1 partition inside the Guest OS.
It can even contain hidden partitions.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vwmagic
Enthusiast
Enthusiast

@{N='HDType';E={(Get-VMGuestDisk -VM $_ | Get-HardDisk).StorageFormat -join '|' }} |

 

 just by the line itself seems not working. Would you please complete the script?

 

Thank you!

Reply
0 Kudos
LucD
Leadership
Leadership

No, you have to insert that line with the other calculated properties on the Select-Object cmdlet.

Get-VM |
    Select-Object Name,
    @{N = 'Datastores'; E = { (Get-Datastore -VM $_).Name -join '|' } },
    @{N = 'HDType'; E = { (Get-VMGuestDisk -VM $_ | Get-HardDisk).StorageFormat -join '|' } },
    @{N = "GuestCapacityGB"; E = { [math]::Round(($_.Guest.Disks | ForEach-Object { $_.Capacity / 1GB } | Measure-Object -Sum | Select-Object -ExpandProperty Sum), 2) } },
    @{N = "GuestUsageGB"; E = { [math]::Round(($_.Guest.Disks | ForEach-Object { $_.CapacityGB - $_.FreeSpaceGB } | Measure-Object -Sum | Select-Object -ExpandProperty Sum), 1) } },
    @{N = "FreeSpaceGB"; E = { [math]::Round(($_.Guest.Disks | ForEach-Object { $_.FreeSpace / 1GB } | Measure-Object -Sum | Select-Object -ExpandProperty Sum), 2) } },
    @{N = "GuestUsage(%)"; E = {
            $capacityGB = ($_.Guest.Disks | Measure-Object -Property CapacityGB -Sum).Sum
            $freeGB = ($_.Guest.Disks | Measure-Object -Property FreeSpaceGB -Sum).Sum
            [math]::Round((($capacityGB - $freeGB) / $capacityGB * 100), 2) }
    } |
    Sort-Object -Property "GuestUsage(%)" |
    Export-Csv -Path c:\temp\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos