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
Provided you are complying with the requirements for the Get-VMGuestDisk cmdlet, you could do
@{N='HDType';E={(Get-VMGuestDisk -VM $_ | Get-HardDisk).StorageFormat -join '|' }} |
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Could you please add the new line into the script above?
Sure, but it's just a calculated property like the others you have in there.
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) }
},
@{N = 'HDType'; E = { (Get-VMGuestDisk -VM $_ | Get-HardDisk).StorageFormat -join '|' }} |
Sort-Object -Property "GuestUsage(%)" |
Export-Csv -Path c:\temp\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Would like to ask you for one more help.
Based on the script, can you please separate VM disks from each VM's, list all individual VM disks including capacity, usage and free space along with disk format on each one of VM? We have just upgraded vSphere to version 7.
Thank you for all your help.
Just to be sure, that will be the view from the vSphere side, i.e. Harddisks.
Not partitions inside the Guest OS, as your original script is doing
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I honestly don't know the differences between these two. I thought they are same.
Anyway, the purpose of the script is to list how much each VM disks has been really used by data. In Thick format, if the capacity is 500GB, for instance, and the real usage only 10GB. So, I have about 450GB over provisioned, in this case, I wanted to cut the provisioned size down. But, I would like to list all VM disks, Thick or Thin
That sounds like you want something like the report I posted about in yadr – A vdisk reporter
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I tried your script, but got the following errors. Is this because API 4.0 missed? Do I need to add extra line in your script in order to invoke API 4.0?
I have gone to Developer Center and select the API Explorer tab, from there, there are quite a lot of things listed, how do I check if API 4.0 is installed or not?
Cannot index into a null array.
At line:38 char:1
+ $vmdkName = $vm.LayoutEx.File[$diskFiles[0]].Name
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
I assume you are running this on vSphere 6.5 or beyond?
Then API 4.0 should be no problem.
Is this returning any VMs?
Get-View -ViewType VirtualMachine
Are there VMs without Harddisk?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
We are running vSphere 7.0, one before the latest version.
Get-View -ViewType VirtualMachine
the command above works fine, and I got a long list of outputs. please see below as just one of a lots.
How would I know if I have a vm without harddisks? There are more than 2000 VM's here.
...
Capability : VMware.Vim.VirtualMachineCapability
Config : VMware.Vim.VirtualMachineConfigInfo
Layout : VMware.Vim.VirtualMachineFileLayout
LayoutEx : VMware.Vim.VirtualMachineFileLayoutEx
Storage : VMware.Vim.VirtualMachineStorageInfo
EnvironmentBrowser : EnvironmentBrowser-envbrowser-2083
ResourcePool : VirtualApp-resgroup-v2646
ParentVApp : VirtualApp-resgroup-v2646
ResourceConfig : VMware.Vim.ResourceConfigSpec
Runtime : VMware.Vim.VirtualMachineRuntimeInfo
Guest : VMware.Vim.GuestInfo
Summary : VMware.Vim.VirtualMachineSummary
Datastore : {Datastore-datastore-180}
Network : {DistributedVirtualPortgroup-dvportgroup-35}
Snapshot :
RootSnapshot : {}
GuestHeartbeatStatus : gray
LinkedView :
Parent :
CustomValue : {101}
OverallStatus : green
ConfigStatus : green
ConfigIssue : {}
EffectiveRole : {-1}
Permission : {}
Name : vmname.abc.com
DisabledMethod : {MakePrimaryVM_Task, TerminateFaultTolerantVM_Task, ResetVM_Task, UnmountToolsInstaller...}
RecentTask : {}
DeclaredAlarmState : {alarm-10.vm-2083, alarm-1002.vm-2083, alarm-11.vm-2083, alarm-1703.vm-2083...}
TriggeredAlarmState : {}
AlarmActionsEnabled : True
Tag : {}
Value : {101}
AvailableField : {Application/System, Availability, Compute, FA.GosAgent...}
MoRef : VirtualMachine-vm-2083
Client : VMware.Vim.VimClientImpl
....
Thank you!
Can you check if there are any VM with no Harddisks or even no Files (vCenter ghost entries).
Check if this returns anything
Get-View -ViewType VirtualMachine |
Where{$_.LayoutEx.File.Count -eq 0 -or
$_.Config.Hardware.Device.where{$_.DeviceInfo.Label -like "Hard disk *"}.Count -eq 0} |
Select Name,
@{N='Files';E={$_.LayoutEx.File.Count}},
@{N='HD';E={$_.Config.Hardware.Device.where{$_.DeviceInfo.Label -like "Hard disk *"}.Count}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Returned following errors:
At line:2 char:34
+ Where{$_.LayoutEx.Files.Count -eq -or
+ ~
You must provide a value expression following the '-eq' operator.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedValueExpression
There was a 0 missing.
I corrected the code above
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, there are 3 VM's with 0 on HD.
Returned 0's on FILES for all VM's.
Should we do anything to your original script then?
You could change line 13 to
$vms = Get-View -ViewType VirtualMachine | where{$_.LayoutEx.Files.Count -ne 0}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Great, Thanks!
I do have a few more questions, if you can please help me out:
1. For Thin disks, "AllocatedMB" would be really physical space occupied in Datastore. Correct?
2. For those with FALSE on both Thin and RDM columns, could they then considered to be Thick type of disks?
If yes, The "AllocataedMB" and CapacityMB are all the same. Is there anyway I can tell how much space really being used by the data? because if it is too small, I wanted to cut down the provisioned size(CapacityMB).
By the way, after I replaced line 13 with your new line, it returned following errors:
Add-Type : Cannot add type. The type name 'DiskInfo' already exists.
At line:19 char:1
+ Add-Type -Language CsharpVersion3 -TypeDefinition $DiskInfoDef
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (DiskInfo:String) [Add-Type], Exception
+ FullyQualifiedErrorId : TYPE_ALREADY_EXISTS,Microsoft.PowerShell.Commands.AddTypeCommand
That's a known issue with C# types.
You can't remove types defined this way.
You'll have to stop/start your PS session I'm afraid.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I closed out and restarted a new POWERSHELL session. This time there are no any errors, but nothing were put in the output file, just a empty file. Any idea?