VMware Cloud Community
sanjaygpt171
Enthusiast
Enthusiast

PowerCLI scrip to get a list of VMs, Attributes (Team and use case) and used disk space

I want powerCLI script get a list of VMs, Attributes (Team and use case) , disk size andused disk space

Reply
0 Kudos
19 Replies
LucD
Leadership
Leadership

What do you already have?


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

Reply
0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

I have access to vcenter but need to pull the information mentioned above. It's having 600 VM's

Reply
0 Kudos
LucD
Leadership
Leadership

I meant what script do you already have?
What is missing?
What is not working?


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

Reply
0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

Below is the script, i am trying to run,

$report = @() 

# $vm = get-cluster <cluster_name> | Get-VM | select Name

# foreach($vm in (get-cluster <cluster_name> | Get-VM | select Name))

foreach($vm in (Get-Content -Path C:\TEMP\vmliste.txt))

{

    Get-HardDisk -VM $vm | ForEach-Object { 

        $HardDisk = $_ 

        $row = "" | Select Hostname, VM, GuestName, Datastore, VMXpath, HardDisk, DiskType, CapacityGB, DiskFreespace, TotalVMFSConsumed, ProvisionType 

                    $row.Hostname = $vm.VMHost.Name 

                    $row.VM = $VM.Name 

                    $row.GuestName = $vm.Guest.HostName 

                    $row.Datastore = $HardDisk.Filename.Split("]")[0].TrimStart("[") 

                    $row.VMXpath = $HardDisk.FileName 

                    $row.HardDisk = $HardDisk.Name 

                    $row.CapacityGB = ("{0:f1}" -f ($HardDisk.CapacityKB/1MB)) 

# $row.DiskFreespace = $row.CapacityGB - ($_.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum)

$row.DiskFreespace = $vm.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum

$row.DiskType = $HardDisk.get_DiskType() 

$row.TotalVMFSConsumed = $vm.get_UsedSpaceGB() 

$row.ProvisionType = $HardDisk.StorageFormat 

                    $report += $row 

  } 

 

}

$report | Export-Csv -Path C:\users\sguptaadmin\Desktop\VMDisk-CapacityReport1.csv -NoTypeInformation -UseCultur

Reply
0 Kudos
LucD
Leadership
Leadership

And what is the problem you are encountering with that script?


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

Reply
0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

Below is the error

Method invocation failed because [System.String] does not contain a method named 'get_UsedSpaceGB'.

At C:\users\sguptaadmin\Desktop\clusterreport1.ps1:20 char:1

+ $row.TotalVMFSConsumed = $vm.get_UsedSpaceGB()

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : MethodNotFound

Reply
0 Kudos
LucD
Leadership
Leadership

Not sure why you are using a method here, just refer to the property.
Something like this

$report = @()

# $vm = get-cluster <cluster_name> | Get-VM | select Name

# foreach($vm in (get-cluster <cluster_name> | Get-VM | select Name))

foreach($vm in (Get-Content -Path C:\TEMP\vmliste.txt))

{

   Get-HardDisk -VM $vm | ForEach-Object {

   $HardDisk = $_

   $row = "" | Select Hostname, VM, GuestName, Datastore, VMXpath, HardDisk, DiskType, CapacityGB, DiskFreespace, TotalVMFSConsumed, ProvisionType

   $row.Hostname = $vm.VMHost.Name

   $row.VM = $VM.Name

   $row.GuestName = $vm.Guest.HostName

   $row.Datastore = $HardDisk.Filename.Split("]")[0].TrimStart("[")

   $row.VMXpath = $HardDisk.FileName

   $row.HardDisk = $HardDisk.Name

   $row.CapacityGB = ("{0:f1}" -f ($HardDisk.CapacityKB/1MB))

   # $row.DiskFreespace = $row.CapacityGB - ($_.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum)

   $row.DiskFreespace = $vm.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum

   $row.DiskType = $HardDisk.DiskType

   $row.TotalVMFSConsumed = $vm.UsedSpaceGB

   $row.ProvisionType = $HardDisk.StorageFormat

   $report += $row

  }

}


