Contributor
Contributor

VMs Disk Usage Report

I got a script that work but want to include a subtotal for each VM and not sure how to insert/add a subtotal line at end of each VM.  The output report will be in CSV.

Any help will be greatly appreciated.

$FinalResult = @() 

$ALLvms = Get-View -ViewType "VirtualMachine"

$allvms | foreach-object {

       $vm = $_

       $vmname = $vm.name

       $Drives = $vm.Guest.Disk

       $drives  | foreach-object {

            $driveC = $_

            $object = New-Object -TypeName PSObject

            $Capacity = "{0:N0}" -f [math]::Round($DriveC.Capacity / 1MB) 

            $Freespace = "{0:N0}" -f [math]::Round($DriveC.FreeSpace / 1MB)

            $Percent = [math]::Round(($FreeSpace)/ ($Capacity) * 100)

            $PercentFree = "{0:P0}" -f ($Percent/100)

            $object | Add-Member -MemberType NoteProperty -Name "Server Name" -Value $vmname

            $object | Add-Member -MemberType NoteProperty -Name Disk -Value $Drivec.DiskPath

            $object | Add-Member -MemberType NoteProperty -Name "Capacity MB" -Value $Capacity

            $object | Add-Member -MemberType NoteProperty -Name "Free MB" -Value $FreeSpace

            $object | Add-Member -MemberType NoteProperty -Name "Free %" -Value $PercentFree

            $finalResult += $object

        }  

    }

$finalResult | Export-Csv "C:\DiskUsageReport.csv" -NoTypeInformation  -UseCulture    # dump the report to .csv

14 Replies
Leadership
Leadership

Try like this

$FinalResult = @()

foreach($vm in Get-View -ViewType "VirtualMachine" -Filter @{'Name'='stat'}){

       $totalCapacity = $totalFree = 0

       $vm.Guest.Disk | %{

            $object = New-Object -TypeName PSObject

            $Capacity = "{0:N0}" -f [math]::Round($_.Capacity / 1MB)

            $totalCapacity += $_.Capacity

            $totalFree += $_.FreeSpace

            $Freespace = "{0:N0}" -f [math]::Round($_.FreeSpace / 1MB)

            $Percent = [math]::Round(($FreeSpace)/ ($Capacity) * 100)

            $PercentFree = "{0:P0}" -f ($Percent/100)

            $object | Add-Member -MemberType NoteProperty -Name "Server Name" -Value $vm.Name

            $object | Add-Member -MemberType NoteProperty -Name Disk -Value $_.DiskPath

            $object | Add-Member -MemberType NoteProperty -Name "Capacity MB" -Value $Capacity

            $object | Add-Member -MemberType NoteProperty -Name "Free MB" -Value $FreeSpace

            $object | Add-Member -MemberType NoteProperty -Name "Free %" -Value $PercentFree

            $finalResult += $object

        }

        $object = New-Object -TypeName PSObject

        $object | Add-Member -MemberType NoteProperty -Name "Server Name" -Value $vm.Name

        $object | Add-Member -MemberType NoteProperty -Name Disk -Value 'SubTotal'

        $object | Add-Member -MemberType NoteProperty -Name "Capacity MB" -Value ("{0:N0}" -f ($totalCapacity/1MB))

        $object | Add-Member -MemberType NoteProperty -Name "Free MB" -Value ("{0:N0}" -f ($totalFree/1MB))

        $object | Add-Member -MemberType NoteProperty -Name "Free %" -Value ("{0:P0}" -f ($totalFree/$totalCapacity))

        $finalResult += $object

    }

$finalResult | Export-Csv "C:\DiskUsageReport.csv" -NoTypeInformation  -UseCulture    # dump the report to .csv


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

Contributor
Contributor

A big thanks to LucD for quick response!  I spent 2 days and stuck trying to get the result I wanted Smiley Sad and took LucD maybe 10 min.  I just started learning PowerShell/PowerCLI about a week and It shows.

Thanks again LucD!

