Using VMware seriously requires a lot of (shared) storage. This kind of storage (on a SAN for instance) is quite expensive. So you might want to check if you are wasting a lot of this space. When you look at the storage in VMware, it consists of multiple abstraction layers. A virtual machine has one or more Logical Disks, which are indicated by driveletters. You can use WMI to determine the amount of used and free space (Win32_LogicalDisk). One or more logical disks are contained in a partition. One or more partitions reside on a physical disk. That physical disk is really a virtual disk, a vmdk file to be precise. One or more vmdk files reside in a Datastore, which can be found on a LUN on your SAN.
The following script enumerates most of these layers (from logical disk to datastore) and calculates the used and free space. The final line exports the results to a csv file for use in Excel. And the script also helps you to calculate the average free space by showing the totals without the duplicates (don’t try to average the averages in excel, that’s not accurate because datastores contain duplicates and averages should be weighed).

UPDATE: I have modified the script, so no more matching of disks is done based on disk size. The match is made based on SCSI IDs and WMI relations. Thanks to adavidm on the VI Toolkit Community

#Get VMware Disk Usage
# Created by Hugo Peeters
# http://www.peetersonline.nl
# VARIABLES
$Decimals = 1
$VCServer = "MYVCSERVER"
# SCRIPT
# Connect to VC
Write-Progress "Gathering Information" "Connecting to Virtual Center" -Id 0
$VC = Connect-VIServer $VCServer
# Create Output Collection
$myCol = @()
# List Datastores (Datastore Name)
Write-Progress "Gathering Information" "Listing Datastores" -Id 0
$Datastores = Get-Datastore | Sort Name
# List vms
Write-Progress "Gathering Information" "Listing VMs and Disk Files" -Id 0
$VMSummaries = @()
ForEach ($vm in (Get-VM))
	{
	$VMView = $VM | Get-View
	ForEach ($VirtualSCSIController in ($VMView.Config.Hardware.Device | Where {$_.DeviceInfo.Label -match "SCSI Controller"}))
		{
		ForEach ($VirtualDiskDevice  in ($VMView.Config.Hardware.Device | Where {$_.ControllerKey -eq $VirtualSCSIController.Key}))
			{
			$VMSummary = "" | Select VM, HostName, PowerState, DiskFile, DiskName, DiskSize, SCSIController, SCSITarget
			$VMSummary.VM = $VM.Name
			$VMSummary.HostName = $VMView.Guest.HostName
			$VMSummary.PowerState = $VM.PowerState
			$VMSummary.DiskFile = $VirtualDiskDevice.Backing.FileName
			$VMSummary.DiskName = $VirtualDiskDevice.DeviceInfo.Label
			$VMSummary.DiskSize = $VirtualDiskDevice.CapacityInKB * 1KB
			$VMSummary.SCSIController = $VirtualSCSIController.BusNumber
			$VMSummary.SCSITarget = $VirtualDiskDevice.UnitNumber
			$VMSummaries += $VMSummary
			}
		}
	Clear-Variable VMView -ErrorAction SilentlyContinue
	}