$report | Export-Csv -Path C:\users\sguptaadmin\Desktop\VMDisk-CapacityReport1.csv -NoTypeInformation -UseCultur


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

Reply
0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

report.PNGrequired_col.PNG

I have tried your script but the "used disk space" column is not pulling any result. In report section, it is showing as empty. Also, i need to pull custom attributes of VM's. Can you also add that in script.

Reply
0 Kudos
LucD
Leadership
Leadership

I'm sorry, but that is not "my" script, you came with that script.

The DiskFreeSpace property will only contain a value for a VM that has the VMware Tools installed and is powered on.


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

Reply
0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

Could you please correct the script , if i am making some mistake in it. Or you can provide your own script. I need to completed this ASAP. Could you please help me with that.

Below is the reference link:

https://communities.vmware.com/thread/581948

https://communities.vmware.com/thread/581948

Reply
0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

When i am running on single server providing VM_name valur in scrip, it is running fine. Below is the script

======================================================

$report = @() 

$vm = Get-VM -Name "VM_name"

    Get-HardDisk -VM $vm | ForEach-Object { 

        $HardDisk = $_ 

        $row = "" | Select Hostname, VM, GuestName, Datastore, VMXpath, HardDisk, DiskType, CapacityGB, DiskFreespace, TotalVMFSConsumed, ProvisionType 

                    $row.Hostname = $vm.VMHost.Name 

                    $row.VM = $VM.Name 

                    $row.GuestName = $vm.Guest.HostName 

                    $row.Datastore = $HardDisk.Filename.Split("]")[0].TrimStart("[") 

                    $row.VMXpath = $HardDisk.FileName 

                    $row.HardDisk = $HardDisk.Name 

                    $row.CapacityGB = ("{0:f1}" -f ($HardDisk.CapacityKB/1MB)) 

# $row.DiskFreespace = $row.CapacityGB - ($_.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum)

$row.DiskFreespace = $vm.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum

$row.DiskType = $HardDisk.get_DiskType() 

$row.TotalVMFSConsumed = $vm.get_UsedSpaceGB() 

$row.ProvisionType = $HardDisk.StorageFormat 

                    $report += $row 

  } 

 

$report | Export-Csv -Path C:\users\sguptaadmin\Desktop\VMDisk-CapacityReport1.csv -NoTypeInformation -UseCultur

======================================================================================

The main issue occur , if i am using taking value from cluster or a list of VM's from text file. Below code i have used.

$report = @()

# $vm = get-cluster <cluster_name> | Get-VM | select Name

# foreach($vm in (get-cluster <cluster_name> | Get-VM | select Name))

foreach($vm in (Get-Content -Path C:\TEMP\vmliste.txt))

{

   Get-HardDisk -VM $vm | ForEach-Object {

   $HardDisk = $_

   $row = "" | Select Hostname, VM, GuestName, Datastore, VMXpath, HardDisk, DiskType, CapacityGB, DiskFreespace, TotalVMFSConsumed, ProvisionType

   $row.Hostname = $vm.VMHost.Name

   $row.VM = $VM.Name

   $row.GuestName = $vm.Guest.HostName

   $row.Datastore = $HardDisk.Filename.Split("]")[0].TrimStart("[")

   $row.VMXpath = $HardDisk.FileName

   $row.HardDisk = $HardDisk.Name

   $row.CapacityGB = ("{0:f1}" -f ($HardDisk.CapacityKB/1MB))

   # $row.DiskFreespace = $row.CapacityGB - ($_.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum)

   $row.DiskFreespace = $vm.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum

   $row.DiskType = $HardDisk.DiskType

   $row.TotalVMFSConsumed = $vm.UsedSpaceGB

   $row.ProvisionType = $HardDisk.StorageFormat

   $report += $row

  }

}

$report | Export-Csv -Path C:\users\sguptaadmin\Desktop\VMDisk-CapacityReport1.csv -NoTypeInformation -UseCultur

Reply
0 Kudos
LucD
Leadership
Leadership

You're passing a string instead of a VirtualMachine object.

Try changing that line to

foreach($vm in (Get-VM -Name (Get-Content -Path C:\TEMP\vmliste.txt)))


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

