VMware Cloud Community
markus81
Contributor
Contributor
Jump to solution

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

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

54 Replies
zerotraction
Contributor
Contributor
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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}}       }     }   } }


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
zerotraction
Contributor
Contributor
Jump to solution

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

--- A

0 Kudos
LucD
Leadership
Leadership
Jump to solution

And no error messages ?

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

Do they return anything ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
zerotraction
Contributor
Contributor
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
zerotraction
Contributor
Contributor
Jump to solution

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... Smiley Happy

Thanks again,

--- A

0 Kudos
TomHowarth
Leadership
Leadership
Jump to solution

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

Tom Howarth VCP / VCAP / vExpert
VMware Communities User Moderator
Blog: http://www.planetvm.net
Contributing author on VMware vSphere and Virtual Infrastructure Security: Securing ESX and the Virtual Environment
Contributing author on VCP VMware Certified Professional on VSphere 4 Study Guide: Exam VCP-410
0 Kudos
mastercam
Contributor
Contributor
Jump to solution

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!)

0 Kudos
Imcbride
Contributor
Contributor
Jump to solution

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.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

An option is to use the New-VIProperty cmdlet.

See the YellowFolderName property on my VIProperties page under VirtualMachine.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jlinc
Contributor
Contributor
Jump to solution

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,

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jlinc
Contributor
Contributor
Jump to solution

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"
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That inner loop goes through all the harddisks.

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jlinc
Contributor
Contributor
Jump to solution

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!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

jlinc
Contributor
Contributor
Jump to solution

Unfortunately the column still returns blank.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which PowerCLI version are you using ?

Do a

Get-PowerCLIVersion


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
jlinc
Contributor
Contributor
Jump to solution

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
0 Kudos