Automation

 View Only
  • 1.  vmdk script

    Posted Apr 23, 2009 12:07 AM

    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!



  • 2.  RE: vmdk script
    Best Answer

    Posted Apr 23, 2009 05:19 AM

    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.



  • 3.  RE: vmdk script

    Posted May 26, 2009 03:07 PM

    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!



  • 4.  RE: vmdk script

    Posted May 26, 2009 03:16 PM

    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
    



  • 5.  RE: vmdk script

    Posted Apr 23, 2009 10:53 AM

    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



  • 6.  RE: vmdk script

    Posted Apr 23, 2009 11:47 AM

    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.



  • 7.  RE: vmdk script

    Posted Apr 23, 2009 12:42 PM

    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.