Menu

Virtual Geek

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

Copy or clone distributed virtual switch portgroups to standard switch portgroups - Powercli

Migrating ESXi server from one vCenter server to another vCenter server is very easy task if complete networking is on standard virtual switches. When migrating ESXi server with distributed virtual switches to another VC is also easy but requires extra steps. First it is recommended to move back from DVSwitch to SSwitch before migrating esxi to another vCenter. Failing to do so will create issues and make exiting DVswitch on esxi server to go in zombie state and won't operate properly If you want to make changes to virtual machines networks or need to add new virtual Portgroups or any other operations need to perform those are not allowed on same as host is not part of DVswitch, and DVswitch and its portgroup configuration is only possible through original vCenter where it is built. Again removing it from Esxi host is very hectic task and can be done using esxcfg or esxcli commands in shell. So always make sure you do it properly in first place. Below is the error if don't remove host from dvswitch and directly move to another vCenter.

     "The distributed Virtual Switch corresponding to the proxy switches a4 1c 33 73 ac 8f 23 ac-c5 2e 8a 6a b9 ca f0 ce on the host does not exist in vCenter or does not contain the host."

Earlier Parts
Part 1 - Copy or clone distributed virtual switch portgroups to standard switch portgroups - Powercli
Migrating move back from Distributed virtual switch to Standard virtual switch - VMKernal Adapter - Part 2
Migrating move back from Distributed virtual switch to Standard virtual switch - VMs (Virtual Machine) - Part 3
Part 4: Remove ESXi Host from Distributed switch and migrate to virtual switch completely

Here are some screenshots from my environment. This is part 1 of the series, I have few ESXi in cluster, all the esxi servers are already on DVSwitch and I don't have any Standard switch. I have plenty of dvportgroups. Here I will be creating one SSwitch and cloning those dvportgroups on it. I need portgroup name and vlanid. Creating it manually on multiple Esxi is error prone, time consuming and will require special attention as portgroup names are case sensitive.

vmware vsphere vcenter esx powercli, cluster, dvswitch distributed standard, New-VirtualPortGroup, Get-vdswitch script error handling, hosts and clusters, networking configure, portgroup, standard vswitch.png

To make this task easy I have written below script. Copy script in Copy-DvsPortGroupToSSwitch.ps1 file. once it is executed, This will ask for vCenter server FQDN or IP, ClusterName where Esxi host exist and DVswitch which portgroup you to replicate on newly created SSwitch. Supply vCenter user name and password for authentication. As a result it shows vCenter server it connected to, creates new standard virtual switch name SvSwitch100, and all portgroups on it. Just to mention portgroups are created with default settings.

vmware vsphere vcenter esx powercli, cluster, dvswitch distributed standard virtual switch, New-VirtualSwitch, New-VirtualPortGroup, Get-vdswitch script error handling

Same can be verified on the VMWare vSphere Web Client, under esxi, configure tab and Virtual switches, this script helps you to create consistent vSwitch and vPortgroups across hosts.

vmware vsphere vcenter esxi, cluster new virtual switch, distributed virtual switch, vswitch, create using powercli, clone dvswitch, copy dvswitch.png

Below is the script, copy it in the file. to run it correctly use below article to setup your Vmware powercli environment.
VMWARE VSPHERE POWERCLI INSTALLATION AND CONFIGURATION STEP BY STEP
POWERCLI INITIALIZE-POWERCLIENVIRONMENT.PS1 CANNOT BE LOADED BECAUSE RUNNING SCRIPTS IS DISABLED

  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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
#requires -version 4
<#
.SYNOPSIS
    Clone/Copy dvswitch (Distributed virtual switch) portgroups on standard virtual switch.
.DESCRIPTION
    The Copy-DvsPortGroupToSSwitch cmdlet creates new standard switch named 'SvSwitch100' on esxi host and clone portgroups from existing distributed standard switch and create it on 'SvSwitch100'.
.PARAMETER vCenter
    Prompts you for vCenter server FQDN or IP address to connect, vc parameter is an alias, This value can be taken from pipline by property name.
.PARAMETER Cluster
    Make sure you type a valid ClusterName within the provided vCenter server. New vSwitch name 'SvSwitch100' is created on all esxi hosts withing this cluster, and existing DvSwitch PortGroups cloned to newly vSwitch created.
.PARAMETER DVSwitch
    This ask for existing distributed virtual switch (dvswitch). All the portgroups from this distributed vswitch is copied to vSwitch 
.INPUTS
    VMware.VimAutomation.ViCore.Impl.V1.Inventory.ClusterImpl
    VMware.VimAutomation.Vds.Impl.V1.VmwareVDSwitchImpl
    VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.VirtualPortGroupImpl
    VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.VirtualSwitchImpl
.OUTPUTS
    VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.VirtualPortGroupImpl
    VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.VirtualSwitchImpl
.NOTES
  Version:        1.0
  Author:         Kunal Udapi
  Creation Date:  12 August 2017
  Purpose/Change: Clone or copy existing distributed virtual portgroups from dvswitch to Standard virtual switch
  Useful URLs: http://vcloud-lab.com