# Loop through Datastores
ForEach ($Datastore in $Datastores)
	{
	# List vmdk files in datastore (vmdk Name)
	Write-Progress "Gathering Information" ("Processing Datastore {0}" -f $Datastore.Name) -Id 0
	$DSView = $Datastore | Get-View
	$fileQueryFlags = New-Object VMware.Vim.FileQueryFlags
	$fileQueryFlags.FileSize = $true
	$fileQueryFlags.FileType = $true
	$fileQueryFlags.Modification = $true
	$searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
	$searchSpec.details = $fileQueryFlags
	$searchSpec.sortFoldersFirst = $true
	$dsBrowser = Get-View $DSView.browser
	$rootPath = "["+$DSView.summary.Name+"]"
	$searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)
	ForEach ($result in $searchResult)
		{
		ForEach ($vmdk in ($result.File | ?{$_.Path -like "*.vmdk"} | Sort Path))
			{
			Write-Progress "Gathering Information" ("Processing VMDK {0}" -f $vmdk.Path) -Id 1
			Write-Host "=============================================================================="
			# Find vm using the vmdk (VM Name)
			$VMRef = ($VMSummaries | ?{$_.DiskFile -match $Datastore.Name -and $_.DiskFile -match $vmdk.Path})
			"VMDK {0} belongs to VM {1}" -f $vmdk.Path, $VMRef.VM
			If ($VMRef.Powerstate -eq "PoweredOn")
				{
				Write-Host "VM is powered on" -ForegroundColor "yellow"
				$Partitions = Get-WmiObject -Class Win32_DiskPartition -ComputerName $VMRef.HostName
				If ($?)
					{
					$Disks = Get-WmiObject -Class Win32_DiskDrive -ComputerName $VMRef.HostName
					$LogicalDisks = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $VMRef.HostName
					$DiskToPartition = Get-WmiObject -Class Win32_DiskDriveToDiskPartition -ComputerName $VMRef.HostName
					$LogicalDiskToPartition = Get-WmiObject -Class Win32_LogicalDiskToPartition -ComputerName $VMRef.HostName
					Write-Host "Read partition and disk information" -ForegroundColor "yellow"
					# Match disk based on SCSI ID's
					$DiskMatch = $Disks | ?{($_.SCSIPort - 1) -eq $VMRef.SCSIController -and $_.SCSITargetID -eq $VMRef.SCSITarget}
					If ($DiskMatch -eq $null){Write-Warning "NO MATCHES!"}
					Else
						{
						Write-Host "Found match:" -ForegroundColor "yellow"
						$DiskMatch
						# Find the Partition(s) on this disk
						$PartitionsOnDisk = ($DiskToPartition | ?{$_.Antecedent -eq $DiskMatch.__PATH})
						If ($PartitionsOnDisk -eq $null){Write-Warning "NO PARTITIONS!"}
						Else
							{
							ForEach ($PartitionOnDisk in $PartitionsOnDisk)
								{
								Write-Host "Disk contains partition" -ForegroundColor "yellow"
								$PartitionOnDisk.Dependent
								$PartitionMatches = $Partitions | ?{$_.__PATH -eq $PartitionOnDisk.Dependent}
								ForEach ($PartitionMatch in $PartitionMatches)
									{
									$LogicalDiskRefs = $LogicalDiskToPartition | ?{$_.Antecedent -eq $PartitionMatch.__PATH}
									If ($LogicalDiskRefs -eq $null)
										{
										Write-Warning "NO LOGICAL DISKS!"
										}
									Else
										{
										ForEach ($LogicalDiskRef in $LogicalDiskRefs)
											{
											$LogicalDiskMatches = $LogicalDisks | ?{$_.__PATH -eq $LogicalDiskRef.Dependent}
											ForEach ($LogicalDiskMatch in $LogicalDiskMatches)
												{
												Write-Host "Matching Logical Disk:" -ForegroundColor "yellow"
												$LogicalDiskMatch
												# Create Output Object
												$myObj = "" | Select Datastore, DSSizeGB, DSFreeGB, DSPercentFree, DiskFile, VM, HardDisk, DriveLetter, DiskSizeGB, DiskFreeGB, PercFree
												# List datastore name
												$myObj.Datastore = $Datastore.Name
												# Determine datastore size in GB
												$myObj.DSSizeGB = [Math]::Round(($Datastore.CapacityMB * 1MB / 1GB),$Decimals)
												$myObj.DSFreeGB = [Math]::Round(($Datastore.FreeSpaceMB * 1MB / 1GB),$Decimals)
												# Determine datastore free space (DS%Free)
												$myObj.DSPercentFree = [Math]::Round((100*($Datastore.FreeSpaceMB/$Datastore.CapacityMB)),$Decimals)
												# List disk file name
												$myObj.DiskFile = $vmdk.Path
												# List VM Name
												$myObj.VM = $VMRef.VM
												# Determine virtual hard disk / logical drive
												$myObj.HardDisk = $VMRef.DiskName
												# Report driveletter
												$myObj.DriveLetter = $LogicalDiskMatch.DeviceID
												# Report Size
												$myObj.DiskSizeGB = [Math]::Round(($LogicalDiskMatch.Size / 1GB),$Decimals)
												# Report Free Space
												$myObj.DiskFreeGB = [Math]::Round(($LogicalDiskMatch.FreeSpace / 1GB),$Decimals)
												# Calculate Percentage free space
												$myObj.PercFree = [Math]::Round((100 * ([int]($LogicalDiskMatch.FreeSpace / 1MB) / [int]($LogicalDiskMatch.Size / 1MB))),$Decimals)
												Write-Host "RESULT:" -ForegroundColor "yellow"
												$myObj
												# Add output object to output collection
												$myCol += $myObj
												}
											Clear-Variable LogicalDiskMatches -ErrorAction SilentlyContinue
											}
										}
									Clear-Variable LogicalDiskRefs -ErrorAction SilentlyContinue
									}
								Clear-Variable PartitionMatches -ErrorAction SilentlyContinue
								}
							}
						Clear-Variable PartitionsOnDisk -ErrorAction SilentlyContinue
						}
					Clear-Variable DiskMatch -ErrorAction SilentlyContinue
					Clear-Variable Disks -ErrorAction SilentlyContinue
					Clear-Variable LogicalDisks -ErrorAction SilentlyContinue
					Clear-Variable DiskToPartition -ErrorAction SilentlyContinue
					Clear-Variable LogicalDiskToPartition -ErrorAction SilentlyContinue
					}
				Clear-Variable Partitions -ErrorAction SilentlyContinue
				}
			Else
				{
				Write-Host "VM is powered off" -ForegroundColor "yellow"
				}
			Clear-Variable VMRef -ErrorAction SilentlyContinue
			Write-Progress "Gathering Information" ("Processing VMDK {0}" -f $vmdk.Path) -Id 1 -Completed
			}
		}
	}
