Automation

 View Only
  • 1.  List VMs and their datastores

    Posted Dec 17, 2008 09:56 PM

    I took a stab at listing all VMs in my VC with the datastores where their vmdks are located:

    $report = @()

    $allvms = Get-VM

    foreach ($vm in $allvms) {

    $row = "" | select VMNAME, DATASTORE

    $row.VMNAME = $vm.name

    {color:#ff0000}$row.DATASTORE = (Get-VM | get-datastore)

    $report += $row

    }

    $report | Export-Csv "C:\vm_ds.csv" -NoTypeInformation

    No luck. I know the problem is with the get-datastore cndlet, The file shows the VMNAME column populated with the VMs as expected, but the DATASTORE column reports System.Object[].

    Any help would be appreciated.



  • 2.  RE: List VMs and their datastores
    Best Answer

    Posted Dec 17, 2008 11:51 PM

    You are already in a loop through all the guests (the foreach loop in line 3), so there is no need to get (again) all the guest with the Get-VM cmdlet in the 6th line.

    You can just pass the guest (i $vm) to the Get-Datastore cmdlet in that line

    The System.Object[] entry in the CSV file is caused by the fact that the Export-CSV cmdlet doesn't know how to handle objects or arrays of objects.

    A solution is to loop through all the datastore names that are returned.

    And store the datastore name in the DATASTORE property.

    The result is that there will be multiple lines in the CSV file for guests that have more than 1 datastore.

    $report = @()
    
    $allvms = Get-VM
    foreach ($vm in $allvms) {
    	$dstores = $vm | Get-Datastore
    	foreach($ds in $dstores){
    		$row = "" | select VMNAME, DATASTORE
    		$row.VMNAME = $vm.name
    		$row.DATASTORE = $ds.Name
    		$report += $row
    	}
    }
    
    $report | Export-Csv "C:\vm_ds.csv" -NoTypeInformation
    



  • 3.  RE: List VMs and their datastores

    Posted Mar 26, 2009 01:12 PM

    LucD,

    What does the += in $report += $row stand for?

    And the "" in $row = "" | select VMNAME, DATASTORE ?



  • 4.  RE: List VMs and their datastores

    Posted Mar 26, 2009 01:17 PM

    The first is shorthand notation for

    $report = $report + $row
    

    The 2nd is the initialisation of the variable $row with an empty string.



  • 5.  RE: List VMs and their datastores

    Posted Mar 26, 2009 01:19 PM

    Thank you.



  • 6.  RE: List VMs and their datastores

    Posted Mar 26, 2009 04:13 PM

    If I wanted to add the Cluster Name for each vm to this report how would I go about doing that? Thanks for any input.

    I think I might have figured it out, please tell me if this looks correct?

    $report = @()

    $allvms = Get-VM

    foreach ($vm in $allvms) {

    $dstores = $vm | Get-Datastore

    foreach($ds in $dstores){

    $cluster = $vm | Get-Cluster

    foreach ($clus in $cluster){

    $row = "" | select VMNAME, DATASTORE, CLUSTER

    $row.VMNAME = $vm.name

    $row.DATASTORE = $ds.Name

    $row.CLUSTER = $clus.name

    $report += $row

    }

    }

    }

    $report | Export-Csv "C:\vm_ds.csv" -NoTypeInformation



  • 7.  RE: List VMs and their datastores

    Posted Mar 26, 2009 04:40 PM

    try this:

    get-cluster |`
      %{$cluster = $_; get-vm -Location $_ |
        %{$vm = $_; $dsNames = get-datastore -vm $_ | % {$_.name};
          write-host $cluster.name $vm.name $dsNames
        }
      }
    

    http://www.vmwarescripting.com



  • 8.  RE: List VMs and their datastores

    Posted Mar 26, 2009 04:42 PM

    The Get-Cluster cmdlet returns an object.

    You would probably only want the name of the cluster.

    You could do that like this

    $report = @() 
    $allvms = Get-VM 
    foreach ($vm in $allvms) { 
            $clusterName = ($vm | Get-Cluster).Name
            $dstores = $vm | Get-Datastore 
            foreach($ds in $dstores){ 
                    $row = "" | select VMNAME, DATASTORE, Cluster 
                    $row.VMNAME = $vm.name 
                    $row.DATASTORE = $ds.Name 
                    $row.Cluster = $clusterName
                    $report += $row 
            } 
    } 
    $report | Export-Csv "C:\vm_ds.csv" -NoTypeInformation 
    



  • 9.  RE: List VMs and their datastores

    Posted Mar 26, 2009 05:04 PM

    Guys,

    Thanks for your help, one thing I noticed is that it takes a very long time for the script to run and return the info, is there anyway I can speed it up?



  • 10.  RE: List VMs and their datastores

    Posted Mar 26, 2009 05:14 PM

    sure, reduce the amount of virtual machines you have :smileyhappy:

    http://www.vmwarescripting.com



  • 11.  RE: List VMs and their datastores

    Posted Mar 26, 2009 05:20 PM

    LMAO....so true, I was just hoping there was something else up your sleeve.