VMware Cloud Community
Munster
Contributor
Contributor

Help with a 'vm disks' script

Dear all

I've attached my script (which i've taken from here and there and changed around) to try and get some info about our disks within our estate. Still 'wet behind the ears' regarding powershell i'm still trying to get to grasps with scripting as a whole. The problem i'm having is the script works fine EXCEPT for the entry where i'm trying to request FREESPACE. It writes ALL the details to the csv file fine but everytime I have a loop within a loop I always am at a loss as it never outputs the correct data.

Could someone help PLEEEEASE? And more importantly give me some pointers (or weblinks) as to what to do when I come across a situation where i would like some info outputted as shown in my script but I have a nested loop within a loop scenario ?!??!

As always Many thanks for your help in advance

Munster (script below)

# VARIABLES
$outputFile = 'C:\scripts\results\VMDiskFormat.csv'       # Output file path

# SCRIPT
$myCol = @()                                            # Prepare output collection
$VMs = Get-VM | Sort Name                                  # Get all VMs (sorted)
$counter = 0                                            # Initialize counter for progress bar

Foreach ($vm in $vms){
    $counter++                                            # Increase counter for progress bar
    Write-Progress -Activity "Gathering disk information" -Status "Processing VM $VM" -PercentComplete (100*($counter/$VMs.count))   # Display progress bar
    $VMView = $VM | Get-View
                                                       
    ForEach ($DISK in $VM.HardDisks)  {                  # Loop through VM's harddisks
           
        $myObj = "" |                                     # Create new object
        Select VM, StorageFormat, Capacity, Freespace, Filename, Persistence, TotNumVirtualDisks
        $myObj.VM                     = $VM.Name                # Virtual Machine Name
        $myObj.StorageFormat         = $DISK.StorageFormat     # VM Disk Format
        $myObj.Capacity             = ( "{0:n2}" -f ($DISK.Capacitykb/1mb))# VM Capacity per disk
       $myObj.Freespace             = ( "{0:n2}" -f (($VMView.guest.disk).freespace/1mb))# VM Freespace per disk
        $myObj.Filename             = $DISK.FileName        # Virtual Disk FileName
        $myObj.Persistence             = $DISK.Persistence        # Persistent Disk
        $myObj.TotNumVirtualDisks     = ($VMView.summary.config.numvirtualdisks)
               
        $myCol += $myObj                                # Add output to collection
    }

}
$myCol | Export-Csv $outputFile -NoTypeInformation        # Export output to csv
Invoke-Item $outputFile                                    # Launch output file

0 Kudos
3 Replies
Zsoldier
Expert
Expert

It looks like the problem is with this line:

$myObj.Freespace             = ( "{0:n2}" -f (($VMView.guest.disk).freespace/1mb))# VM Freespace per disk

No where do you specify which disk object that you want to work against.  This will likely return data when only one object is returned.  You need a where statement here to match the current disk you are working against.  Something like this:

$myObj.Freespace             = ( "{0:n2}" -f (($VMView.guest.disk | where {$_.name -eq $Disk.Name}).freespace/1mb))# VM Freespace per disk

Play around with it to see what you can come up with.

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
0 Kudos
LucD
Leadership
Leadership

I'm afraid that will not work.

The $DISK.Name property will have the name of the virtual disk, something like "Hard disk 1" for example.

While the $VMView.guest.disk entries have no Name property but a DiskPath property which is the path used inside the OS running on the guest, something like "C:\" for example.

You are trying to make a link between virtual disks and OS partitions.

That isn't that straightforward

And don't forget, a virtual disk ("Hard disk 1") can hold multiple partitions ("C:\","D:\").


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

0 Kudos
Munster
Contributor
Contributor

Your right LucD ! I noticed that and thought I was doing something wrong.

I've come across this scenario in some of my other scripts where I can create bespoke objects (that i can output to csv very easily) for a specific subset (in this case $vm.harddisks) only to find that ALL the data i require is not under that subset (in this case freespace).

Does anyone know how I could get the freespace value by  any chance ???? if someone could even point me to some scripts they've seen that might show how i can work this out that would be a great place to start

0 Kudos