VMware Cloud Community
Squigglymonkey
Enthusiast
Enthusiast

script help with getting Memory, Processors and hard disk (allocated, freespace) info

I found a script in another discussion here, and modified(removed what i didn't need) and it pulls the disk information, but I see that it lists all the drives, their correct allocated size, but it shows the same value for used space. What have I got wrong?

 

 

 

$report = @()
Foreach($VM in Get-VM){
Get-HardDisk -VM $vm | ForEach-Object {
   $HardDisk = $_
   $row = "" | Select VM, HardDisk, CapacityGB, DiskFreespace
   $row.VM = $VM.Name
   $row.HardDisk = $HardDisk.Name
   $row.CapacityGB = ("{0:f1}" -f ($HardDisk.CapacityKB/1MB))
   $row.DiskFreespace = $vm.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum
   $report += $row
   }
}
$report | Export-Csv -Path .\VMDisk-CapacityReport.csv -NoTypeInformation -UseCulture

 

 

out put looks like this:

"server05","Hard disk 1","40.0","15.7100372314453"
"server05","Hard disk 2","15.0","15.7100372314453"
"server05","Hard disk 3","32.0","15.7100372314453"



Reply
0 Kudos
12 Replies
LucD
Leadership
Leadership

You are combining the harddisks as the hypervisor sees them (Get-Harddisk) with the harddisks as the Guest OS sees them.
These are not the same.

Also, you are using 

$vm.Guest.Disks 

which is all the harddisks the Guest OS sees, not that specific harddisk as returned by Get-Harddisk.

I wonder which script you took this from?


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

Reply
0 Kudos
Squigglymonkey
Enthusiast
Enthusiast

https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-VM-Disk-Usage/td-p/1409959

If i don't change it at all, and use the one listed as answer it does the same thing.

Reply
0 Kudos
LucD
Leadership
Leadership

Yes, that script just lists the same total free space, as seen inside the GuestOS, for each harddisk from Get-Harddisk.


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

Reply
0 Kudos
kwhornlcs
Enthusiast
Enthusiast

Doesn't give the VMDK name/mapping, but does this do what you're looking for: [UPDATED - the sort path was in the wrong context]

 

 

$report = @()
Foreach($VM in Get-VM){
(Get-VMGuest $vm).Disks | Sort Path | ForEach-Object {
   $row = "" | Select VM, Path, CapacityGB, Freespace
   $row.VM = $vm.Name
   $row.Path = $_.Path
   $row.CapacityGB = [Math]::Round($_.CapacityGB, 2)
   $row.Freespace = [Math]::Round($_.FreeSpaceGB, 2)
   $report += $row
   }
}
$report | Export-Csv -Path .\VMDisk-CapacityReport.csv -NoTypeInformation -UseCulture

 

 

 

Squigglymonkey
Enthusiast
Enthusiast

Thank you, that has the right freespace vale, but does not have the Disk number. 

This script will get automated and sent to server owners to keep track of thier space and request more when they think they need it.
So i'd like them to be able to send the disk # when they request space allocated.

 

Reply
0 Kudos
kwhornlcs
Enthusiast
Enthusiast

Unfortunately mapping disk path to get what the guest OS sees as free space to the VMDK is not always simple or reliable. This seems to be about as close as I could get it. It does take into account there could be multiple volumes on each vmdk - for me this was more prevelant of *nix OSs. And reports back where the volume mapping is unavailable, which seems to be a quirk more than a problem.

Speaking of which, @LucD - When using the Get-VMGuestDisk by HardDisk parameter I would sometimes get "Guest mapping is not available for vm xxxxx" - while others I'd get no problem...in most cases it was a powered off VM, but in at least one case it was different results against VMs that were powered on with the same HW compatibility, VMtools versions and same OS - any ideas?

 

$report = @()
Foreach($VM in Get-VM){
    If (Get-HardDisk -VM $VM | Get-VMGuestDisk) {
        $vmdkList = Get-HardDisk -VM $VM
        ForEach ($vmdk in $vmdkList){
            $GuestDisk = Get-VMGuestDisk -HardDisk $vmdk
            $GuestDisk | ForEach-Object {
                $row = "" | Select VM, vmdk, Path, CapacityGB, Freespace
                $row.VM = $vm.Name
                $row.vmdk = $vmdk.Name
                $row.Path = $_.DiskPath
                $row.CapacityGB = [Math]::Round($_.CapacityGB, 2)
                $row.Freespace = [Math]::Round($_.FreeSpaceGB, 2)
                $report += $row
            }
        }
    } Else {
        $GuestDisk = Get-VMGuestDisk -VM $VM
        $GuestDisk | ForEach-Object {
                $row = "" | Select VM, vmdk, Path, CapacityGB, Freespace
                $row.VM = $vm.Name
                $row.vmdk = "Mapping N/A"
                $row.Path = $_.DiskPath
                $row.CapacityGB = [Math]::Round($_.CapacityGB, 2)
                $row.Freespace = [Math]::Round($_.FreeSpaceGB, 2)
                $report += $row
        }
    }
}
$report | Sort VM,vmdk,Path | Export-Csv -Path .\VMDisk-CapacityReport.csv -NoTypeInformation -UseCulture

 

 

Reply
0 Kudos
LucD
Leadership
Leadership

Perhaps it is something like #issue404 on Open VM Tools?

Cody did a great post on that, see What’s New in vSphere 7.0 Storage Part III: GuestInfo VirtualDiskMapping Linux, PowerCLI support




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

Reply
0 Kudos
Squigglymonkey
Enthusiast
Enthusiast

Yes, this gets the drive info.  Is there a way to also have the numcpu and memorygb (from the get-vm) also listed with the drives?

 

 

$report = @()
Foreach($VM in Get-VM){
(Get-VMGuest $vm).Disks | Sort Path | ForEach-Object {
   $row = "" | Select VM, Path, CapacityGB, Freespace
   $row.VM = $vm.Name
   $row.Path = $_.Path
   $row.CapacityGB = [Math]::Round($_.CapacityGB, 2)
   $row.Freespace = [Math]::Round($_.FreeSpaceGB, 2)
   $report += $row
   }
}
$report | Export-Csv -Path .\VMDisk-CapacityReport.csv -NoTypeInformation -UseCulture
VMPathCapacityGBFreespaceCPUMemory
server123C:\39.6612.5648
Reply
0 Kudos
Squigglymonkey
Enthusiast
Enthusiast

Can get-harddisk list free or used space? Can those values be pulled from anywhere else beside the guest?

Reply
0 Kudos
LucD
Leadership
Leadership

As I explained before, Get-Harddisk returns the vDisk info as the hypervisor sees it.
What the Guest OS puts on those vDisks, 1 or more partitions, is not visible to the hypervisor.
The only way to get the Guest OS disk-related info is through Get-VMGuestDisk, which uses the VMware Tools to transfer that info.


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

kwhornlcs
Enthusiast
Enthusiast

Memory and CPU are pretty easy - just additional properties from the Get-VM, see below - this will give the VMDK hard disk number as well when it is available:

$report = @()
Foreach($vm in Get-VM){
    If (Get-HardDisk -VM $vm | Get-VMGuestDisk) {
        $VMDKS = Get-HardDisk -VM $vm
        ForEach ($VMDK in $VMDKS){
            $GuestDisk = Get-VMGuestDisk -HardDisk $VMDK
            $GuestDisk | ForEach-Object {
                $row = "" | Select VM, memory, vcpu, vmdk, Path, CapacityGB, Freespace
                $row.VM = $vm.Name
                $row.memory = $vm.MemoryGB
                $row.vcpu = $vm.NumCpu
                $row.vmdk = $VMDK.Name
                $row.Path = $_.DiskPath
                $row.CapacityGB = [Math]::Round($_.CapacityGB, 2)
                $row.Freespace = [Math]::Round($_.FreeSpaceGB, 2)
                $report += $row
            }
        }
    } Else {
        $GuestDisk = Get-VMGuestDisk -VM $vm
        $GuestDisk | ForEach-Object {
                $row = "" | Select VM, memory, vcpu, vmdk, Path, CapacityGB, Freespace
                $row.VM = $vm.Name
                $row.memory = $vm.MemoryGB
                $row.vcpu = $vm.NumCpu
                $row.vmdk = "Mapping N/A"
                $row.Path = $_.DiskPath
                $row.CapacityGB = [Math]::Round($_.CapacityGB, 2)
                $row.Freespace = [Math]::Round($_.FreeSpaceGB, 2)
                $report += $row
        }
    }
}
$report | Sort VM,vmdk,Path| Export-Csv -Path .\VMDisk-CapacityReport.csv -NoTypeInformation -UseCulture

 

Reply
0 Kudos
Squigglymonkey
Enthusiast
Enthusiast

Shoot. We don't have Vsphere 7.0 😞

Reply
0 Kudos