# Disconnect from VC
Disconnect-VIServer -Confirm:$False
# OUTPUT
Write-Host "==================================================="
Write-Host "==================================================="
$TotalDSFree = ($myCol | Select Datastore, DSFreeGB -Unique | Measure-Object DSFreeGB -Sum).Sum
$TotalDSSize = ($myCol | Select Datastore, DSSizeGB -Unique | Measure-Object DSSizeGB -Sum).Sum
$AverageDSFree =  [Math]::Round(100 * ($TotalDSFree / $TotalDSSize),$Decimals)
$AverageDiskFree =  [Math]::Round(100 * (($myCol | Measure-Object DiskFreeGB -Sum).Sum / ($myCol | Measure-Object DiskSizeGB -Sum).Sum),$Decimals)
Write-Host "Total DS Free: $TotalDSFree"
Write-Host "Total DS Size: $TotalDSSize"
Write-Host "Average DS Free Percentage: $AverageDSFree"
Write-Host "Average Disk Free Percentage: $AverageDiskFree"
$myCol | Export-Csv -NoTypeInformation 'P:\#TEMP\VMwareDiskUsage.csv'

»crosslinked«

Related posts:

  1. Remove vmware memory limits with Powershell
  2. Calculate vSphere 5 Licenses with Powershell (UPDATED)

  76 Responses to “Get VMware Disk Usage with Powershell”

  1. i have trouble connecting witht he first string

  2. I can connect but not with the script

  3. Hi, I tgried your script but its not working with vCenter 4.1. I get Messages following message: “vm xyz is off”, but all VM guest are running.

  4. I get the following error message:

    Attempted to divide by zero.
    At E:\Scripts\VM-Disks4.ps1:175 char:92
    + $AverageDiskFree = [Math]::Round(100 * (($myCol | Measure-Object DiskFreeGB -Sum).Sum / <<<< ($myCol | Measure-Object
    DiskSizeGB -Sum).Sum),$Decimals)
    + CategoryInfo : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException

    Could you give me an explanation why I get this error message?

  5. Derek,
    Looks like copy/paste error. I have updated the post so the formatting is better. Please try copying the script again.
    Hugo

  6. @Admin

    I still get the same error. I have a small test cluster running one VM currently (I know it sound sick :-)). I have the rights to do get-wmiobject queries. I don’t why it is saying attempted to divide by zero. That doesn’t make any sense to me.
    Do you have any other ideas?

    Complete powershell output:

    ====================================================
    VMDK VM01.vmdk belongs to VM VM01
    VM is powered on
    Read partition and disk information
    WARNING: NO MATCHES!
    ====================================================
    VMDK VM01_1.vmdk belongs to VM VM01
    VM is powered on
    Read partition and disk information
    WARNING: NO MATCHES!
    ====================================================
    VMDK VM01_2.vmdk belongs to VM VM01
    VM is powered on
    Read partition and disk information
    WARNING: NO MATCHES!
    ===================================================
    ===================================================

    Attempted to divide by zero.
    At E:\Scripts\petersonline-vmware-disk-usage.ps1:171 char:54
    + $AverageDSFree = [Math]::Round(100 * ($TotalDSFree / <<<< $TotalDSSize),$Decimals)
    + CategoryInfo : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException

    Attempted to divide by zero.
    At E:\Scripts\peetersonline-vmware-disk-usage.ps1:172 char:89
    + $AverageDiskFree = [Math]::Round(100 * (($myCol | Measure-Object DiskFreeGB -Sum).Sum / <<<< ($myCol | Measure-Object Di
    skSizeGB -Sum).Sum),$Decimals)
    + CategoryInfo : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException

    Total DS Free:
    Total DS Size:
    Average DS Free Percentage:
    Average Disk Free Percentage:

  7. Morpheus,
    Can you paste the output of the following commands?

    $vmView = Get-VM VM01 | Get-View
    $vmView.Config.Hardware.Device | Where {$_.DeviceInfo.Label -match "SCSI Controller"}
    Get-WmiObject -Class Win32_DiskDrive -ComputerName $vmView.Guest.HostName

    Hugo

  8. Thanks for your quick reply! Here is the output:

    HotAddRemove : True
    SharedBus : noSharing
    ScsiCtlrUnitNumber : 7
    BusNumber : 0
    Device : {2000, 2001, 2002}
    Key : 1000
    DeviceInfo : VMware.Vim.Description
    Backing :
    Connectable :
    ControllerKey : 100
    UnitNumber : 3
    DynamicType :
    DynamicProperty :

    Partitions : 1
    DeviceID : \\.\PHYSICALDRIVE0
    Model : VMware Virtual disk SCSI Disk Device
    Size : 48315294720
    Caption : VMware Virtual disk SCSI Disk Device

    Partitions : 3
    DeviceID : \\.\PHYSICALDRIVE1
    Model : VMware Virtual disk SCSI Disk Device
    Size : 48315294720
    Caption : VMware Virtual disk SCSI Disk Device

    Partitions : 1
    DeviceID : \\.\PHYSICALDRIVE2
    Model : VMware Virtual disk SCSI Disk Device
    Size : 21467980800
    Caption : VMware Virtual disk SCSI Disk Device

  9. Hmmm, not all properties are showing. My bad. Try this:

    $vmView = Get-VM VM01 | Get-View
    $vmView.Config.Hardware.Device | Where {$_.DeviceInfo.Label -match "SCSI Controller"} | fl *
    Get-WmiObject -Class Win32_DiskDrive -ComputerName $vmView.Guest.HostName | fl *
  10. No problem. Btw. the VM’s name is 912vmt01. New output:

    HotAddRemove : True
    SharedBus : noSharing
    ScsiCtlrUnitNumber : 7
    BusNumber : 0
    Device : {2000, 2001, 2002}
    Key : 1000
    DeviceInfo : VMware.Vim.Description
    Backing :
    Connectable :
    ControllerKey : 100
    UnitNumber : 3
    DynamicType :
    DynamicProperty :

    ConfigManagerErrorCode : 0
    LastErrorCode :
    NeedsCleaning :
    Status : OK
    DeviceID : \\.\PHYSICALDRIVE0
    StatusInfo :
    Partitions : 1
    BytesPerSector : 512
    ConfigManagerUserConfig : False
    DefaultBlockSize :
    Index : 0
    InstallDate :
    InterfaceType : SCSI
    MaxBlockSize :
    MaxMediaSize :
    MinBlockSize :
    NumberOfMediaSupported :
    SectorsPerTrack : 63
    Size : 48315294720
    TotalCylinders : 5874
    TotalHeads : 255
    TotalSectors : 94365810
    TotalTracks : 1497870
    TracksPerCylinder : 255
    __GENUS : 2
    __CLASS : Win32_DiskDrive
    __SUPERCLASS : CIM_DiskDrive
    __DYNASTY : CIM_ManagedSystemElement
    __RELPATH : Win32_DiskDrive.DeviceID=”\\\\.\\PHYSICALDRIVE0″
    __PROPERTY_COUNT : 51
    __DERIVATION : {CIM_DiskDrive, CIM_MediaAccessDevice, CIM_LogicalDevice, CIM_LogicalElement…}
    __SERVER : 912VMT01
    __NAMESPACE : root\cimv2
    __PATH : \\912VMT01\root\cimv2:Win32_DiskDrive.DeviceID=”\\\\.\\PHYSICALDRIVE0″
    Availability :
    Capabilities : {3, 4}
    CapabilityDescriptions : {Random Access, Supports Writing}
    Caption : VMware Virtual disk SCSI Disk Device
    CompressionMethod :
    CreationClassName : Win32_DiskDrive
    Description : Disk drive
    ErrorCleared :
    ErrorDescription :
    ErrorMethodology :
    FirmwareRevision : 1.0
    Manufacturer : (Standard disk drives)
    MediaLoaded : True
    MediaType : Fixed hard disk media
    Model : VMware Virtual disk SCSI Disk Device
    Name : \\.\PHYSICALDRIVE0
    PNPDeviceID : SCSI\DISK&VEN_VMWARE&PROD_VIRTUAL_DISK\5&22BE343F&0&000000
    PowerManagementCapabilities :
    PowerManagementSupported :
    SCSIBus : 0
    SCSILogicalUnit : 0
    SCSIPort : 2
    SCSITargetId : 0
    SerialNumber : 6000c295fce1c72fdac7991d60556608
    Signature : 4084640933
    SystemCreationClassName : Win32_ComputerSystem
    SystemName : 912VMT01
    Scope : System.Management.ManagementScope
    Path : \\912VMT01\root\cimv2:Win32_DiskDrive.DeviceID=”\\\\.\\PHYSICALDRIVE0″
    Options : System.Management.ObjectGetOptions
    ClassPath : \\912VMT01\root\cimv2:Win32_DiskDrive
    Properties : {Availability, BytesPerSector, Capabilities, CapabilityDescriptions…}
    SystemProperties : {__GENUS, __CLASS, __SUPERCLASS, __DYNASTY…}
    Qualifiers : {dynamic, Locale, provider, UUID}
    Site :
    Container :

    ConfigManagerErrorCode : 0
    LastErrorCode :
    NeedsCleaning :
    Status : OK
    DeviceID : \\.\PHYSICALDRIVE1
    StatusInfo :
    Partitions : 3
    BytesPerSector : 512
    ConfigManagerUserConfig : False
    DefaultBlockSize :
    Index : 1
    InstallDate :
    InterfaceType : SCSI
    MaxBlockSize :
    MaxMediaSize :
    MinBlockSize :
    NumberOfMediaSupported :
    SectorsPerTrack : 63
    Size : 48315294720
    TotalCylinders : 5874
    TotalHeads : 255
    TotalSectors : 94365810
    TotalTracks : 1497870
    TracksPerCylinder : 255
    __GENUS : 2
    __CLASS : Win32_DiskDrive
    __SUPERCLASS : CIM_DiskDrive
    __DYNASTY : CIM_ManagedSystemElement
    __RELPATH : Win32_DiskDrive.DeviceID=”\\\\.\\PHYSICALDRIVE1″
    __PROPERTY_COUNT : 51
    __DERIVATION : {CIM_DiskDrive, CIM_MediaAccessDevice, CIM_LogicalDevice, CIM_LogicalElement…}
    __SERVER : 912VMT01
    __NAMESPACE : root\cimv2
    __PATH : \\912VMT01\root\cimv2:Win32_DiskDrive.DeviceID=”\\\\.\\PHYSICALDRIVE1″
    Availability :
    Capabilities : {3, 4}
    CapabilityDescriptions : {Random Access, Supports Writing}
    Caption : VMware Virtual disk SCSI Disk Device
    CompressionMethod :
    CreationClassName : Win32_DiskDrive
    Description : Disk drive
    ErrorCleared :
    ErrorDescription :
    ErrorMethodology :
    FirmwareRevision : 1.0
    Manufacturer : (Standard disk drives)
    MediaLoaded : True
    MediaType : Fixed hard disk media
    Model : VMware Virtual disk SCSI Disk Device
    Name : \\.\PHYSICALDRIVE1
    PNPDeviceID : SCSI\DISK&VEN_VMWARE&PROD_VIRTUAL_DISK\5&22BE343F&0&000100
    PowerManagementCapabilities :
    PowerManagementSupported :
    SCSIBus : 0
    SCSILogicalUnit : 0
    SCSIPort : 2
    SCSITargetId : 1
    SerialNumber : 6000c2951c44d797d0bbffa986904b63
    Signature : 4084640957
    SystemCreationClassName : Win32_ComputerSystem
    SystemName : 912VMT01
    Scope : System.Management.ManagementScope
    Path : \\912VMT01\root\cimv2:Win32_DiskDrive.DeviceID=”\\\\.\\PHYSICALDRIVE1″
    Options : System.Management.ObjectGetOptions
    ClassPath : \\912VMT01\root\cimv2:Win32_DiskDrive
    Properties : {Availability, BytesPerSector, Capabilities, CapabilityDescriptions…}
    SystemProperties : {__GENUS, __CLASS, __SUPERCLASS, __DYNASTY…}
    Qualifiers : {dynamic, Locale, provider, UUID}
    Site :
    Container :

    ConfigManagerErrorCode : 0
    LastErrorCode :
    NeedsCleaning :
    Status : OK
    DeviceID : \\.\PHYSICALDRIVE2
    StatusInfo :
    Partitions : 1
    BytesPerSector : 512
    ConfigManagerUserConfig : False
    DefaultBlockSize :
    Index : 2
    InstallDate :
    InterfaceType : SCSI
    MaxBlockSize :
    MaxMediaSize :
    MinBlockSize :
    NumberOfMediaSupported :
    SectorsPerTrack : 63
    Size : 21467980800
    TotalCylinders : 2610
    TotalHeads : 255
    TotalSectors : 41929650
    TotalTracks : 665550
    TracksPerCylinder : 255
    __GENUS : 2
    __CLASS : Win32_DiskDrive
    __SUPERCLASS : CIM_DiskDrive
    __DYNASTY : CIM_ManagedSystemElement
    __RELPATH : Win32_DiskDrive.DeviceID=”\\\\.\\PHYSICALDRIVE2″
    __PROPERTY_COUNT : 51
    __DERIVATION : {CIM_DiskDrive, CIM_MediaAccessDevice, CIM_LogicalDevice, CIM_LogicalElement…}
    __SERVER : 912VMT01
    __NAMESPACE : root\cimv2
    __PATH : \\912VMT01\root\cimv2:Win32_DiskDrive.DeviceID=”\\\\.\\PHYSICALDRIVE2″
    Availability :
    Capabilities : {3, 4}
    CapabilityDescriptions : {Random Access, Supports Writing}
    Caption : VMware Virtual disk SCSI Disk Device
    CompressionMethod :
    CreationClassName : Win32_DiskDrive
    Description : Disk drive
    ErrorCleared :
    ErrorDescription :
    ErrorMethodology :
    FirmwareRevision : 1.0
    Manufacturer : (Standard disk drives)
    MediaLoaded : True
    MediaType : Fixed hard disk media
    Model : VMware Virtual disk SCSI Disk Device
    Name : \\.\PHYSICALDRIVE2
    PNPDeviceID : SCSI\DISK&VEN_VMWARE&PROD_VIRTUAL_DISK\5&22BE343F&0&000200
    PowerManagementCapabilities :
    PowerManagementSupported :
    SCSIBus : 0
    SCSILogicalUnit : 0
    SCSIPort : 2
    SCSITargetId : 2
    SerialNumber : 6000c296c6c454bb5777618a3576bdcb
    Signature : 4084640899
    SystemCreationClassName : Win32_ComputerSystem
    SystemName : 912VMT01
    Scope : System.Management.ManagementScope
    Path : \\912VMT01\root\cimv2:Win32_DiskDrive.DeviceID=”\\\\.\\PHYSICALDRIVE2″
    Options : System.Management.ObjectGetOptions
    ClassPath : \\912VMT01\root\cimv2:Win32_DiskDrive
    Properties : {Availability, BytesPerSector, Capabilities, CapabilityDescriptions…}
    SystemProperties : {__GENUS, __CLASS, __SUPERCLASS, __DYNASTY…}
    Qualifiers : {dynamic, Locale, provider, UUID}
    Site :
    Container :

  11. Odd. You’re SCSI bus:target numbers appear to be off. Try replacing this line of code in the script and see whether you get a match:

    #REPLACE
    $DiskMatch = $Disks | ?{($_.SCSIPort - 1) -eq $VMRef.SCSIController -and $_.SCSITargetID -eq $VMRef.SCSITarget}
    #WITH
    $DiskMatch = $Disks | ?{($_.SCSIPort - 2) -eq $VMRef.SCSIController -and ($_.SCSITargetID + 1) -eq $VMRef.SCSITarget}

    Hugo

  12. Wait, my bad (again). Show me the output of the following commands:

    $vmView = Get-VM VM01 | Get-View
    ForEach ($VirtualSCSIController in ($VMView.Config.Hardware.Device | Where {$_.DeviceInfo.Label -match "SCSI Controller"}))
    {
    	Write-Host $VirtualSCSIController.BusNumber
    	$VMView.Config.Hardware.Device | Where {$_.ControllerKey -eq $VirtualSCSIController.Key} | Select UnitNumber
    }

    Hugo

  13. Output:

    0

    UnitNumber
    ———-
    0
    1
    2

  14. Aha! It is only the SCSI bus ID that is off. Make this adjustment to the original script and you should get results:

    #REPLACE
    $DiskMatch = $Disks | ?{($_.SCSIPort - 1) -eq $VMRef.SCSIController -and $_.SCSITargetID -eq $VMRef.SCSITarget}
    #WITH
    $DiskMatch = $Disks | ?{($_.SCSIPort - 2) -eq $VMRef.SCSIController -and $_.SCSITargetID -eq $VMRef.SCSITarget}

    Hugo

  15. Cool it works! Thank you very much! Could you give me a short explanation what’s the difference between $_.SCSIPort – 1 and $_.SCSIPort – 2?

  16. A disk in Windows (and probably in Linux) gets a SCSI ID in the form of x:y, where x is a number indicating the SCSI Bus i.e. controller and y is the port i.e. disk. Take a look at Edit Settings for your VM and find each disk’s SCSI ID. In your case 0:0, 0:1 and 0:2.
    The script uses this ID to match the disks of your vm’s to the disk info found via WMI.
    In your case, WMI lists the IDs as 2:0, 2:1 and 2:2. In my testlab it reports 1:0,1:1 and 1:2 for the same vm disks. So instead of subtracting 1 from the SCSI Bus ID in my case, you need to subtract two.
    Hugo

  17. Dear Hugo,

    I did a test on our big cluster now. I see for some VMs WMI lists the IDs as 2:0, 2:1 and 2:2 but for others it lists the IDs as 1:0, 1:1 and 1:2. The strange thing is these VMs (w2k8R2) were installed with the same image. What could I do to get results from all VMs no matter if their IDs are 1 or 2? Thanks a lot for your help!

  18. One possible in one scenario: if you are certain you never use more than one SCSI Controller in a vm. Then you can remove the checking of the SCSI Bus ID:

    #REPLACE
    $DiskMatch = $Disks | ?{($_.SCSIPort - 1) -eq $VMRef.SCSIController -and $_.SCSITargetID -eq $VMRef.SCSITarget}
    #WITH
    $DiskMatch = $Disks | ?{$_.SCSITargetID -eq $VMRef.SCSITarget}

    Hugo

  19. I do not get results.

    this is the only output i receive.

    =============================
    =============================
    Total DS Free:
    Total DS Size:
    Average DS Free Percentage:
    Average Disk Free Percentage:

    Please help.

  20. I also get a bunch of false positives (VM Powered Off) looking through the code now.

    Running vCenter 4.1.0 Build 345043, and VMware vSphere PowerCLI 4.1 U1 build 332441

  21. Hey Man, very good script, thx for sharing!!

  22. I’m getting errors like:
    The term ‘Connect-VIServer’ is not recognized as the name of a cmdlet, function
    , script file, or operable program. Check the spelling of the name, or if a pat
    h was included, verify that the path is correct and try again.
    At C:\getdiskspace.ps1:10 char:23
    + $VC = Connect-VIServer <<<< $VCServer
    + CategoryInfo : ObjectNotFound: (Connect-VIServer:String) [], Co
    mmandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

  23. You need to install powerCLI and either run it from the powerCLI console or add the snapin to you powershell session.

  24. I get the same thing as Molotsi:

    =============================
    =============================
    Total DS Free:
    Total DS Size:
    Average DS Free Percentage:
    Average Disk Free Percentage:

    Is this because I tried to add the DiskFileSizeGB object you mentioned in the comments in the Script Repository post? Your script is what I’m looking for, however, without the DiskFileSizeGB object, it doesn’t give what I need.. Thanks in advance!

  25. can someone please post the full script, i cant get it to work

  26. Is there a way to exclude some servers? Because Linux servers will get an error.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

   
© 2012 PeetersOnline Suffusion theme by Sayontan Sinha