.EXAMPLE
    PS C:\>.\Copy-DvsPortGroupToSSwitch.ps1 -vCenter vcsa65.vcloud-lab.com -Cluster Cluster01 -DVSwitch DVSwitch-NonProd-01

    This command connects vcenter 'vcsa65.vcloud-lab.com', copy/clone dvswitch portgroups from 'DVSwitch-NonProd-01' and create new vswitch and copied portgroups on all esxi host in the cluster name 'cluster01'
#>
[CmdletBinding(SupportsShouldProcess=$True,
    ConfirmImpact='Medium', 
    HelpURI='http://vcloud-lab.com', 
    SupportsTransactions=$True)]
Param (
    [parameter(Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true, HelpMessage='Type vCenter server IP or FQDN you want to connect')]
    [alias('vc')]
    [String]$vCenter,
    [parameter(Position=1, Mandatory=$true, ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, HelpMessage='Type valid Cluster Name within vCenter server')]
    [alias('c')]
    [String]$Cluster,
    [parameter(Position=2, Mandatory=$true, ValueFromPipelineByPropertyName=$true, HelpMessage='Type valid distributed virtual switch (dvswitch) name')]
    [alias('dvs')]
    [String]$DVSwitch
)
Begin {
#$Cluster = 'Cluster01'
#$DVSwitch = 'DVSwitch-NonProd-01'
    if ( -not (Get-Module  vmware.vimautomation.core)) {
        Import-Module vmware.vimautomation.core
        Import-Module vmware.vimautomation.vds
    }
}
Process {
    if ($global:DefaultVIServers.Name -notcontains $vCenter) {
        try {
            Connect-VIServer $vCenter -ErrorAction Stop
        }
        catch {
            Write-Host $($Error[0].Exception) -ForegroundColor Red
            break
        }
    }
    try {
        $ClusterInfo = Get-Cluster $cluster -ErrorAction Stop
        $DvSwitchInfo = Get-VDSwitch -Name $DVSwitch -ErrorAction Stop
    }
    catch {
        Write-Host $($Error[0].Exception) -ForegroundColor Red
        break
    }
    #Updated 24/04/2018 - Create switches on only connected and Maintenance hosts
    $AllEsxis = $ClusterInfo | Get-VMhost | Where-Object {$_.ConnectionState -eq 'Connected' -or $_.ConnectionState -eq 'Maintenance'}
    $DvPortGroupInfo = $DvSwitchInfo | Get-VDPortgroup | Where-Object {$_.IsUplink -eq $false}

    foreach ($esxi in $ALLEsxis) {
        $ExistingSwitchs = $esxi | Get-VirtualSwitch
        $esxiName = $esxi.name
        if ($ExistingSwitchs.Name -notcontains 'SvSwitch100') {
            $vSwitch100 = $esxi | New-VirtualSwitch -Name SvSwitch100 -Mtu $DvSwitchInfo.Mtu
            $NvSwitchName = $vSwitch100.Name
            Write-Host "$([char]8734) " -ForegroundColor Magenta -NoNewline
            Write-Host "Created $NvSwitchName on $esxiName" -BackgroundColor Magenta 
            Foreach ($DvPortGroup in $DvPortGroupInfo) {
                $vPortGroupName = $DvPortGroup.Name
                $vLanID = $DvPortGroup.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
                $NewPortGroup = $vSwitch100 | New-VirtualPortGroup -Name $DvPortGroup.Name -VLanId $vLanID
                Write-Host "`t $([char]8730) " -ForegroundColor Green -NoNewline
                Write-Host "Created New PortGroup $vPortGroupName With vLanID $vLanID" -BackgroundColor DarkGreen
            }
        }
        else {
            Write-Host "$([char]215) " -ForegroundColor Red -NoNewline
            Write-Host "SvSwitch100 already present on $esxiName skipping..." -BackgroundColor DarkRed 
            Continue
        }
    }
}
End {
    Disconnect-VIServer $vCenter -Confirm:$false
}

Here I want to show what happens if you try to rerun the script again, It will detect that SvSwitch100 is already present and does not execute further. I have stored my script under c:\temp.

.\Copy-DvsPortGroupToSSwitch.ps1 -vCenter vcsa65.vcloud-lab.com -Cluster cluster01 -DVSwitch DVSwitch-NonProd-01

× SvSwitch100 already present on esxi002.vcloud-lab.com skipping...

Part 1 VMware Powercli : Gather distributed virtual switch information to JSON file to migrate standard switch
Part 2 Copy or clone distributed virtual switch portgroups to standard switch portgroups - Powercli
Part 3 VMware Powercli: Migrate VMs to another network

vmware vsphere vcenter esx powercli, cluster, dvswitch distributed standard virtual switch, New-VirtualSwitch, New-VirtualPortGroup, Get-vdswitch script error handling, clone copy portgroups, migrating esxi host with dvswitch, configure esxi

Download link, This script is also available on Github.

Useful Articles
VMWARE VCENTER 6.5 UPGRADATION ERROR
PART 2 : CONFIGURING ESXI ON VMWARE WORKSTATION HOME LAB
POWERCLI - CREATE DATACENTER AND ADD ESXI HOST IN VCENTER
PART 1 : BUILDING AND BUYING GUIDE IDEAS FOR VMWARE LAB

Go Back



Comment

Blog Search

Page Views

11240035

Follow me on Blogarama