Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

VMware PowerCLI Clone VM from template with customization specs in vCenter

This is a second alternative script to VMware PowerCLI Clone and Deploy VM from template in vCenter. In earlier article I did customization of newly cloned Virtual Machine using Windows PowerShell commands which I executed using Invoke-VMscript. Here in this script I used inbuilt customization profiles.

(Note: If in your case customization profile is not working, check once cloning vm manually on the vCenter web client, There are highly possible chances that your customization profile may not be working through web client as well. First you need to get your Virtual Machine template configuration corrected and prepared it correctly. I used Windows 2022 OS, Removed stale drivers and hardware, Removed and readded network adapter, used vmxnet3 network adapter, Tested through web client all worked good, I feel there is a disconnect between VMware and Microsoft so Customization not works properly)

You can download this script Clone-vCenterVM.ps1 here or it also available on github.com/janviudapi

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#Created By: vcloud-lab.com 
#Owner:  vJanvi
#Clone a Virtual Machine from Template with Customization in VMware vCenter Using PowerCLI

#vCenter Info
$vCenterServer = 'marvel.vcloud-lab.com'
$vCenterUser = 'Administrator@vsphere.local'
$vCenterPassword = 'Computer@123'

#Windows OS customization Spec
$newVMInfo = @(
    @{
        Template = 'Quantumania_Win_22' #'Wakanda_Forever_Win_01'
        OSCustomizationSpec = 'Windows_Customization'
        NewVMName = 'Test003'
        NewVMFolder = 'TempVMs'
        NewVMCluster = 'BiFrost'
        NewVMNetwork = 'VM Network'
        NewVMDataStore = 'StarLord_Datastore01'
        NewVMIPv4 = '192.168.34.90'
        NewVMSubnet = '255.255.255.0'
        NewVMGateway = '192.168.34.1'
        NewVMDNS = @('192.168.34.11', '192.168.34.12')
    },
    @{
        Template = 'Wakanda_Forever_Win_01' 
        OSCustomizationSpec = 'Windows_Customization'
        NewVMName = 'Test002'
        NewVMFolder = 'TempVMs'
        NewVMCluster = 'BiFrost'
        NewVMNetwork = 'VM Network'
        NewVMDataStore = 'StarLord_Datastore01'
        NewVMIPv4 = '192.168.34.91'
        NewVMSubnet = '255.255.255.0'
        NewVMGateway = '192.168.34.1'
        NewVMDNS = @('192.168.34.11', '192.168.34.12')
    }    
)

Import-Module VMware.VimAutomation.Core
Connect-VIServer -Server $vCenterServer -User $vCenterUser -Password $vCenterPassword

foreach ($rawNewVM in $newVMInfo)
{
    # try {
    #     Get-OSCustomizationSpec -Name tempCustom -ErrorAction Stop | Remove-OSCustomizationSpec -Confirm:$false
    # }
    # catch {
    #     <#Do this if a terminating exception happens#>
    # }
     
    $newVM = [PSCustomObject]$rawNewVM
    $cluster = Get-Cluster -Name $newVM.NewVMCluster
    $template = Get-Template -Name $newVM.Template
    $folder = Get-Folder -Name $newVMInfo.NewVMFolder
    #$portGroup = Get-VirtualPortGroup -Name $newVM.NewVMNetwork
    $datastore = Get-Datastore -Name $newVM.NewVMDataStore

    Write-Host "Configuring Customization spec - $($newVM.NewVMName)" -BackgroundColor DarkYellow
    $specs = Get-OSCustomizationSpec -Name $newVM.OSCustomizationSpec | New-OSCustomizationSpec -Name $rawNewVM.NewVMName -Type NonPersistent
    $tempSpecs = Get-OSCustomizationSpec -Name $specs.Name | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $newVM.NewVMIPv4 -SubnetMask $newVM.NewVMSubnet -DefaultGateway $newVM.NewVMGateway -Dns $newVM.NewVMDNS
    #New-OSCustomizationSpec -Name 'TempWin2019' -FullName 'TestName' -OrgName 'TestOrg' -OSType Windows -ChangeSid -AdminPassword (Read-Host -AsSecureString -Prompt typepassword) -Domain 'vCloud-lab.com' -TimeZone 035 -DomainCredentials (Get-Credential) -ProductKey '1111-1111-1111-1111' -AutoLogonCount 1
    #$tempCustom = Get-OSCustomizationSpec -Name $specs.Name

    $parameters = @{
        Name = $newVM.NewVMName 
        Template = $template 
        Location = $folder 
        ResourcePool = $cluster 
        Datastore = $datastore 
        DiskStorageFormat = 'Thin'
    }

    Write-Host "Configuring Customization spec - $($newVM.NewVMName)" -BackgroundColor DarkYellow
    New-VM @parameters | Select-Object Name, VMHost, PowerState
    Get-VM -Name $newVM.NewVMName | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $newVM.NewVMNetwork -Confirm:$false | Select-Object Parent, Type, NetworkName
    Get-VM -Name $newVM.NewVMName | Set-VM -OSCustomizationSpec $specs.Name -Confirm:$false | Select-Object Name, PowerState, VMHost
    Get-VM -Name $newVM.NewVMName | Start-VM | Select-Object Name, PowerState, VMHost
}

Disconnect-VIServer * -Confirm:$false -Force

This is the screenshot while and after cloning virtual machines with customization spec. To use the script add your vCenter server credentials and new VMs information.

vmware vsphere vCenter powercli esxi virtual machine customization spec working network dapter portgroup vmhost powerstate clone template virtual machine.png

Useful Articles
Using terraform to clone a virtual machine on VMware vSphere infrastructure
Terraform module clone VMware vSphere Linux and Windows virtual machine
Terraform VMware vSphere Virtual Machine customization clone failed on Windows
Terraform VMware vSphere Virtual Machine cloning Operating system not found
Ansible Deploy a VMware vSphere Virtual Machine from a Template
VMware ansible Error couldn't resolve module community.vmware
Ansible Error couldn't resolve module vmware.vmware_rest
Can not execute kustomize build issue installing Ansible AWX Operator

Go Back

Comment

Blog Search

Page Views

11274102

Follow me on Blogarama