I got a question from a colleague if we could export the disk information of a certain vm which had 37 vmdk’s and they needed the size and names of all of them. So i went searching for something like i usually do with the use of Google :). I came across another blogger who had written an entire function for this use case :

i will put the function on here as well but be sure to give this guy a visit if you use it :). My entire script consists of a connection to a vcenter his function and a choice for display on console or export to csv.

function Get-VMDisk {

[CmdletBinding()]
    param( 
        [Parameter(Mandatory=$True, ValueFromPipeline=$True, Position=0, HelpMessage = "VMs to process")]
        [ValidateNotNullorEmpty()]
          [VMware.VimAutomation.ViCore.Impl.V1.Inventory.InventoryItemImpl[]] $myVMs
    )
Process {
        $View = @()
        foreach ($myVM in $myVMs){
            $VMDKs = $myVM | get-HardDisk
            foreach ($VMDK in $VMDKs) {
                if ($VMDK -ne $null){
                    [int]$CapacityGB = $VMDK.CapacityKB/1024/1024
                    $Report = [PSCustomObject] @{
                            Name = $myVM.name 
                            PowerState = $myVM.PowerState
                            Datastore = $VMDK.FileName.Split(']')[0].TrimStart('[')
                            VMDK = $VMDK.FileName.Split(']')[1].TrimStart('[')
                            StorageFormat = $VMDK.StorageFormat
                            CapacityGB = $CapacityGB
                            Controller = $VMDK.ExtensionData.ControllerKey -1000
                            Unit = $VMDK.ExtensionData.UnitNumber
                        }
                        $View += $Report
                    }   
                }
            }
    $View | Sort-Object VMname, Controller, Unit
    }
}

#Make Connection with vCenter
Try {
    if ($Connect) {
      Continue
    }
    else {
      $Credential = Get-Credential -UserName "Username" -Message "Provide password."
      $Connect = Connect-VIServer "vcenter" -Credential $Credential
      Write-Host Connected to $Connect -ForegroundColor Green
    }
  }
  Catch {
    $Error
    Write-Host Unable to connect to $Connect -ForegroundColor Red
  }
#Output to Console
Get-VM "name"| Get-VMDisk | ft -AutoSize

#Export to CSV
Get-VM "name"| Get-VMDisk | Export-Csv -NoTypeInformation -UseCulture -Path "VM-Disks.csv"

#Disconnect vCenter Connection
Disconnect-VIServer *

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: