Correcting VMDK paths

I found a bunch of VMs recently which had an incorrect path to the VMDK files – they were still working as the VMDKs were in the VM’s home directory, and the hypervisor seems to check there if it can’t find them where the vmx file thinks they should be.

However, while they would run, things like VMotion wouldn’t work, so they needed correcting.

A bit of googling found this post which was a good starting point. A little rework later and I had the following code:

$VMs = Get-ResourcePool "ResPool" | Get-VM

foreach ($VM in $VMs){
$Datastore = Get-Datastore -VM $VM
$HDDs = Get-Harddisk -VM $VM

foreach ($HDD in $HDDs){
$HDDname = $HDD.Filename
$HDDsNames = $HDDname.Split("/")
$count = $HDDsNames.count
if ($count -gt 3){
$VMDKName = $HDDsNames[$count-1]

$DiskPath = "[" + $Datastore.Name + "] " + $VM.Name + "/" + $VMDKName

Remove-HardDisk -HardDisk $HDD -Confirm:$false
New-HardDisk -VM $VM -DiskPath $DiskPath
}
}

}

This just enumerates the VMs in a resource pool, checks the VMDK path to see if it has more than 3 ‘/’ in it, and if it does, it rewrites it in a correct format.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s