Automation

 View Only
Expand all | Collapse all

Generating a Report based on Resource Pool location

sp93

sp93Oct 13, 2008 11:58 AM

  • 1.  Generating a Report based on Resource Pool location

    Posted Oct 10, 2008 07:33 PM

    Hey Everyone,

    I'm very new to powershell, so forgive me if this is easy....however....

    I want to generate a .csv with the following information:

    VMname

    VMDK file size

    server location and resource pool location making sure to report correctly nested Resource Pools

    I have attempted this and have had limited success. I have never been able to combine all needs into one output or have been able to handle the nested Resource Pools......

    Thanks in advance



  • 2.  RE: Generating a Report based on Resource Pool location

    Posted Oct 13, 2008 11:58 AM

    Anybody?



  • 3.  RE: Generating a Report based on Resource Pool location

    Posted Oct 13, 2008 02:11 PM

    Something like this

    $report = @()
    
    $vms = Get-VM
    foreach ($vm in $vms){
      foreach($hd in $vm | Get-HardDisk){
        $row = "" | select VMname, VMdisksize, server, respool
        $row.VMname = $vm.Name
        $row.VMdisksize = $hd.CapacityKB
        $row.server = ($vm | Get-VMHost).Name
        $row.respool = ($vm | Get-ResourcePool).Name
        $report += $row
      }
    }
    
    $report | Export-Csv "C:\test.csv" -noTypeInformation
    

    Note that you will have a line per hard disk for guests that have more than 1 hard disk.



  • 4.  RE: Generating a Report based on Resource Pool location

    Posted Oct 13, 2008 02:29 PM

    copy and pasted your response into VI toolkit window and ran it.

    I keep getting the following over and over again :

    Get-ResourcePool : object reference not set to an instance of an object.

    At line:7 char:43

    + $row.respool = ($vm | Get-ResourcePool) <<<< .Name



  • 5.  RE: Generating a Report based on Resource Pool location

    Posted Oct 13, 2008 03:51 PM

    You did connect to the VC server with the Connect-VIServer cmdlet before ?



  • 6.  RE: Generating a Report based on Resource Pool location

    Posted Oct 13, 2008 04:47 PM

    Yes I was connected.

    I disconnected, reconnected then I ran it again, and this time it did complete, however the nested resource pools aren't there, only the resource pool the VM is in. If that resource pool is under another, it's not being displayed.

    Thanks



  • 7.  RE: Generating a Report based on Resource Pool location

    Posted Oct 13, 2008 05:46 PM

    Oops, I must have missed that requirement.

    This should give the complete resource pool path.

    $report = @()
     
    $vms = Get-VM
    foreach ($vm in $vms){
      foreach($hd in $vm | Get-HardDisk){
        $row = "" | select VMname, VMdisksize, server, respool
        $row.VMname = $vm.Name
        $row.VMdisksize = $hd.CapacityKB
        $row.server = ($vm | Get-VMHost).Name
    	$rpImpl = $vm | Get-ResourcePool
    	$rp = Get-View -id ($vm | Get-ResourcePool).Id
    	$nested = ""
    	while($rp.gettype().Name -eq "ResourcePool" -and $rp.Name -ne "Resources"){
    	  $nested += ("/" + $rp.Name)
    	  $rp = Get-View $rp.Parent
    	}
        $row.respool = $nested
        $report += $row
      }
    }
     
    $report | Export-Csv "C:\test.csv" -noTypeInformation
    



  • 8.  RE: Generating a Report based on Resource Pool location

    Posted Oct 13, 2008 06:27 PM

    extremely close, only problem is that the path for the resource pool is reveresed.

    so for something with a resource pool path of top level1\top level2 it's displaying top level2\top level1.....



  • 9.  RE: Generating a Report based on Resource Pool location
    Best Answer

    Posted Oct 13, 2008 08:17 PM

    As you can see (nearly) everything is possible with PowerShell :smileywink:

    $report = @()
     
    $vms = Get-VM
    foreach ($vm in $vms){
      foreach($hd in $vm | Get-HardDisk){
        $row = "" | select VMname, VMdisksize, server, respool
        $row.VMname = $vm.Name
        $row.VMdisksize = $hd.CapacityKB
        $row.server = ($vm | Get-VMHost).Name
    	$rpImpl = $vm | Get-ResourcePool
    	$rp = Get-View -id ($vm | Get-ResourcePool).Id
    	$nested = ""
    	while($rp.gettype().Name -eq "ResourcePool" -and $rp.Name -ne "Resources"){
    	  $nested = "/" + $rp.Name + "/" + $nested
    	  $rp = Get-View $rp.Parent
    	}
        $row.respool = $nested.TrimEnd("/")
        $report += $row
      }
    }
     
    $report | Export-Csv "C:\test.csv" -noTypeInformation
    



  • 10.  RE: Generating a Report based on Resource Pool location

    Posted Oct 14, 2008 11:56 AM

    simply awesome, thanks so much, it's exactly what I wanted.



  • 11.  RE: Generating a Report based on Resource Pool location

    Posted Oct 22, 2008 06:33 PM

    Thanks for this script. Works nice!



  • 12.  RE: Generating a Report based on Resource Pool location

    Posted Oct 29, 2008 10:14 AM

    Hi Luc,

    Thanks again for you contribution!

    I do have one question however. With the provided script, if a VM has multiple disks, the information is written on two lines.

    For example this output:

    VM1,17826816,"[Datastore1] VM1/VM1.vmdk",esx01,/RP01

    VM1,22020096,"[Datastore1] VM1/VM1_1.vmdk",esx01,/RP01

    I tried to change some stuff, but with no luck.

    Could you help me out on this one?

    Regards,



  • 13.  RE: Generating a Report based on Resource Pool location

    Posted Oct 29, 2008 10:59 AM

    What exactly did you try to change ?

    To have each VM on 1 line, even when it has multiple disks ?

    If yes, that could be done by placing the disks in an array but then the Export-CSV would have a problem.

    That is why I decided to have multiple lines.



  • 14.  RE: Generating a Report based on Resource Pool location

    Posted Oct 29, 2008 11:05 AM

    Luc,

    That exactly what i'm trying to do; So i want each VM on one line, with all the disks.

    I have this script:

    -


    Get-Cluster |

    %{$cluster = $_; get-vm -Location $_ |

    #%{$vm = $_; $dsDiskInfo = get-harddisk -vm $_ | % {$_.FileName, $_.CapacityKB};

    %{$vm = $_; $dsDiskInfo = get-harddisk -vm $_ | % {$_.FileName, $_.CapacityKB};

    write-host $cluster.name $vm.name $vm.NumCPU $vm.MemoryMB $dsDiskInfo

    }

    }

    -


    Which works pretty good, only i want to use export-csv instead of write-host. Searching for a solution i come across this post and your script.



  • 15.  RE: Generating a Report based on Resource Pool location

    Posted Jan 12, 2009 05:31 PM

    Hello All,

    I'm also very new to powershell and i look at this post and try to play whit your script and to adjust it to my needs but with no luck ;-( I need to generate a report that will include also the datastore information in this report

    Sorry with the incontinence

    Thanks in advance

    Guy



  • 16.  RE: Generating a Report based on Resource Pool location

    Posted Jan 12, 2009 06:28 PM

    Luckily the HardDiskImpl contains a property, called Filename, that contains the datastore.

    With a bit of string manipulation we can extract the datastore and include it in the report.



  • 17.  RE: Generating a Report based on Resource Pool location

    Posted Jan 13, 2009 06:30 AM

    LucD

    Thanks for your kindness and support

    Guy



  • 18.  RE: Generating a Report based on Resource Pool location

    Posted Oct 31, 2008 03:11 PM

    Hello Luc, I am trying to run your code and being a complete noob in the powershell game I am getting the following error. Can you help.

    Error is Unexpected token 'vms' in expression or statement

    Unexpected token 'vms' in expression or statement.

    At C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\Scripts\disksize.

    ps1:1 char:19

    + $report = @() $vms <<<< = Get-VM foreach ($vm in $vms){ foreach($hd in $vm |

    Get-HardDisk){ $row = "" | select VMname, VMdisksize, server, respool $row.VMn

    ame = $vm.Name $row.VMdisksize = $hd.CapacityKB $row.server = ($vm | Get-VMHost

    ).Name $rpImpl = $vm | Get-ResourcePool $rp = Get-View -id ($vm | Get-ResourceP

    ool).Id $nested = "" while($rp.gettype().Name -eq "ResourcePool" -and $rp.Name

    -ne "Resources"){ $nested = "/" + $rp.Name + "/" + $nested $rp = Get-View $rp.P

    arent } $row.respool = $nested.TrimEnd("/") $report += $row } } $report | Expor

    t-Csv "C:\test.csv" -noTypeInformation

    I copy and pasted your code directly into powershell cmd window and also created a ps1 file. Same error on both. What am I doing wrong LOL.



  • 19.  RE: Generating a Report based on Resource Pool location

    Posted Oct 31, 2008 03:23 PM

    I suspect you access the community with an IE browser ?

    There are some known problems (should be fixed somewhere in November).

    I attached the file so you have the correct script.



  • 20.  RE: Generating a Report based on Resource Pool location

    Posted Oct 31, 2008 03:36 PM

    Cheers mate, will be using firefox from now on. Appreciate the help.



  • 21.  RE: Generating a Report based on Resource Pool location

    Posted Oct 31, 2008 06:10 PM

    I have been trying and stumbling to find a way to accurately format powershell code in forums regardless of the browser used, and have come up with an approach in this blog post.

    Would be happy to receive improvements which I'll reflect.

    I was also very glad to see LucD's comment that there are improvements coming real soon.



  • 22.  RE: Generating a Report based on Resource Pool location

    Posted Oct 31, 2008 06:34 PM

    Thanks for the tip, I'll try your recommendations in my next "code" post.

    The hint that improvements are coming is in