dvSwitch scripting – Part 5 – Clone/pg to dvPG

An interesting question came up in the PowerCLI Community recently. Jason wanted to use a script from another thread where you could connect a NIC to a specific portgroup while cloning a new guest from a template.

The script didn’t work when the requested portgroup was on a dvSwitch. Enough of a reason for me to have another look.

The original script looks up the portgroup on the ESX server and then just copies the VirtualDevice object to the VirtualMachineConfigSpec object.

But, as expected this didn’t seem to work when dealing with dvSwitch portgroups.

In fact, to my amazement, the script created a new portgroup with the same name as the requested dvSwitch portgroup. No error message whatsoever !

The following script solves the problem. It finds the dvSwitch, the portgroup and sets up the configuration parameters correctly for the CloneVM_Task method.

The template was created with a connection to a regular portgroup.


Line 15: The script uses the template’s ESX host to find the dvSwitch

Line 20,21: To specify the dvSwitch we need the Uuid and the key of the dvSwitch

Line 27-31: This loop finds the NIC which we want to connect to the dvSwitch portgroup. The script doesn’t have any error handling code for this. If the NIC name is misspelled, the CloneVM_Task will fail.

Line 42,43: The backing device is where the big difference with the original script is located.

After the script runs you will see your new guest, called PC1 in my test environment, connected to the dvSwitch portgroup.

So with this script you don’t have go updating your huge collection of templates when you move to dvSwitches 😉

One Comment


    I’m not good at scripting and I been looking at sample of cloning VM template using customization scripts, came across this crucial part I need since we have various port groups. Where do I add this piece of the script to? Here is what I have.

    ‘Beginning VM cloning script’
    function LoadSnapin{
    if (!(Get-PSSnapin | where {$_.Name -eq $PSSnapinName})){
    Add-pssnapin -name $PSSnapinName
    LoadSnapin -PSSnapinName “VMware.VimAutomation.Core”

    Connect-VIServer WDCVCS02

    ‘Reading CSV file’

    $vms = Import-Csv C:\vmtest.csv
    ‘Reading the contents of the CSV, and for each line execute the following code’

    foreach ($vm in $vms){
    ‘Declaring variables that correspond to the column names in the CSV’
    $VMName = $vm.name
    $VMHost = $vm.host
    $Datastore = $vm.datastore
    $Template = Get-Template $vm.templatevm
    $Customization = $vm.customization
    $IPAddress = $vm.ipaddress
    $Subnetmask = $vm.subnetmask
    $DefaultGW = $vm.gateway
    $DNS1 = $vm.dns1
    $DNS2 = $vm.dns2
    $WINS1 = $vm.wins1
    $WINS2 = $vm.wins2
    $Location = Get-Folder $vm.location
    $NetworkName = $vm.vlan
    $Notes = $vm.notes

    ‘Modifying the customization file with the network information you specified in the CSV’

    if (!(Get-VM $VMName -ErrorAction 0))
    Get-OSCustomizationSpec $Customization | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $IPAddress -SubnetMask $Subnetmask -DefaultGateway $DefaultGW -Dns $DNS1,$DNS2 -Wins $WINS1,$WINS2

    ‘Deploying new VMs from the template you specified in the CSV’

    New-VM -Name $VMName -OSCustomizationSpec $Customization -Template $Template -Location $location -VMHost $VMHost -DiskStorageFormat Thin -Datastore $Datastore -RunAsync
    Get-Task | ? {$_.id -eq $Item.id}
    nVM aleady exists, checking for running tasks . .n”
    $Task = @(Get-Task | ? {$_.Description -eq “Clone virtual machine”})
    if ($Task)
    Write-Host “Task is $($Task[$i].PercentComplete) % complete.”
    “Task is complete!`n”


    ‘Powering up the newly created VMs to allow the guest customization to complete’

    Start-VM -VM $VMName -Confirm:$false -RunAsync | Wait-Task

    Get-VM -Name $VMName | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $NetworkName -StartConnected $true -Confirm:$false

    #Get-Cluster “DEV – HP Rack 2 – Cluster 2 – Windows” | Get-VM $VMName | Update-Tools -NoReboot#
    ‘Ending script’


Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.