Tuesday, December 11, 2012

Find Orphaned Replica's in VMware View

One of my little gripes with View is that is can be difficult to determine which replica's are related to which linked clones, and whether they are orphaned or not. I had to help a customer with an array migration recently, and we had a little pain to easily work out exactly which replica's we could un-protect and remove safely. So I knocked up the script below to help.

Find-OrphanedReplica.ps1 will traverse through all the VMs in the inventory, find the replica VMs, and then determine which (if any) are linked to that replica. It uses the '.ExtensionData.LayoutEx.File' properties of the vm to make a (pretty good) guess as to which replica is associated with.

The script will spit out the list of replica VMs that do not appear to have any clones linked to them. If you look at the code, you will also notice that the hash table ($ReplicaList) I store everything up in also stores which linked clones are associated with a given replica, so you could easily add a statement at the end to display which linked clones are attached to the replicas.

So a few disclaimers; replica's are not scum, as the picture above describes :) and this script is not an absolute guide that the replica is not used, or referenced anywhere. It is a quick tool to help you identify replicas that should be investigated further. Or as a handy way to map out which clones link to which replica's.

$VMList = @()
$ReplicaList = @{}
$VMtoReplica = @{}

$AllVMs = Get-VM

foreach ($Replica in ($AllVMs | where {$_.Name -like "replica*"})){
 $ReplicaShort = $Replica.ToString().Substring(0,30)

foreach ($VM in ($AllVMs | where {$_.Name -notlike "*replica*"})){
 foreach ($diskfile in $VM.ExtensionData.LayoutEx.file){
  if ($diskfile.Name -like "*replica*" -and $diskfile.Name -notlike "*flat*" ){

foreach ($item in $VMtoReplica.get_Keys()){
 $VMList = @($item)
 $VMList += @($ReplicaList.get_Item($VMtoReplica.get_Item($item)))
 $VMList = @()

foreach ($Replica in $ReplicaList.get_Keys()){
 if ($ReplicaList.get_Item($Replica) -eq ""){
  $VM = $AllVMs | where {$_.Name -like ($Replica.Insert(30,"*"))}
  Write-Host $VM.Name does not appear to have any linked clones attached.


Chris Hampton said...

I am attempting to run your script but I am getting the following error referencing the line of code below:

Exception calling Substring with 2 arguments: Index and length must refer to a location within the string.
Parameter name: length"

Code reference: $VMtoReplica.set_Item($VM.Name,$diskfile.Name.Substring($diskfile.Name.IndexOf("/")+1,30))

Chris Hampton

David O'Connor said...

looks like it is matching something weird, that is not 30 chars long...can you print out the things it is matching by adding this to line 15 from above


so then the $VMtoReplica.set_item is pushed to line 16.

then we can see the matches.

Chris Hampton said...

Ok, I made the change you suggested. The error message does not appear now and I get the following output displayed:

[vx4495_VDI3_Replica_Lun21] replica-cbca0a4d-5ec2-4c40-9f5d-0ba660ab7687/replica-cbca0a4d-5ec2-4c40-9f5d-0ba660ab7687.vmdk

This is one example line of the output. At the end of this output list, I still see the listing of the replica does not appear to have any listed clones attached.

Anonymous said...

Great Job, Thanks


blogs I read


My photo

Enjoys spending time in layer 6 & 7. Passionate about delivering quality education.
Based in Sydney.