Isolating Stale VMDKs

Posted by

Step 1: Generate a list of vmdk associated with the VMs

In this step, we will generate a list of VMDKs associated with all the VMs registered on a vCenter server.

Save the following code as a PowerShell script

Script 1:

$VmInfo = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {
ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {
ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {
"" | Select-Object -Property @{N="VM";E={$VM.Name}},
@{N="Datacenter";E={$Datacenter.name}},
@{N="Cluster";E={$Cluster.Name}},
@{N="Hard Disk";E={$HardDisk.Name}},
@{N="Datastore";E={$HardDisk.FileName.Split("]")[0].TrimStart("[")}},
@{N="VMConfigFile";E={$VM.ExtensionData.Config.Files.VmPathName}},
@{N="VMDKpath";E={$HardDisk.FileName}}
}
}
}
}
$VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "VmInfo.csv"

Connect to the vCenter server using following command

Connect-VIServer -Server <vCenter server FQDN> -User <vCenter server admin user> -Password <Password for the user>

Execute the script 1

The execution of the above script will generate VmInfo.csv at the path of execution.

Step 2: Generate a list of vmdks associated with datastores

In this step, we will generate a list of VMDKs associated with all the datastores registered on a vCenter server.

Save the following code as a PowerShell script

Script 2:

$Datacenter = ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {
ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {
ForEach ($VMhost in ($Cluster | Get-VMhost | Sort-Object -Property Name)) {
$datastores = Get-Datastore -VMHost $VMHost| Sort-Object -Property Name
foreach ($datastore in ($datastores)) {
write-host $datastore
Get-ChildItem -Recurse -Path vmstores:\ -Include *vmdk* | select Name,DatastoreFullPath,LastWriteTime | Export-Csv -NoTypeInformation -UseCulture -Path vmdkinfo.csv
}
}
}
}

Execute the Script 2

The execution of the above script will generate vmdkinfo.csv at the path of execution.

Step 3: Compare the outputs from Two scripts executed

Sample output of Script 1

VMDatacenterClusterHard DiskDatastoreVMConfigFileVMDKpath
TestVMSyed DatacenterNazeer ClusterHard disk 1datastore1[datastore1] TestVM/TestVM.vmx[datastore1] TestVM/TestVM.vmdk
win7-SP1-X64Syed DatacenterNazeer ClusterHard disk 1datastore1 (1)[datastore1 (1)] win7-SP1-X64/win7-SP1-X64.vmx[datastore1 (1)] win7-SP1-X64/win7-SP1-X64.vmdk
W2K8-ENT-x64Syed DatacenterSyed ClusterHard disk 1datastore1 (2)[datastore1 (2)] W2K8-ENT-x64/W2K8-ENT-x64.vmx[datastore1 (2)] W2K8-ENT-x64/W2K8-ENT-x64.vmdk

Sample output of Script 2

NameDatastoreFullPathLastWriteTime
win7-SP1-X64.vmdk[datastore1 (1)] win7-SP1-X64/win7-SP1-X64.vmdk4/1/2016 4:24
win7-SP1-X64-flat.vmdk[datastore1 (1)] win7-SP1-X64/win7-SP1-X64-flat.vmdk4/5/2016 12:06
TempVM.vmdk[datastore1] TempVM/TempVM.vmdk3/26/2016 14:52
TempVM-flat.vmdk[datastore1] TempVM/TempVM-flat.vmdk3/26/2016 15:01
TestVM.vmdk[datastore1] TestVM/TestVM.vmdk4/1/2016 15:16
TestVM-flat.vmdk[datastore1] TestVM/TestVM-flat.vmdk4/2/2016 3:13
Win7-SP1-X86-disk1.vmdk[datastore1] Win7-SP1-X86/Win7-SP1-X86-disk1.vmdk4/28/2016 10:07
W2K8-ENT-x64.vmdk[datastore1 (2)] W2K8-ENT-x64/W2K8-ENT-x64.vmdk4/1/2016 4:33
W2K8-ENT-x64-flat.vmdk[datastore1 (2)] W2K8-ENT-x64/W2K8-ENT-x64-flat.vmdk4/28/2016 10:03

You can use excel vlookup function to compare the files. Please note that each “VMDKpath” will result in at least two “DatastoreFullPath”.

Attachment:

A sample excel sheet with vlookup example is attached. You see that TEMPVM.VMDK and TempVM-flat.vmdk are not associated with any VM in the inventory.