I want powerCLI script get a list of VMs, Attributes (Team and use case) , disk size andused disk space
What do you already have?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I have access to vcenter but need to pull the information mentioned above. It's having 600 VM's
I meant what script do you already have?
What is missing?
What is not working?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
And what is the problem you are encountering with that script?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
Not sure why you are using a method here, just refer to the property.
Something like this
# $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
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.
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
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:
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
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
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
================
You could do like this.
Don't forget to add the 'Description' on the Select at the start of the foreach loop.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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 = $_
=========================================================================
You don't need a Select-Object on there, just do
foreach($vm in (Get-Cluster <cluster_name> | Get-VM))
{
Get-HardDisk -VM $vm | ForEach-Object {
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for help, Working like charm
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
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
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