My notes and ramblings, normally about automation

PowerCLI - Disk Space Utilization - .NET Object

· Read in about 3 min · (599 Words)
PowerCLI PowerShell VMware

Referencing documentation is one of those things that seems overwhelming at first, but ends up becoming fundamental. I’m working on a series of posts where I take specific tasks and show how to refer to the documentation to accomplish the task.

In this blog, we’ve been tasked with reporting information about a VM’s disk space and its associated filesystem. We’ll be working with the built in PowerCLI .Net objects.

For more information about this blog series, see the following kickoff blog post: PowerCLI - Documentation Overview

Disk Space Utilization - .Net Object

We’ll want to start by referencing the PowerCLI documentation, known as the cmdlet reference. Instead of diving right in on the cmdlet list, we’re going to instead take a look at the Types. Since, at the root of the request, we’re looking for information about a VM, we’ll start with the VirtualMachine object. There, we can see all of the available properties including some that are relevant to our task such as: HardDisks, UsedSpaceGB, and ProvisionedGB. We can also take note of a deprecation notice for the HardDisks property which, as part of the Type column, refers us to the HardDisk object. Moving to the HardDisk object page, we can see some additional properties we might want to reference by the names of CapacityGB and CapacityKB. We’re not quite ready to move to coding just yet, we haven’t found any filesystem information. There’s one other section, that’s part of the VirtualMachine object, we should explore. This is the VMGuest object. Here we can see properties that have been pulled from VMware Tools, including one very interesting property for the our task: Disks. This property is related to the DiskInfo object. On the DiskInfo object page, we can see properties such as Path, Capacity, FreeSpace, CapacityGB, and FreeSpaceGB.

Quick recap, based on the request for hard disk and filesystem of a VM, we’re going to be interested in the following properties:

Object Property
VirtualMachine UsedSpaceGB
VirtualMachine ProvisionedSpaceGB
HardDisk CapacityGB
DiskInfo Path
DiskInfo CapacityGB
DiskInfo FreeSpaceGB

We now know what information we’re looking to retrieve, so we can start figuring out what code we can use to retrieve this information. The two main areas we’ll be concerned with are the “Property of” and “Returned by” sections. For the first two properties, we can use the Get-VM cmdlet to return those. Due to the deprecation of the HardDisk property, we have to use the Get-HardDisk cmdlet to return CapacityGB. Then, for our last three properties, we can use the output from the original Get-VM cmdlet. We know this because the DiskInfo object is a property of VMGuest, which just happens to be a property of VirtualMachine.

We can break it down to be the following properties:

Cmdlet Property Path
Get-VM UsedSpaceGB
Get-VM ProvisionedSpaceGB
Get-HardDisk CapacityGB
Get-VM Guest – Disks – Path
Get-VM Guest – Disks – CapacityGB
Get-VM Guest – Disks – FreeSpaceGB

Here’s what it looks like in PowerCLI code:

# Populating variables with required cmdlets
$vm = Get-VM -Name $vmName
$hardDisk = Get-HardDisk -VM $vm
# Creating a new PowerShell custom object to easily output properties
$output = New-Object -TypeName PSCustomObject
# Adding desired properties to the new PowerShell object 
Add-Member -InputObject $output -MemberType NoteProperty -Name UsedSpaceGB -Value $vm.UsedSpaceGB
Add-Member -InputObject $output -MemberType NoteProperty -Name ProvisionedSpaceGB -Value $vm.ProvisionedSpaceGB
Add-Member -InputObject $output -MemberType NoteProperty -Name CapacityGB -Value $hardDisk.CapacityGB
Add-Member -InputObject $output -MemberType NoteProperty -Name GuestPath -Value $vm.Guest.Disks.Path
Add-Member -InputObject $output -MemberType NoteProperty -Name GuestCapacityGB -Value $vm.Guest.Disks.CapacityGB
Add-Member -InputObject $output -MemberType NoteProperty -Name GuestFreeSpaceGB -Value $vm.Guest.Disks.FreeSpaceGB
# Viewing the output

Here’s what it looks like in our PowerShell session: Example: Disk Information from .Net Object