Function to export VMs with thin provisioned disks to CSV file

I wanted to know how many disks in our environment are thin provisioned, so I wrote a quick function to export that list to a CSV file.

I have it pull the VM name, vmdk path & name, Size in GB, and if it’s Thin Provisioned (Boolean, which should always be true).


Just copy & paste this into a .ps1 file and run it. The initial instantiation of $vms takes a while, depending on the size of your environment, but the rest goes quickly. Make sure you’re connected to a host or vcenter before you run it, or you won’t get anything.

function find-thin{
    write-host -fore green `n "getting all VMs, this may take a while"

    $vms = get-vm |sort name |get-view

    Write-host -fore green `n "Starting Scan"

    $vmdks = @()

    foreach ($vm in $vms){
        foreach ($device in $vm.config.hardware.Device){
            if($device.GetType().Name -eq "VirtualDisk"){
                if($device.Backing.ThinProvisioned){
                    $info = "" | Select VM, File, SizeInGB, Thin
                    $info.VM = $vm.name
                    $info.File = $device.backing.filename
                    $info.SizeInGB = $device.capacityinkb/1048576
                    $info.thin = $device.Backing.ThinProvisioned
                    $vmdks += $info
                }
            }
        }
    }

    write-host -fore green `n "finished searching all VMs" `n

    $vmdks | export-csv d:\thindisk.csv
}

find-thin

To change where the file is saved, simply change the path in line 27 to the destination of your choice.

This entry was posted in Scripts, Virtualization and tagged , , , by Luke. Bookmark the permalink.

About Luke

Hello! My name is Luke Huckaba and I'm an addict. My drug of choice is technology and I'm far from sober. I'm currently a Systems Specialist and specialize in anything Windows-related, Dell server hardware, and Virtualization using VMWare products. In my spare time, when not toying with the above mentioned items, I can be found riding my motorcycle, working on cars, and attempting to play softball. Feel free to shoot me an e-mail with any questions you may have.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>