VMware Cloud Community
zatara
Enthusiast
Enthusiast
Jump to solution

vmdk script

Here is what I'm currently doing to get a listing of each virtual machines vmdk files and the capacity:

get -vm | get-harddisk | out-File machines.txt

There are two extra things I want to do:

1. Add an extra column with the virtual machine name each vmdk belongs to. (It's already obvious from the vmdk path, but this will help with sorting).

2. Also, I have a custom field in vcenter called APP that is filled in for each virtual machine. I would like this to output that value in another column.

Can someone assist with this?

Thanks in advance!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

This should do the trick.

$report = @()
Get-VM | % {
      $disks = $_ | Get-HardDisk
	foreach($disk in $disks){
		$row = "" | Select VMname, Diskname, CapacityKB, Persistence, APPField
		$row.VMName = $_.Name
		$row.Diskname = $disk.Filename
              $row.CapacityKB = $disk.CapacityKB
		$row.Persistence = $disk.Persistence
		$row.APPField = $_.CustomFields
		$report += $row
	}
}
$report | ft -autosize | Out-File machines.txt

The script will handle guests with multiple hard disks. The report will create a line per hard disk.

The CustomFields property in the VirtualMachineImpl object is a dictionary, so we can fetch a specific attribute by using the attributename as the index.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

This should do the trick.

$report = @()
Get-VM | % {
      $disks = $_ | Get-HardDisk
	foreach($disk in $disks){
		$row = "" | Select VMname, Diskname, CapacityKB, Persistence, APPField
		$row.VMName = $_.Name
		$row.Diskname = $disk.Filename
              $row.CapacityKB = $disk.CapacityKB
		$row.Persistence = $disk.Persistence
		$row.APPField = $_.CustomFields
		$report += $row
	}
}
$report | ft -autosize | Out-File machines.txt

The script will handle guests with multiple hard disks. The report will create a line per hard disk.

The CustomFields property in the VirtualMachineImpl object is a dictionary, so we can fetch a specific attribute by using the attributename as the index.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Kevin_Hamilton
Contributor
Contributor
Jump to solution

Hello Zatara, I am sure what LucD has written will do the trick as 99.9% it is right on the money.

Just thought I would point you to the following link

I have played with this and it's great. It incorporates VI Toolkit with PowerGui and it runs a great report for all your VM's, including

VMDK locations, size and space used. Have a look it really is the business and as it's new it can only get better.

Cheers

H

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Kevin, I fully agree with you that the EcoShell is a great and free tool.

It offers most of the information the regular user is looking for.

As long as you use these built in scripts, it's indeed very easy to use.

The only (small) problem I see is that the learning curve for brewing your own reports is slightly steeper than with native VITK.

If you look at the code that is behind the built in scripts you will notice that there is quite a bit of EcoShell terminology in those scripts.

Which makes it, in my opinion, not so easy for the beginning PS/VITK user to roll his own customised reports.

Scott I hope you interpret this as constructive criticism.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Kevin_Hamilton
Contributor
Contributor
Jump to solution

LucD, totally agree with you mate. If trying to learn writing the scripts from scratch it aint the best resource. Mainly aimed at people like myself who although love the script side lack the knowledge to write them from scratch. I am learning honest LOL. Just a real good place for people to look at what it offers. I think it can be used in conjunction to a degree.

0 Kudos
zatara
Enthusiast
Enthusiast
Jump to solution

LucD,

That was very helpful. Thank you. I only had to modify one line by putting:

$row.APPField = $_.CustomFields.item("APP")

One other quick question,

Is there a way to automatically convert the KB to GB inside the script?

Thanks again!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

PowerShell has some built in constants like 1Kb (1024), 1Mb (1024 * 1024) and 1Gb (1024 * 1024 * 1024).

To go from Kb to Gb you would have to divide by 1024 * 1024 which is the 1Mb constant.

The adapted script:

$report = @()
Get-VM | % {
      $disks = $_ | Get-HardDisk
	foreach($disk in $disks){
		$row = "" | Select VMname, Diskname, CapacityGB, Persistence, APPField
		$row.VMName = $_.Name
		$row.Diskname = $disk.Filename
               $row.CapacityGB = $disk.CapacityKB / 1Mb
		$row.Persistence = $disk.Persistence
		$row.APPField = $_.CustomFields
		$report += $row
	}
}
$report | ft -autosize | Out-File machines.txt


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference