PowerCLI

 View Only
Expand all | Collapse all

List harddrive info per VM with VM name

  • 1.  List harddrive info per VM with VM name

    Posted Aug 11, 2009 08:20 PM

    I am a powershell newby. I figured out how to connect to virtual center and get a list of VMs, and I can get a list of all the harddrives, but I cannot figure out how to get the VM name included with the harddrive info. here is my script:

    Add-PSsnapin VMware.VimAutomation.Core

    connect-VIServer virtualcenterhost

    get-vm | get-harddisk | Export-Csv d:\output\results.csv

    I can get the VMname from the path in the filename, but that is not easy to parse, it would be much easier if I could add the name of the VM to the output somehow. and we have one screwed up VM where they were playing around and added a harddrive from one VM to another for testing, so I know that one if I went off the path info, it would be wrong.

    It would be really nice, if I could combine the output from this line with the harddrive info:

    get-vm | select name, Description, MemoryMB, numcpu | Export-Csv d:\output\testresults.csv

    I tried adding Harddisks to the select statement above, but I wnat the number of drives and size of each drive per VM.

    thanks

    Mike



  • 2.  RE: List harddrive info per VM with VM name



  • 3.  RE: List harddrive info per VM with VM name
    Best Answer

    Posted Aug 12, 2009 08:24 AM

    The problem is due to the fact that the cmdlet Export-Csv in this case takes the objects that are in the pipeline to write to the CSV file.

    In this case the objects that are in the pipeline is the output of the Get-Harddisk cmdlet, which is a HardDiskImpl object.

    And the HardDiskImpl object doesn't contain the name of the guest.

    One way of solving this is to use "calculated properties" on the Select-Object cmdlet.

    Something like this for example

    Get-VM | select Name, Description, MemoryMB, numcpu, @{Name="HardDisk";E={$_ | Get-HardDisk | Select Name, CapacityKB, Filename}} | Export-Csv "D:\output\results.csv"
    

    The problem you will in your CSV file is that the hard disk information is all in one column.

    That's because all the hard disk information is considered 1 object, a so-called PsObject with all the hard disk properties in there.

    If you want to have the hard disk information in separate columns you could do

    Get-VM | select Name, Description, MemoryMB, numcpu, @{Name="HardDisk";E={($_ | Get-HardDisk).Name}}, 
                                   @{Name="HDCapacity";Expression={($_ | Get-HardDisk).CapacityKB}} | Export-Csv "D:\output\results.csv"
    

    The problem with this line is that it doesn't handle guests with more than 1 hard disk very well.

    One of the solutions is to create your own "custom object" and populate the properties yourself.

    The disadvantage is that it will be difficult to maintain the one-liner format in this case.

    This is a possible solution

    $report = @()
    Get-VM | %{
    	$vm = $_
    	$_ | Get-HardDisk | %{
    		$row = "" | select VMname, Description, MemoryMB, numcpu, HDName, HDCapacity, HDFilename
    		$row.VMname = $vm.Name
    		$row.Description = $vm.Description
    		$row.MemoryMB = $vm.MemoryMB
    		$row.numcpu = $vm.numcpu
    		$row.HDName = $_.Name
    		$row.HDCapacity = $_.CapacityKB
    		$row.HDFilename = $_.Filename
    		$report += $row
    	}
    }
    
    $report | Export-Csv "C:\report.csv" -noTypeInformation
    

    Note that the resulting CSV file will contain a row for each hard disk for each guest.



  • 4.  RE: List harddrive info per VM with VM name

    Posted Aug 12, 2009 04:00 PM

    Thanks LucD,

    I had worked up a similar script using report late last night, but did not know I could use export-csv with report. that is a huge help, I was having to manually convert the output-file to a csv.

    Mike



  • 5.  RE: List harddrive info per VM with VM name

    Posted Sep 30, 2009 03:12 PM

    Hello,

    Im trying to create a script to help automate our DR environment a little and need some help. Im pretty new to scripting and am having trouble getting my head around this problem. Im trying to list all the disks of my VM's and then remove all those that are not the OS drive (Hard Disk 1 for all my VM's). I have used the code in this thread and I'm able to list all the disks of the VM's but thats about all I got. I need to know the best way to list all the disks by number per VM and filter out Hard disk #1, then input these results into the Remove_HD function. (I got this function from the forum http://communities.vmware.com/message/1285048 ) The Remove_HD code is working well for me, it just needs vmname and hard disk # as input parameters, so thats why im trying to get them from the code in this thread. Here is the code im working with from this thread so far, i can get a list of vmnames and disk and export to a text file, but i dont knwo how to filter out Hard Disk 1. Thanks in advance for the help.

    function ListVMDK {

    $vms = Get-Content C:\Scripts\PHXDRFailover\VM_List.txt

    foreach ($vm in $vms) {

    $report = @()

    Get-VM $vm | %{

    $vm = $_

    $_ | Get-HardDisk | %{

    $row = "" | select VMname, HDName

    $row.VMname = $vm.Name

    $row.HDName = $_.Name

    $report += $row

    }

    }

    $report | Out-File C:\Scripts\PHXDRFailover\VMDK_2Remove.txt

    }

    }

    Connect-VIServer "VC Server"

    ListVMDK

    Disconnect-VIServer -Confirm:$FALSE



  • 6.  RE: List harddrive info per VM with VM name

    Posted Oct 07, 2009 04:11 PM

    Hello,

    Thanks LucD for the great piece of code, i am using it to list out all the disk sizes I have with all my VMs. I have a question, does anyone know how to filter the results to show only VM's that have contain disks of a certain size? For example I want to show all VMs that have a harddisk(s) that are greater than 100GB in size. So i need to output just the VMnames that match that criteria. Thanks in advance for the help.

    Sean



  • 7.  RE: List harddrive info per VM with VM name

    Posted Oct 07, 2009 04:41 PM

    A possible answer to both your questions.

    In both cases a Where-Object cmdlet can help.

    function ListVMDK {
    	$vms = Get-VM
    
    	foreach ($vm in $vms) {
    		$report = @()
    		Get-VM $vm | % {
    			$vm = $_
    			$_ | Get-HardDisk | where {$_.Name -ne "Hard Disk 1"} | % {
    				$row = "" | select VMname, Description, MemoryMB, numcpu, HDName, HDCapacity, HDFilename
    				$row.VMname = $vm.Name
    				$row.Description = $vm.Description
    				$row.MemoryMB = $vm.MemoryMB
    				$row.numcpu = $vm.numcpu
    				$row.HDName = $_.Name
    				$row.HDCapacityKB = $_.CapacityKB
    				$row.HDFilename = $_.Filename
    				$report += $row
    			}
    		}
    		$report
    
    	}
    }
    
    ListVMDK | where {$_.HDCapacityKB -gt 10000} | Export-Csv "C:\test.csv" -noTypeInformation
    



  • 8.  RE: List harddrive info per VM with VM name

    Posted Oct 08, 2009 06:16 PM

    LucD,

    Thanks for the quick response, I shouldve been able to figure this out! Im still having trouble trying to get the output to just be a list of VM names that contain disks of greater than 100GB, rather than the names plus the other parameters. What is the best way to do this? Essentially i just want to output a list of vm names that match the criteria. Thanks again for all the help.

    Sean



  • 9.  RE: List harddrive info per VM with VM name

    Posted Oct 08, 2009 06:54 PM

    Change the last line of the script as follows

    ...
    ListVMDK | where {$_.HDCapacityKB -gt 10000} | Select VMname
    



  • 10.  RE: List harddrive info per VM with VM name

    Posted Oct 08, 2009 08:15 PM

    Awesome! Thanks LucD. I just added a bit on the end to remove dupe VM names. Thanks again.

    ListVMDK | where {$_.HDCapacityKB -gt 10000} | Select VMname | Select -uniq



  • 11.  RE: List harddrive info per VM with VM name

    Posted Oct 08, 2009 09:00 PM

    No prob. You can merge both select cmdlets.

    Saves a few CPU cycles :smileywink:

    ListVMDK | where {$_.HDCapacityKB -gt 10000} | Select VMname -uniq