Virtual Machine VMDK file report with PowerCLI

I have created a simple report-script that gives a list of what kind of format and how many vmdk each VM has. The report tells me if the disks are Thin or Thick and what size they are i GB.

after some magic in Excel it looks like this ๐Ÿ™‚

And the powerCLI script looks like this, it is quite simple but still gives me information that i need for all my VMs on all datastores and quickly i can tell which machines that uses a lot of disk on my precious SAN ๐Ÿ˜‰

 

# Get data about vmdk and format
# 
# Niklas ร…kerlund / RTS

$VMs = Get-VM *
$Data = @()

 foreach ($VM in $VMs){
	$VMDKs = $VM | get-HardDisk
	foreach ($VMDK in $VMDKs) {
		if ($VMDK -ne $null){
			$CapacityGB = $VMDK.CapacityKB/1024/1024
			$CapacityGB = [int]$CapacityGB
			$into = New-Object PSObject
			Add-Member -InputObject $into -MemberType NoteProperty -Name VMname $VM.Name
			Add-Member -InputObject $into -MemberType NoteProperty -Name Datastore $VMDK.FileName.Split(']')[0].TrimStart('[')
			Add-Member -InputObject $into -MemberType NoteProperty -Name VMDK $VMDK.FileName.Split(']')[1].TrimStart('[')
			Add-Member -InputObject $into -MemberType NoteProperty -Name StorageFormat $VMDK.StorageFormat
			Add-Member -InputObject $into -MemberType NoteProperty -Name CapacityGB $CapacityGB
			$Data += $into
		}
	}

}
$Data | Sort-Object VMname,Datastore,VMDK | Export-Csv -Path C:\temp\VM-VMDKs.csv -NoTypeInformation

Comments

Jeffery Hicks
Reply

Instead of dividing by 1024/1024 , use 1MB. You can also cast it as an [int] with the same command.

[int]$CapacityGB = $VMDK.CapacityKB/1MB

Lastly, since you are defining all of your properties at once for the custom object, consider using a hashtable of property values

$into=New-Object PSObject -property @{
VMName=$VM.Name
Datastore=$VMDK.FileName.Split(‘]’)[0].TrimStart(‘[‘)

}

Niklas
Reply

Sweet! thanks for the input!

Amit Patange
Reply

Would it be possible to get details of each VM disk on 1 line, instead of separating the details based on disks!

Niklas
Reply

I do not know how to extend dynamically in the object, you can put all VM disk details on a string instead with a foreach loop and that way solve it..

Japinator
Reply

Thanks Niklas, exactly what I needed to detail which specific VMs were located on each datastore.

Andrew Dauncey
Reply

I know this is an old post, but just wanted to say thanks. I used it this week. It was a great launch pad for me to learn more powershell.

jim
Reply

Hi Niklas,

Thanks for great script, i was trying to pull out each datastore NAA ID information but some how its does not work,greatly appreciate if you can provide me some help .

Thank you,
jim

Basim
Reply

Hi Niklas.

thanks a lot… ๐Ÿ™‚
excellent script. just wanna figure out if we can display the utilized space as well

shamal
Reply

Is there a way to get the last modified date of VMDK

Superspoons
Reply

Great Script. How would I supply a number of servers supplied in a txt file ?

thanks
SP

Leave a comment

name*

email* (not published)

website