PowerCLI

 View Only
Expand all | Collapse all

Get List of VMs, Datastores and VMDK / path per Cluster

  • 1.  Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jan 12, 2012 09:55 AM

    Hi Folks,

    as I´m new to this stuff, I´m wondering if anybody has already done some scripting work to gather a list of all virtual machines, its corresponding datastore(s) and the path to the VMDK-Files (or at least the name of it) - I know this part is already solved - found a couple of them ;-)

    But my problem is, I need this script drilled down per Cluster or Datacenter. As I said I´m completly new to this CLI-Stuff and don´t have a clue about how to solve this..

    Cheers

    Markus



  • 2.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jan 12, 2012 10:39 AM

    Hi Markus,

    Welcome to the VMware Communities!

    The next PowerCLI script will give you a list of all your VM's and their Datacenter, Cluster, HardDisks, Datastores and VMDK file paths. If you want to have the VM's only for a certain datacenter or cluster, you can filter the output by piping it to the Where-Object cmdlet.

    ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
      ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) { 
        ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
          ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
            "" | Select-Object -Property @{N="VM";E={$VM.Name}},
              @{N="Datacenter";E={$Datacenter.name}},
              @{N="Cluster";E={$Cluster.Name}},
              @{N="Hard Disk";E={$HardDisk.Name}},
              @{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},
              @{N="VMDKpath";E={$HardDisk.FileName}}
          }
        }
      }
    }    
    
    

    Don't hesitate to ask in this forum if you need more help.

    Regards, Robert



  • 3.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jan 12, 2012 10:59 AM

    Hi Robert,

    many thanks! That´s exactly what I´m looking for I think. I´m only struggling with one last thing... the Export-CSV-Thing... My  understanding would be someting like

    $myVariable | Export-CSV blablabla..

    But how would i define the $myVariable in your script?

    I´m sorry for those really stupid rookie-questions ... have to talk to my boss that I need time and lessons to learn this stuff ;-)

    Thanks,

    Markus



  • 4.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jan 12, 2012 11:58 AM

    There are two options if you want to write the output of the script to a .csv file. You can change the last line of the script into:

    } | Export-Csv -NoTypeInformation -UseCulture -Path "VmInfo.csv"
    
    

    This will pipe the output of the script to the Export-CSV cmdlet. And it will create a .csv file called VmInfo.csv.

    The second option is to change the first line of the script into:

    $VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
    
    

    And append the following line at the end of the script:

    $VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "VmInfo.csv"
    
    

    This will put the output of the script from my previous post into a variable $VmInfo. In the last line the variable is used to output it to a .csv file.



  • 5.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jan 12, 2012 12:00 PM

    Isn't option 1 giving you the infamous "An empty pipe element is not allowed." error ?



  • 6.  RE: Get List of VMs, Datastores and VMDK / path per Cluster
    Best Answer

    Posted Jan 12, 2012 12:15 PM

    Luc, you are right. I should have tested this.  :smileysad:

    This is a disadvantage of the ForEach command that I almost never use. Normally I use the Foreach-Object cmdlet and that works fine with pipeing the output to Export-CSV.

    Markus you have to use option 2 in this case.



  • 7.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jan 12, 2012 12:44 PM

    You guys are crazy ;-)

    Exactly what I  need :-) Many thanks to you ! Have just tested and works fine! If I find some spare time I´m trying to modify it a little bit more for more granularity in regards of choosing the cluster manually but for now that works perfect :-)

    Cheers
    Markus



  • 8.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jan 12, 2012 02:04 PM

    Shame on me... Forgot one more thing .... Configuratio File of the VM...

    Can anyone give a hint how to include this also?



  • 9.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jan 12, 2012 02:10 PM

    You can add the VM's configuration file to the script by changing the script into:

    $VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
      ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) { 
        ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
          ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
            "" | Select-Object -Property @{N="VM";E={$VM.Name}},
              @{N="Datacenter";E={$Datacenter.name}},
              @{N="Cluster";E={$Cluster.Name}},
              @{N="Hard Disk";E={$HardDisk.Name}},
              @{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},
              @{N="VMConfigFile";E={$VM.ExtensionData.Config.Files.VmPathName}},
              @{N="VMDKpath";E={$HardDisk.FileName}}
          }
        }
      }
    }
    $VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "VmInfo.csv"
    
    



  • 10.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Mar 06, 2012 09:30 PM

    Hi guys,


    Great script, works well, but I was wondering if it's possible to get the size of the VMDK file added to the output as well? How would I do this?



  • 11.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Mar 07, 2012 04:06 PM
    I will just add one line to this
    
    $VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
      ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) { 
        ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
          ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
            "" | Select-Object -Property @{N="VM";E={$VM.Name}},
              @{N="Datacenter";E={$Datacenter.name}},
              @{N="Cluster";E={$Cluster.Name}},
              @{N="Hard Disk";E={$HardDisk.Name}},
              @{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},
              @{N="VMConfigFile";E={$VM.ExtensionData.Config.Files.VmPathName}},
              @{N="VMDKpath";E={$HardDisk.FileName}},
              @{N="VMDK Size";E={($vm.extensiondata.layoutex.file|?{$_.name -contains $harddisk.filename.replace(".","-flat.")}).size/1GB}}
          }
        }
      }
    }
    $VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "VmInfo.csv"

    Message was edited by: CRad14



  • 12.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Mar 07, 2012 08:44 PM

    Thanks for the reply, but I don't get the sizes in the final report, in fact it's still the same?



  • 13.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Mar 07, 2012 08:46 PM

    Hah!!! I missed a comma, I edited that post, it should be correct now.



  • 14.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Mar 07, 2012 08:58 PM

    Thanks for your help, it works!



  • 15.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Sep 18, 2012 03:37 PM

    The size is returning '0'.

    I'll poke around to see what's what...any help would be appreciated.

    (the vmdk's return fine - thanks for that...a big help!)



  • 16.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Mar 01, 2013 05:50 PM

    Worked out of the box for me.  I needed to get a list of our VM's in order to setup a script and this helped me tremendously.  Thanks.



  • 17.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Sep 23, 2015 04:17 PM

    it worked like a charm for our needs, thanks for sharing.



  • 18.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Sep 28, 2015 06:58 PM

    If you don't want to bother with Scripts just download RVTools. Should provide you with all of this information and more with this information.



  • 19.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Nov 17, 2021 11:20 PM

    Code becomes concise and elegant using the -pipelinevariable parameter, 

    $report = Get-Datacenter -pv datacenter | Get-Cluster -pv cluster | Get-VM -pv vm | Get-HardDisk -pv harddisk | ForEach-Object {
         [psCustomObject] @{
            VM = $vm.name
            Datacenter = $datacenter.Name
            Cluster = $cluster.Name
            HardDisk = $harddisk.Name
            Datastore = $HardDisk.FileName.Split("]")[0].TrimStart("[")
            SizeGB = $harddisk.CapacityGB
            DiskFormat = $harddisk.StorageFormat
            DiskType = $harddisk.DiskType
            VMDKpath = $harddisk.FileName
         }
    }
    $report | Out-GridView


    Cheers
    Xavi



  • 20.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jun 08, 2012 05:51 AM

    Is there a way to capture multiple IP address of those VMs as well? Tried to include some of the scripts I got online to merge with this one but just couldn't get it to work correctly. Currently, I only manage to capture 1 IP address.

    $VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
      ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {
        ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
          ForEach ($IP in $VM.Guest.IPAddress) {
            ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
            "" | Select-Object -Property @{N="VM";E={$VM.Name}},
              @{N="Datacenter";E={$Datacenter.name}},
              @{N="Cluster";E={$Cluster.Name}},
              @{N="IP";E={$IP}},
              @{N="Hard Disk";E={$HardDisk.Name}},
              @{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},
              @{N="VMConfigFile";E={$VM.ExtensionData.Config.Files.VmPathName}},
              @{N="VMDKpath";E={$HardDisk.FileName}},
              @{N="VMDK Size";E={($vm.extensiondata.layoutex.file|?{$_.name -contains $harddisk.filename.replace(".","-flat.")}).size/1GB}}
    }
          }
        }
      }
    }
    $VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "VmInfo.csv"


  • 21.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jun 08, 2012 05:59 AM

    The IPAddress property is an array, you can always join the elements of this array into 1 string.

    But note that you will get these same IP addresses (VM-level) displayed for each harddisk attached to that VM.

    Something like this

    $VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
      ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {
        ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
          ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
            "" | Select-Object -Property @{N="VM";E={$VM.Name}},
            @{N="Datacenter";E={$Datacenter.name}},
            @{N="Cluster";E={$Cluster.Name}},
           
    @{N="IP";E={[string]::Join(',',$VM.Guest.IPAddress)}},
           
    @{N="Hard Disk";E={$HardDisk.Name}},
            @{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},
            @{N="VMConfigFile";E={$VM.ExtensionData.Config.Files.VmPathName}},
            @{N="VMDKpath";E={$HardDisk.FileName}},
           
    @{N="VMDK Size";E={($vm.extensiondata.layoutex.file|?{$_.name -contains $harddisk.filename.replace(".","-flat.")}).size/1GB}}       }     }   } } $VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "VmInfo.csv"


  • 22.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jun 12, 2012 04:57 PM

    Hi - coming a little late to the party, but I believe this is the script that I'm looking for.

    I'm pretty new to scripting for PowerCLI and having issues getting this working on our infrastructure.  Getting an "Unexpected token '<' in expression or statement" error at line 10, character 72.  Seems that it doesn't like the closed square bracket.

    We're running version 4.1 and I'm using PowerCLI 4.1 as well.

    Any ideas?

    Thanks,

    --- A



  • 23.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jun 12, 2012 05:03 PM

    If you do not have a good reason to stick with PowerCLI 4.1, I would in any case advise an upgrade.

    Let's first check if the copy/paste didn't introduce an error.

    Try the attached script.

    And copy the complete error message, should it still appear.



  • 24.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jun 12, 2012 05:12 PM

    Thanks for the help -

    When running the attached script, I get another error "Export-CSV : Cannot bind argument to parameter 'InputObject' because it is null." at Line 18, Character 21 (which is the - in -NoTypeInformation, etc.) in the last line.

    Taking your advice to upgrade to PowerCLI 5.0.1 and hoping that that makes this issue go away.

    Thanks again,

    --- A



  • 25.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jun 12, 2012 05:21 PM

    That would mean that the variable $VmInfo is empty ?

    Try displaying the result on screen, remove the Export-Csv cmdlet at the end.



  • 26.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jun 12, 2012 06:47 PM

    I upgraded the PowerCLI on my workstation (Win7/64) to 5.0.1 and got the same error - in attempt to rule out the workstation as a problem, I also installed PowerCLI 5.0.1 on a server (2k3/32) and got the same error as well.

    If I remove the Export-CSV related code, the script runs (appears to take time to run, anyway) but has zero output.

    Curious...

    --- A



  • 27.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jun 12, 2012 07:43 PM

    Do you get any output when you just display the results on screen ?

    ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
      ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {
        ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
          ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
            "" | Select-Object -Property @{N="VM";E={$VM.Name}},
            @{N="Datacenter";E={$Datacenter.name}},
            @{N="Cluster";E={$Cluster.Name}},
            @{N="IP";E={[string]::Join(',',$VM.Guest.IPAddress)}},
            @{N="Hard Disk";E={$HardDisk.Name}},
            @{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},
            @{N="VMConfigFile";E={$VM.ExtensionData.Config.Files.VmPathName}},
            @{N="VMDKpath";E={$HardDisk.FileName}},
           
    @{N="VMDK Size";E={($vm.extensiondata.layoutex.file|?{$_.name -contains $harddisk.filename.replace(".","-flat.")}).size/1GB}}       }     }   } }


  • 28.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jun 12, 2012 08:02 PM

    No output at all when run from the server or the 32-bit or 64-bit PowerCLI on my workstation.

    --- A



  • 29.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jun 12, 2012 09:13 PM

    And no error messages ?

    Did you try the Get-Datacenter, Get-Cluster and Get-VM cmdlets seperately ?

    Do they return anything ?



  • 30.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jun 12, 2012 09:18 PM

    No error message when I run the script without the Export-CSV.

    Get-Datacenter produces expected output.

    Get-Cluster shows nothing.

    Get-VM produces expected output.

    --- A



  • 31.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jun 12, 2012 09:21 PM

    Ok, I think we're getting somewhere.

    It looks as if you don't have any clusters in your datacenter(s).

    This report will cycle through all the clusters, but since there are none, you will have no results.

    You can leave out the Foreach loop for the clusters, then you should be getting the results for each VM in all your datacenters.



  • 32.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jun 12, 2012 09:41 PM

    Thank you so much!

    Got it working - had to pull all references to $Cluster to get it running.  Runtime took about 5 minutes, had me worried that things were locked up, but all is well.

    Very, very cool... :smileyhappy:

    Thanks again,

    --- A



  • 33.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Sep 18, 2012 02:48 PM

    Sorry for bring up such an old post but how do I use this script with import-csv as a input,  I have a list of VM's in a CSV file that I want to find the used storage for. (note total storage utilsation not just VMDK sizes)

    regards

    Tom



  • 34.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Oct 02, 2012 03:54 PM

    Brilliant script thanks!! Does anyone know how to also list the logical folders that the VM's are in also please?

    I am able to use the following in the vCLI;

    $VMFolder = "Production"

    Get-Folder $VMFolder | Get-VM

    This shows me the production folder, but im trying to display all VM's and the folders they reside in, but i dont seem able to run it in the script.

    Many thanks

    Ian.



  • 35.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Oct 02, 2012 04:54 PM

    An option is to use the New-VIProperty cmdlet.

    See the YellowFolderName property on my VIProperties page under VirtualMachine.



  • 36.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Nov 07, 2012 06:00 PM

    Hello everyone,

    I'm having a hard time trying to get this script do what I want it to do.

    I'd like to just add a column that shows the provisioned space for each drive. Currently the "VMDK Size" column will show the amount of space actually being used, but would like to output the provisioned size of the drive as well. 

    I'm a little new at this, I appreciate any help. Any thoughts?

    Thanks,



  • 37.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Nov 07, 2012 07:22 PM

    One way of getting the allocated harddisk space is as follows

    $vm | Select Name,

    @{N="HD Space";E={$_.HardDisks | Measure-Object -Property CapacityGB -Sum | select -ExpandProperty Sum}}



  • 38.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Nov 07, 2012 08:57 PM

    Thanks for the quick reply. So far I am not able to get this added to the script in this thread though. Not sure what I'm doing wrong.

    $VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
      ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {
        ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
          ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
            "" | Select-Object -Property @{N="VM";E={$VM.Name}},
              @{N="Datacenter";E={$Datacenter.name}},
              @{N="Cluster";E={$Cluster.Name}},
              @{N="Hard Disk";E={$HardDisk.Name}},
              @{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},
              @{N="VMConfigFile";E={$VM.ExtensionData.Config.Files.VmPathName}},
              @{N="VMDKpath";E={$HardDisk.FileName}},
              @{N="VMDK Size";E={($vm.extensiondata.layoutex.file|?{$_.name -contains $harddisk.filename.replace(".","-flat.")}).size/1GB}},
          }
        }
      }
    }
    $VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "C:\VmInfo4.csv"


  • 39.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Nov 07, 2012 09:07 PM

    That inner loop goes through all the harddisks.

    Do you want the capacity per harddisk or the total for the VM ?



  • 40.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Nov 07, 2012 09:17 PM

    For each VM, idealy, one column would should how how big the hard drive is, and how much space is being used by that drive. E.G.:

    VMDatacenterClusterHard DiskDatastoreVMConfigFile VMDKpathVMDK SizeDrive Size
    Data1DC1C1Hard disk 1DS2[DS2] ....vmx[DS2] ....vmdk18.2966940
    Data1DC1C1Hard disk 2SS1[SS1] ....vmx[SS1] ....vmdk18.3029260
    Data2DC2C2Hard disk 1SS1[SS1] ....vmx[SS1] ....vmdk18.3796640









    Again, I appreciate your help!



  • 41.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Nov 07, 2012 09:27 PM

    Try something like this

    $VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
      ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {
        ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
          ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
            "" | Select-Object -Property @{N="VM";E={$VM.Name}},
            @{N="Datacenter";E={$Datacenter.name}},
           
    @{N="Cluster";E={$Cluster.Name}},
            @{N="Hard Disk";E={$HardDisk.Name}},
            @{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},
            @{N="VMConfigFile";E={$VM.ExtensionData.Config.Files.VmPathName}},
            @{N="VMDKpath";E={$HardDisk.FileName}},
            @{N="VMDK Size";E={($vm.extensiondata.layoutex.file|?{$_.name -contains $harddisk.filename.replace(".","-flat.")}).size/1GB}},
            @{N="Drive Size";E={$HardDisk.CapacityGB}}       }     }   } } $VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "C:\VmInfo4.csv"


  • 42.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Nov 07, 2012 10:18 PM

    Unfortunately the column still returns blank.



  • 43.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Nov 07, 2012 10:21 PM

    Which PowerCLI version are you using ?

    Do a

    Get-PowerCLIVersion


  • 44.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Nov 07, 2012 10:24 PM
    PowerCLI Version
    ----------------
       VMware vSphere PowerCLI 5.1 Release 1 build 793510
    ---------------
    Snapin Versions
    ---------------
       VMWare AutoDeploy PowerCLI Component 5.1 build 768137
       VMWare ImageBuilder PowerCLI Component 5.1 build 768137
       VMware License PowerCLI Component 5.1 build 669840
       VMware vSphere PowerCLI Component 5.1 build 793489


  • 45.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Nov 08, 2012 05:51 AM

    Strange, the CapacityGB property was introduced on the Harddisk object in PowerCLI 5.1.

    It works for me.

    Can you do

    Get-VM -Name <whateverVM> | Get-Harddisk | Get-Member

    to check if the CapacityGB property is there ?



  • 46.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Apr 05, 2013 03:38 PM

    What do I need to do in order to add GET-WWPN names to this script?

    #################################################

    $VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
      ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {
        ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
          ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
            "" | Select-Object -Property @{N="VM";E={$VM.Name}},        @{N="Datacenter";E={$Datacenter.name}},        @{N="Cluster";E={$Cluster.Name}},        @{N="Hard Disk";E={$HardDisk.Name}},        @{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},        @{N="VMConfigFile";E={$VM.ExtensionData.Config.Files.VmPathName}},        @{N="VMDKpath";E={$HardDisk.FileName}},        @{N="VMDK Size";E={($vm.extensiondata.layoutex.file|?{$_.name -contains $harddisk.filename.replace(".","-flat.")}).size/1GB}},        @{N="Drive Size";E={$HardDisk.CapacityGB}}
          }
        }
      }
    }
    $VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "C:\scripts\xxxxx.csv"



  • 47.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Apr 08, 2013 11:46 PM

    I believe this will help you with your request though I cannot take credit for the script.

    http://communities.vmware.com/thread/389982

    One way that you should be able to get HostSystems' HBA WWN info is like:

    ## get all HostSystems' .NET View object
    Get-View -ViewType HostSystem -Property name, Config.StorageDevice.HostBusAdapter | %{
       
    $viewHost = $_
       
    ## for each HBA that is a HostFibreChannelHba, get some info
        $viewHost.Config.StorageDevice.HostBusAdapter | ?{$_ -is [VMware.Vim.HostFibreChannelHba]} | %{
           
    New-Object -TypeName PSObject -Property @{
                VMHostName
    = $viewHost.Name
               
    ## the HBA Port WWN in hexadecimal, with each octet split by ":"
                HBAPortWWN = (("{0:x}" -f $_.PortWorldWideName) -split "(\w{2})" | ?{$_ -ne ""}) -join ":"
               
    ## the HBA Node WWN in hexadecimal, with each octet split by ":"
                HBANodeWWN = (("{0:x}" -f $_.NodeWorldWideName) -split "(\w{2})" | ?{$_ -ne ""}) -join ":"
               
    ## the HBA status ("online", for example)
                HBAStatus = $_.Status
            }
    ## end new-object
        } ## end foreach-object
    } | Select VMHostName, HBAPortWWN, HBANodeWWN, HBAStatus | Sort VMHostName

    This gets each host, and for each fiber channel HBA in the host, lists the Node- and Port- WWN in hex.  The output would be something like:

    VMHostName             HBAPortWWN                  HBANodeWWN                  HBAStatus
    ----------             ----------                  ----------                  ---------
    myhost0.domain.com     10:00:00:00:00:00:00:e0     20:00:00:00:00:00:00:e0     online
    myhost0.domain.com     10:00:00:00:00:00:00:5b     20:00:00:00:00:00:00:5b     online
    myhost1.domain.com     10:00:00:00:00:00:00:77     20:00:00:00:00:00:00:77     online
    myhost1.domain.com     10:00:00:00:00:00:00:58     20:00:00:00:00:00:00:58     online
    ...
    


  • 48.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jan 09, 2013 10:04 PM

    Robert,

    Great script though I would like to expand on it to include the snapshot size.  I've tried a number of ways to get the info - the only one that I can get to work still gives me a bit of garbage - the field name on the data line (I believe because I am asking for a select-object within a select-object).

    Any suggestions for making this work within the code you have or do I need to plug it in through a secondary array?



  • 49.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jan 10, 2013 07:43 AM

    The following PowerCLI code will give you the sum of the sizes of all snapshots of a VM:

    (Get-Snapshot -VM $VM | Measure-Object -Property SizeGB -Sum).Sum
    
    


    If you want to get a list of all the individual snapshot sizes then you can use:

    [string]::Join(' ',(Get-Snapshot -VM $VM | Sort-Object -Property Created | ForEach-Object {$_.SizeGB}))
    
    



  • 50.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jan 10, 2013 07:19 PM

    Ahh, now I see where I went wrong with the approach I was trying (using the string entry) - thanks.  I ended up moving the get-snapshot info to a spot right after the last ForEach to insert it into a variable within the array.  This does the job nicely since it captures it at the execution level instead of the select-object level though I won't have the advantage of being able to see the total size of all snapshots so I modified mine entry and it provided the desired result.

    BTW - why is a Join required to get the IP info? I thought it was used for manipuation of string contents.   I noticed that for the IP address issue as well - it seemed to me the Guest IP should be obtainable by a reference to Guest.IPAddress but for some reason it comes up NULL.  I'm still learning the powercli so I have a lot of things I'm missing that seem to be crucial to the process.

    How are you able to post your code directly into the your blog entries here?  I can post the raw text from a text document or post it as an image - but cannot get it posted directly from PowerGUI script editor.



  • 51.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Jan 10, 2013 09:33 PM

    If the property is an array then you can use the [string]::Join() method to convert the array to a single string. This is needed when you want to export the objects to a .csv file using the Export-CSV cmdlet because that does not work with array properties.

    Guest.IPAddress is also an array. So you can use the [string]::Join() method here also.

    To insert a script in a post with the PowerGUI colors, use the PowerGUI Edit - Copy As - Html option. While editing the post select HTML in the upper right corner to insert the code. To prevent Internet Explorer from not showing the last line of the script insert:

    <!--[if IE]>

    <![endif]-->


    between the last:

    </span></pre>

    of the script.



  • 52.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Feb 26, 2013 11:21 PM

    The following script executes and provides what was desired - though there are a few tweeks I would like to impose -

    1. One key tweek is how to get the output csv file to include the source path and file name of the executed script

    2. In the situations where I am looking at a snapshot existing I would also like to get the base file size for the vmdks.  For some reason - the report kills the file size on the base vmdk's when discovering snapshots almost like it is pulling the information from the config file instead of raw off the disk.  JUST ADDING A FOOTNOTE:  I noted that when there is a snapshot on the system, the VMDK size is reported as zero - which of course is incorrect.  So, I'm asking Luke or anyone who can, to tell me what is wrong.  I suspect it is because the extensiondata.name is changed when a snapshot is ran, and the config file is no longer pointed to the Flat file - but my knowledge of the extensiondata is weaker that it should be and I don't really know all the SDK content that lets me drill down to the specifics without a lot more coding.  I have included a copy of the output and the latest version of my PS1.  Anyone who can point me to better guidance on the extensiondata and how to pull the fine details from it will be very much appreciated.

    $VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {   ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {     ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {       ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name )) {         $SnapSize=Get-Snapshot -VM $VM | Measure-Object -Property SizeGB -Sum                  "" | Select-Object -Property @{N="VM";E={$VM.Name}},               @{N="Datacenter";E={$Datacenter.name}},               @{N="Folder";E={$VM.Folder}},               @{N="Host";E={$VM.VMHost}},               @{N="Cluster";E={$Cluster.Name}},                 @{N="# CPU";E={$VM.NumCpu}},               @{N="RAM";E={$VM.MemoryGB}},               @{N="IP";E={$VM.Guest.IPAddress}},               @{N="Hard Disk";E={$HardDisk.Name}},               @{N="HD Size";E={$HardDisk.CapacityGB}},               @{N="HD Type";E={$HardDisk.StorageFormat}},               @{N="Snap Size";E={$SnapSize.Sum}},               @{N="VMDK Size";E={($vm.extensiondata.layoutex.file|?{$_.name -contains $harddisk.filename.replace(".","-flat.")}).size/1GB}},               @{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},               @{N="VMDKpath";E={$HardDisk.FileName.Split("]")[1]}}        }     }   } }$VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path $path 

    Message was edited by: MD4SV



  • 53.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Apr 23, 2013 05:11 PM

    Is there anyway to add to the script getting the LUN ID of the datstore that the VMDK is on?



  • 54.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Sep 08, 2015 03:06 PM

    I know this is really old, but does anyone know how to add the annotations into this?   We would like to have the owners of the VM's listed in the output and they are in the Annotations section.



  • 55.  RE: Get List of VMs, Datastores and VMDK / path per Cluster

    Posted Sep 08, 2015 04:00 PM

    I figured it out...here is what I did.

    Connect-VIServer dabwinpvmvc01

    $VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {

      ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {

        ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {

          ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {

          ForEach ($Contact1 in ($VM | Get-Annotation -CustomAttribute "1st Contact" | Sort-Object -Property Name)) {

      ForEach ($Contact2 in ($VM | Get-Annotation -CustomAttribute "2nd Contact" | Sort-Object -Property Name)) {

            "" | Select-Object -Property @{N="VM";E={$VM.Name}},

              @{N="Datacenter";E={$Datacenter.name}},

              @{N="Cluster";E={$Cluster.Name}},

              @{N="Hard Disk";E={$HardDisk.Name}},

              @{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},

              @{N="VMDKpath";E={$HardDisk.FileName}},

              @{N="VMDK Size in GB";E={($vm.extensiondata.layoutex.file|?{$_.name -contains $harddisk.filename.replace(".","-flat.")}).size/1GB}},

       @{N="1st Contact";E={$Contact1.value}},

       @{N="2nd Contact";E={$Contact2.value}}

      } 

          }

       }

        }

      }

    }

    $VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "VmInfo.csv"