|
| 1 | +<#PSScriptInfo |
| 2 | +.VERSION 1.0.0 |
| 3 | +.GUID 419ab591-3184-4e1a-a1f5-563d386c6f9b |
| 4 | +.AUTHOR William Lam |
| 5 | +.COMPANYNAME VMware |
| 6 | +.COPYRIGHT Copyright 2021, William Lam |
| 7 | +.TAGS VMware Clone |
| 8 | +.LICENSEURI |
| 9 | +.PROJECTURI https://github.com/lamw/vghetto-scripts/blob/master/powershell/VMCloneInfo.ps1 |
| 10 | +.ICONURI https://blogs.vmware.com/virtualblocks/files/2018/10/PowerCLI.png |
| 11 | +.EXTERNALMODULEDEPENDENCIES |
| 12 | +.REQUIREDSCRIPTS |
| 13 | +.EXTERNALSCRIPTDEPENDENCIES |
| 14 | +.RELEASENOTES |
| 15 | + 1.0.0 - Initial Release |
| 16 | +.PRIVATEDATA |
| 17 | +.DESCRIPTION This function retrieves cloning information about a given VM |
| 18 | +#> |
| 19 | + |
| 20 | +Function Get-VMCloneInfo { |
| 21 | + <# |
| 22 | + .NOTES |
| 23 | + =========================================================================== |
| 24 | + Created by: William Lam |
| 25 | + Organization: VMware |
| 26 | + Blog: www.virtuallyghetto.com |
| 27 | + Twitter: @lamw |
| 28 | + =========================================================================== |
| 29 | + .PARAMETER VMName |
| 30 | + The name of a VM to retrieve cloning information |
| 31 | + .EXAMPLE |
| 32 | + Get-VMCloneInfo -VMName "SourceVM" |
| 33 | + Get-VMCloneInfo -VMName "Full-Clone-VM" |
| 34 | + Get-VMCloneInfo -VMName "Linked-Clone-VM" |
| 35 | + Get-VMCloneInfo -VMName "Instant-Clone-VM" |
| 36 | + #> |
| 37 | + param( |
| 38 | + [Parameter(Mandatory = $true)][String]$VMName |
| 39 | + ) |
| 40 | + |
| 41 | + $clonedVM = Get-VM $VMName |
| 42 | + $clonedEvent = Get-VIEvent -Types Info -Entity $clonedVM | Where {($_.Vm.Name -eq $VMName) -and ($_.getType().name -eq "VmClonedEvent" -or $_.EventTypeId -eq "com.vmware.vc.VmInstantClonedEvent" -or $_.getType().name -eq "VmDeployedEvent")} | select -First 1 |
| 43 | + |
| 44 | + if($clonedEvent) { |
| 45 | + # Instant Clone |
| 46 | + if($clonedEvent.EventTypeId -match "com.vmware.vc.VmInstantClonedEvent") { |
| 47 | + $CloneType = "Instant" |
| 48 | + $SourceVM = $clonedEvent.Arguments[0].value |
| 49 | + # Full or Linked |
| 50 | + } else { |
| 51 | + # Full Clone from either VM Template or Content Library VMTX |
| 52 | + if($clonedEvent.getType().Name -eq "VmDeployedEvent") { |
| 53 | + $CloneType = "Full" |
| 54 | + $SourceVM = $clonedEvent.SrcTemplate.Name |
| 55 | + # Standard Full or Linked Clone |
| 56 | + } else { |
| 57 | + # Linked Clone VMs seems to have BackingObjectId defined in main descriptor file |
| 58 | + $backingObjectFiles = $clonedVM.ExtensionData.LayoutEx.File | where {$_.Type -eq "diskDescriptor" -and $_.BackingObjectId -ne $null} |
| 59 | + |
| 60 | + if($backingObjectFiles) { |
| 61 | + $CloneType = "Linked" |
| 62 | + } else { |
| 63 | + $CloneType = "Full" |
| 64 | + } |
| 65 | + $SourceVM = $clonedEvent.SourceVM.Name; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + $results = [pscustomobject] @{ |
| 70 | + Type = $CloneType; |
| 71 | + Source = $SourceVM; |
| 72 | + Date = $clonedEvent.CreatedTime |
| 73 | + User = $clonedEvent.UserName |
| 74 | + } |
| 75 | + |
| 76 | + $results |
| 77 | + } else { |
| 78 | + Write-Host "Unable to find any cloning information for ${VMName}, VM may not have been cloned or vCenter Events have rolled over" |
| 79 | + } |
| 80 | +} |
| 81 | + |
0 commit comments