«

»

Mar 14 2013

Mapping Replicas to View Desktop Pools

Identifying what replica is assigned to what desktop pool can be somewhat challenging in Horizon View. However, in some cases administrators need to know the object relationship to properly plan desktop and pool migrations across hosts or datastores.

The PowerShell script bellow produces a output that demonstrate this relationship.
  
Screen Shot 2013-03-14 at 2.04.33 PM
  
To execute save the script below as a .PS1 file in a View Connection Server with the VMware.VimAutomation.Core plug-in installed. The VMware.VimAutomation.Core plug-in is automatically installed with the VMware vSphere™ PowerCLI. Before executing the script you will need to change the vCenter server address at

 <p>Connect-VIServer -Server vsphere5.company.com

  
Here is the script:

Add-PSSnapin VMware.VimAutomation.Core

function Get-LinkedClone {
 #The following line is a fast replacement for: $vms = get-vm args[0] | get-view
 if( $args[0] -eq $null ) {
 # $vms = Get-View -ViewType VirtualMachine -Property Name,Summary,Config.Hardware.Device
 $vms = Get-View -ViewType VirtualMachine
 } else {
 $vms = Get-View -ViewType VirtualMachine -Property Name,Summary,Config.Hardware.Device, -Filter @{Name = $args[0]}
 }

 $linkedClones = @()
 foreach ($vm in $vms) {
 Write-Host "The name of the vm is " $vm.name
 $unshared = $vm.Summary.Storage.Unshared
 $committed = $vm.Summary.Storage.Committed
 $uncommitted=$vm.Summary.Storage.Uncommitted
 $provisioned= ($vm.Summary.Storage.Commited + $vm.Summary.Storage.Uncommitted)

 $ftInfo = $vm.Summary.Config.FtInfo

 # This identifies if it is thin provisioned.
 if ( ($unshared -ne $committed) -and (($ftInfo -eq $null) -or ($ftInfo.InstanceUuids.Length -le 1)) ){
 # then $vm is a linked clone.
 # Find $vm's base disks.
 $baseDisks = @()
 foreach ($d in $vm.Config.Hardware.Device) {
 $backing = $d.backing
 if ($backing -is [VMware.Vim.VirtualDeviceFileBackingInfo] -and $backing.parent -ne $null) {
 do {
 $backing = $backing.parent
 } until ($backing.parent -eq $null)
 $baseDisks += $backing.fileName
 }
 }

 $isReplica=$vm.name.Contains("replica-")

 if (($baseDisks -ne $null) -and -not $isReplica)
 {
 # $replica=$baseDisks[0].Substring($baseDisks[0].IndexOf("]") +2 , $baseDisks[0].IndexOf("/"))
 $slashpos=$baseDisks[0].IndexOf("/")
 $replicaDisk=$baseDisks[0].Substring(0, $slashpos)
 $rbracketpos=$replicaDisk.IndexOf("]") +1
 $replica=$replicaDisk.Substring($rbracketpos).Trim()

 #$VMName = $vm | select -expandproperty name
 #$VMUnshared = $vm | select -expandproperty storage | select -expandproperty perdatastoreusage | select -expandproperty Unshared
 #--- Get the disk space assigned

 Write-Host "the VM " $vm.name " has this many disks" $vm.Guest.disk.length
 $totalDiskSpace = 0
 $diskSpaceAssigned = 0

 for ($iCount = 1;$iCount -le $vm.Guest.Disk.Count; $iCount++)
 {
 $index=$iCount-1
 $diskSpaceAssigned = [math]::Round($vm.Guest.Disk[$index].Capacity / 1GB)
 #$Details | Add-Member -Name "Disk$($DiskNum)FreeSpace(GB)" -MemberType NoteProperty -Value ([math]::Round($disk.FreeSpace / 1GB))
 $totalDiskSpace=$totalDiskSpace + $diskSpaceAssigned
 }
 $GuestTotalDiskSpace = $totalDiskSpace
 $uncommittedGB= [math]::round($uncommitted/1GB,2)
 $VMUnsharedGB = [math]::round($unshared/1GB,2)
 # $VMUsed = $vm | select -expandproperty storage | select -expandproperty perdatastoreusage | select -expandproperty Committed
 $VMUsedGB = [math]::round($committed/1GB,2)
 $VMprovisionedGB=[math]::round($provisioned/1GB,2)
 $linkedClone = new-object PSObject
 $linkedClone | add-member -type NoteProperty -Name Name -Value $vm.name
 $linkedClone | add-member -type NoteProperty -Name Parent -Value $replica

 $linkedClone | add-member -type NoteProperty -Name DiskCount -Value $vm.Guest.Disk.Count

 #$linkedClone | add-member -type NoteProperty -Name BaseDisks -Value $baseDisks
 #$linkedClone | add-member -type NoteProperty -Name DiskProvisionedGB -Value $VMprovisionedGB

 $linkedClone | add-member -type NoteProperty -Name UniqueDiskUsedGB -Value $VMUnsharedGB
 $linkedClone | add-member -type NoteProperty -Name VMCombinedStorageGB -Value $VMUsedGB
 $linkedClone | add-member -type NoteProperty -Name GuestTotalDiskSpace -Value $GuestTotalDiskSpace

 $linkedClones += $linkedClone
 }
 #else { do nothing for VMs that are not linked clones }
 }
 else
 {

 Write-Host $vm.Name "Not a Linked Clone" -ForegroundColor 'Red'
 }
 $stan = $linkedClones | sort Name, DiskCount, VMCombinedStorageGB, UsedStorageGB, GuestTotalDiskSpace
 $stan | ConvertTo-Html > c:\temp\output.html
 }
}

Connect-VIServer -Server vsphere5.company.com
Get-LinkedClone</pre>

  
Thanks VMware EUC Senior Consultant Hans Bader for sharing the script with us. However, please not that I have not heavily tested the script and if you find any issues or want to improve it please share with us.
  
This article was first published by Andre Leibovici (@andreleibovici) at myvirtualcloud.net.  

Similar Posts:

Permanent link to this article: http://myvirtualcloud.net/?p=4782

1 comment

  1. Norbert

    love this script, thanks alot :)

Leave a Reply