0 Kudos
Contributor
Contributor

Big thanks to LucD

To add datastore name for each VMDK in VM

foreach($vm in Get-View -ViewType "VirtualMachine" -Filter @{'Name'='mailbox'}){

       $totalCapacity = $totalFree = 0

       $vm.Guest.Disk | %{

            $object = New-Object -TypeName PSObject

            $Capacity = "{0:N0}" -f [math]::Round($_.Capacity / 1GB)

            $totalCapacity += $_.Capacity

            $totalFree += $_.FreeSpace

            $Freespace = "{0:N0}" -f [math]::Round($_.FreeSpace / 1GB)

            $Percent = [math]::Round(($FreeSpace)/ ($Capacity) * 100)

            $PercentFree = "{0:P0}" -f ($Percent/100)

            $Datastore = Get-VM -Name $vm.Name | Get-HardDisk

            $object | Add-Member -MemberType NoteProperty -Name "Server Name" -Value $vm.Name

            $object | Add-Member -MemberType NoteProperty -Name Disk -Value $_.DiskPath

            $object | Add-Member -MemberType NoteProperty -Name "Capacity GB" -Value $Capacity

            $object | Add-Member -MemberType NoteProperty -Name "Free GB" -Value $FreeSpace

            $object | Add-Member -MemberType NoteProperty -Name "Free %" -Value $PercentFree

            $object | Add-Member -MemberType NoteProperty -Name "Datastore" -Value $Datastore.filename.split(']')[0].split('[')[1]

            $finalResult += $object

        }

        $object = New-Object -TypeName PSObject

        $object | Add-Member -MemberType NoteProperty -Name "Server Name" -Value $vm.Name

        $object | Add-Member -MemberType NoteProperty -Name Disk -Value 'SubTotal'

        $object | Add-Member -MemberType NoteProperty -Name "Capacity GB" -Value ("{0:N0}" -f ($totalCapacity/1GB))

        $object | Add-Member -MemberType NoteProperty -Name "Free GB" -Value ("{0:N0}" -f ($totalFree/1GB))

        $object | Add-Member -MemberType NoteProperty -Name "Free %" -Value ("{0:P0}" -f ($totalFree/$totalCapacity))

        $object | Add-Member -MemberType NoteProperty -Name "Datastore" -Value $Datastore

        $finalResult += $object

    }

But result are list only the first datastore name

0 Kudos
Leadership
Leadership

Mapping guest OS partitions to VMDKs and datastores is not obvious, and I haven't seen a foolproof algorithm yet.

In your specific case, are all VMDL sitting on the same datastore (VMDK affinity)?


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

0 Kudos
Contributor
Contributor

what is weird is that i can run the bottom script and get results, but not the other scripts. anecdotal comments 
0 Kudos
Contributor
Contributor

what is weird is that i can run the bottom script and get results, but not the other scripts. anecdotal comments 
0 Kudos
Leadership
Leadership

Not sure I understand what you are saying.

Can you perhaps give some run examples? And eventual messages?


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

0 Kudos
Expert
Expert

LucD,

I ran the below code, but the output is blank in CSV. Do i need to change anything ?

Thanks

V

0 Kudos
Leadership
Leadership

You did adapt the Filter on the Get-View cmdlet?


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

0 Kudos
Expert
Expert

LucD,

Are you referring the line  -Filter ?

foreach($vm in Get-View -ViewType "VirtualMachine" -Filter @{'Name'='stat'})

Thanks

V

Leadership
Leadership

Yes


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

0 Kudos
Expert
Expert

Yes, exactly same line i have used it, but blank output.

Thanks

V

0 Kudos
Leadership
Leadership

But do you have VMs that have the text 'stat' in their DisplayName?


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

0 Kudos
Expert
Expert

LucD,

It's my bad that i didn't notice. Also, instead of specific VM name starts with 'Stat', is it possible to get the usage for specific list of VM's. E.g. C:\temp\vmlist ?

Thanks in advance.

V

0 Kudos