Reply
0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

I have corrected the script and below changes has been made and , it is working now. rest, all code is same,

foreach($vmName in (Get-Content -Path C:\TEMP\vmliste.txt))

    {

$vm = Get-VM -Name $vmName

   Get-HardDisk -VM $vm | ForEach-Object {

=======================================

Just one more help needed . I want to fetch custom attributes as wee for each VM, could you please let me know, what can be added in script,

Currently, i am using below property  but it is fetching all the fileld of custome attributes but i need only "Department" and "Use case "  column details only.

=====================

select -ExpandProperty Customfields

================

required_col.PNG

Reply
0 Kudos
LucD
Leadership
Leadership

You could do like this.
Don't forget to add the 'Description' on the Select at the start of the foreach loop.

$row.Description = (Get-Annotation -CustomAttribute 'Description' -Entity $vm).Value


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

Reply
0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

Thank you very much for help.

Just need one more help, if i want to use the "cluster_name"  instead of providing list of VM's in other file. What changes do we need to made in script?

I was using below code, but it was not working , so i did manually pull the list of VM's in cluster and then made changes in code.

Could you please correct this

======================================================================

$report = @()

$vm = get-cluster <cluster_name> | Get-VM | select Name                               //This is the first logic but it is not working

foreach($vm in (get-cluster <cluster_name> | Get-VM | select Name))             //This is second logic but it is not working

{

$vm = Get-VM -Name $vmName

   Get-HardDisk -VM $vm | ForEach-Object {

   $HardDisk = $_

=========================================================================

Reply
0 Kudos
LucD
Leadership
Leadership

You don't need a Select-Object on there, just do

$report = @()

foreach($vm in (Get-Cluster <cluster_name> | Get-VM))

{

   Get-HardDisk -VM $vm | ForEach-Object {


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

Reply
0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

Thanks for help, Working like charm Smiley Happy

Reply
0 Kudos
sanjaygpt171
Enthusiast
Enthusiast

I am facing one more issue while running script. If a VM has 2 hard-disks attached, it is showing correct free space for one HDD only and for other it is copying the old value.

Could you please correct error

pastedImage_0.png

Below is the script , i am using

$report = @()

foreach($vmName in (Get-Content -Path C:\TEMP\vmliste.txt))

{

$vm = Get-VM -Name $vmName

   Get-HardDisk -VM $vm | ForEach-Object {

   $HardDisk = $_

   $row = "" | Select Hostname, VM, GuestName, Datastore, VMXpath, HardDisk, DiskType, CapacityGB, DiskFreespace, TotalVMFSConsumed, ProvisionType, Department, Use_Case

   $row.Hostname = $vm.VMHost.Name

   $row.VM = $VM.Name

   $row.GuestName = $vm.Guest.HostName

   $row.Datastore = $HardDisk.Filename.Split("]")[0].TrimStart("[")

   $row.VMXpath = $HardDisk.FileName

   $row.HardDisk = $HardDisk.Name

   $row.CapacityGB = ("{0:f1}" -f ($HardDisk.CapacityKB/1MB))

   # $row.DiskFreespace = $row.CapacityGB - ($_.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum)

   $row.DiskFreespace = $vm.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum

   $row.DiskType = $HardDisk.DiskType

   $row.TotalVMFSConsumed = $vm.UsedSpaceGB

   $row.ProvisionType = $HardDisk.StorageFormat

   $row.Department = (Get-Annotation -CustomAttribute 'Department' -Entity $vm).Value

   $row.Use_Case = (Get-Annotation -CustomAttribute 'Use Case' -Entity $vm).Value

   $report += $row

  }

}

$report | Export-Csv -Path C:\users\sguptaadmin\Desktop\VMDisk-CapacityReport1.csv -NoTypeInformation -UseCultur

Reply
0 Kudos
LucD
Leadership
Leadership

The DiskFreeSpace in that report comes from adding all the free space on the guest OS drives together.
That is reported by VMware Tools.
What you want now is a link between VMDK and guest OS drives, and I'm afraid there is no fool-proof method (for now) to make that link.


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

Reply
0 Kudos