Contents

Script Dumpster: Reporting all files on ESX Datastores


Contents

The Problem

I had a customer who was complaining that the simple overview of disks used by VM’s didn’t add up to the amount of data in use on datastores. Simple reasoning being swap files, snapshots etc. etc. , but the customer wanted to see exactly what was causing this…

In comes PowerCLI [VMWare’s adaption of PowerShell for their solutions]

The Script

Edit: As I had thought at first instance, the datacenter name was working just fine on my 2 test environments, but in a 3rd I quickly ran into an error.

I’ve now adapted the script to automatically check all available DataCenters and underlying DataStores 🙂

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
$ESXServer = 'ServerName'
$ESX_Cred = Get-Credential

Connect-VIServer $ESXServer -Credential $ESX_Cred

Set-Location vmstore:
$Datacenters = Get-ChildItem
$AllFiles = @()


foreach ($Datacenter in $Datacenters){
    $DataStores = Get-ChildItem

        foreach ($DataStore in $DataStores) {
            $Files = Get-ChildItem -Path $DataStore -Recurse | Where-Object { -not $_.PsIsContainer}
            $AllFiles += $Files
        }
}

$AllFiles | Select-Object DataStoreFullPath,Name,ItemType,@{L='LengthMB';E={ "{0:N2}" -f ($_.Length / 1MB)}},LastWriteTime

If required you can simply export the outcome using

1
Export-CSV

Happy Scripting! 🙂