Menu

Virtual Geek

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

VMware Powercli : Gather distributed virtual switch information to JSON file to migrate standard switch

I have already written complete series on how to migrate distributed virtual switch to Standard virtual switch which helps to move Esxi host from one vCenter to another vCenter server. As you should not remove Esxi directly with distributed switch from vCenter until you move it to standard virtual switch properly. If you do it, you will find everything is working fine at the networking end, but you cannot modify network and it is kind of broken, and recovering from it will be hectic, you can consider downtime as well. 

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

In my earlier part series all the steps were performed using VMware vSphere Web Client, graphical user interface. This is good for few clusters and Esxi hosts for migration., but what if I need to migrate to standard switches on more than 100 clusters, and each cluster has at list 5 esxi host. It will be very time consuming task and if there are any errors in it complete day is screwed up.

Note: I wrote and built this script where each esxi host in cluster is connected to only one distributed virtual switch with 2 uplink physical network adapters, It has Two vmkernel adpaters (Management and vMotion) and all the VMs network are connected to this distributed portgroups. as below screenshots, This is the common configuration I have seen in most of the environment. If you have mismatch in configuration (ie: more than 2 uplink connected or more than 2 vmkernel adapters exist), script  will not collect correct information, Once information is gathered, it is stored in JSON file

vmware vsphere vcenter esxi, virtual switches configuration, dvswitch, virtual distibuted switch collect information, powercli, automation, gather information

Before migrating from DVSwitch to standard vSwitch I need to collect existing information. This database contains, connected physical network adapters uplinks esxi DVSwitch, VMKernel port and Virtual Machine network adapter configuration (connected PortGroup). This script only gathers information and save to JSON file and doesn't make any changes. 

To run script correctly use below article to setup your VMware PowerCLI environment. Make sure you have installed latest version of Powercli, read help in the scripts.
VMWARE VSPHERE POWERCLI INSTALLATION AND CONFIGURATION STEP BY STEP
POWERCLI INITIALIZE-POWERCLIENVIRONMENT.PS1 CANNOT BE LOADED BECAUSE RUNNING SCRIPTS IS DISABLED

PS C:\Scripts> .\Get-VDswitchInfo.ps1 -vCenter vcsa65.vcloud-lab.com -Cluster Cluster01 -VDSwitch DVSwitch-NonProd-01 -JsonFile C:\Scripts\dvswitchInfo.json

Parameters Break-Down
-vCenter: vCenter parameter requires vCenter FQDN or IP to connect, this prompts for UserName and Password. 
-Cluster: Provide clustername for this parameter in the connected vCenter, Information will be fetched from Host withing this virtual cluster.
-VDSwitch: Esxi hosts within cluster, connected to this virtual distributed switch.
-JsonFile: Data is collected and stored in this JSON file.

vmware vsphere powercli, virtual distributed switch, get DVswitch information to JSON file, Migration from vDswitch to standard switch, cluster, vCenter, esxi

Earlier Series
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

Json file has complete network information as below and it is one of the industry standard to view and store data.

vmware vsphere powercli, Get-cluster, Get-VMhost, get-vdswitch, Get-Vdportgroup, get-vm, get-vmhost, get-networkadapter, vcenter, esxi, powershell, collect information to json, distributed switch standard switch

download vmware vsphere powercli, powershell, esxi, vcenter, distributed virtual switch, standard switch,  migration powercliDownload script.
This script is also available on GitHub.
 

  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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#requires -version 4
<#
.SYNOPSIS
    Collects vDSwitch (Distributed virtual switch) portgroups, Virtual machines and Physical Port infromation for migration.
.DESCRIPTION
    The Get-VDswitchInfo Collects existing virtual distributed switch information, This script is written for one dvswitch in a cluster, and 2 nics per esxi server. Here it collects info about EsxiHost, DVSwitch Physical Adapters, VMKernel adapters, Virtual Machine Network. This is information is useful in my next script when migrating from DvSwitch to Standard switch.
.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. This script collect information from this Cluster and host.
.PARAMETER DVSwitch
    This ask for existing distributed virtual switch (dvswitch) in the cluster.
.PARAMETER JsonFile
    Collected information is stored in JSON file, provide a path for json ie: c:\temp\vdinfo.json 
.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: Collect dvswitch information to json file for DVswtich to SSwitch migration
  Useful URLs:    http://vcloud-lab.com/entries/powercli/copy-or-clone-distributed-virtual-switch-portgroups-to-standard-switch-portgroups-powercli
  OS Version:     Windows 10 pro version 1703, Build 15063.726
  Powershell:     5.1.15063.726  Desktop Edition
  Powercli:       VMware PowerCLI 6.5 Release 1 build 4624819
                  VMware VimAutomation Core PowerCLI Component 6.5 build 4624450
                  VMware Vds PowerCLI Component 6.5 build 4624695

.EXAMPLE
    PS C:\>.\Get-VDswitchInfo.ps1 -vCenter vcsa65.vcloud-lab.com -Cluster Cluster01 -VDSwitch DVSwitch-NonProd-01 -JsonFile c:\temp\dvswitchInfo.json

    This command connects vcenter 'vcsa65.vcloud-lab.com',  infCollectormation from 'DVSwitch-NonProd-01' and Cluster 'Cluster01' its esxi host, keep the information in c:\temp\dvswitchInfo.json file.
#>

[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('vds')]
    [String]$VDSwitch,
    [parameter(Position=3, Mandatory=$true, ValueFromPipelineByPropertyName=$true, HelpMessage='Type valid distributed virtual switch (dvswitch) name')]
    [alias('File')]
    [String]$JsonFile
)
Begin {
    if ( -not (Get-Module  vmware.vimautomation.core)) {
        Import-Module vmware.vimautomation.core
        Import-Module vmware.vimautomation.vds
    }

    if ($global:DefaultVIServers.Name -notcontains $vCenter) {
        try {
            Connect-VIServer $vCenter -ErrorAction Stop
        }
        catch {
            Write-Host $($Error[0].Exception) -ForegroundColor Red
            break
        }
    }

    $OverAllInfo = @()
} #Begin
Process {
    try {
        $ClusterInfo = Get-Cluster $Cluster -ErrorAction Stop
        $DvSwitchInfo = Get-VDSwitch -Name $VDSwitch -ErrorAction Stop
    }
    catch {
        Write-Host $($Error[0].Exception) -ForegroundColor Red
        break
    }

    $EsxiHosts = $ClusterInfo | Get-VMHost | Sort-Object Name
    foreach ($ESXi in $EsxiHosts) {
        $ESXiHostName = $ESXi.Name
        Write-Host "Collecting information from $ESXiHostName ..." -ForegroundColor Green
        try {
            $DistributedSwitch = $ESXi | Get-VDSwitch -Name $VDSwitch
        }
        catch {
            Write-Host $($Error[0].Exception) -ForegroundColor Red
            Continue
        }
        $vDSwitchName = $DistributedSwitch.Name

        $UplinkPortGroups = $DistributedSwitch | Get-VDPort -Uplink | Where-Object {$_.ProxyHost.Name -eq $ESXiHostName} | Sort-Object Name
        $PhysicalAdapters = $DistributedSwitch | Get-VMHostNetworkAdapter -Physical | Where-Object {$_.VMHost.Name -eq $ESXiHostName} | Sort-Object Name
        $VMKernelAdapters = $DistributedSwitch | Get-VMHostNetworkAdapter -VMKernel | Where-Object {$_.VMHost.Name -eq $ESXiHostName} | Sort-Object Name
        $VDPortGroup = $DistributedSwitch | Get-VDPortgroup 
        #$UPLinks = $VDPortGroup | Where-Object {$_.IsUplink -eq $true}
        
        if ($PhysicalAdapters.Count -lt 2) {
            Write-Host "No network redundancy found on server $ESXiHostName, do no proceed until you have 2 Nic cards..." -ForegroundColor Red
        } #if ($PhysicalAdapters.Count -lt 2)

        $PNicInfo = $UplinkPortGroups | Select-Object Name, ConnectedEntity, Switch, ProxyHost
    
        $CompleteVMKInfo = @()
        $VMKArray = @('Management', 'vMotion')
        foreach ($VMK in $VMKArray) {
            switch ($VMK) { 
                Management {
                    $SpecificVMK = $VMKernelAdapters | Where-Object {$_.ManagementTrafficEnabled -eq $true}
                } #Management {
                vMotion {
                    $SpecificVMK = $VMKernelAdapters | Where-Object {$_.VMotionEnabled -eq $true}
                } #vMotion
            } #switch ($VMK) {
        
            $VMKPortGroup = $VDPortGroup | Where-Object {$_.Name -eq $SpecificVMK.PortGroupName}
            $VMKVLanId = $VMKPortGroup.VlanConfiguration.VlanId
            $VMKInfo = [PSCustomObject]@{
                Name = $SpecificVMK.Name
                IP = $SpecificVMK.IP
                SubnetMask = $SpecificVMK.SubnetMask
                VLANId = $VMKVLanId
            } #$ManagementVMKInfo = [PSCustomObject]@{  
            $CompleteVMKInfo += $VMKInfo
        } #foreach ($VMK in $VMKArray) {
    
        $CompleteVMNetInfo = @()
        $VirtualMachines = $ESXi | Get-VM 
        foreach ($VM in $VirtualMachines) {
            $VMNetAdapters = $VM | Get-NetworkAdapter 

            foreach ($VMNet in $VMNetAdapters) {
                if ($VMNet.ExtensionData.DeviceInfo.Summary -match 'DVSwitch') {
                    $VMNetworks = [PSCustomObject]@{
                        VMName = $VM.Name
                        AdapterName = $VMNet.Name
                        PortGroupName = $VMNet.NetworkName
                        EsxiName = $VM.VMhost
                    } #$VMNetworks = [PSCustomObject]@{
                }  #if ($VMNet.ExtensionData.DeviceInfo.Summary -match 'DVSwitch') {
            $CompleteVMNetInfo += $VMNetworks
            } #foreach ($VMNet in $VMNetAdapters) {
        } #foreach ($vm in $VirtualMachines) {
    
        $MainObj = New-Object psobject
        $MainObj | Add-Member -Name EsxiHost -MemberType NoteProperty -Value $ESXi.Name
        $MainObj | Add-Member -Name ConnectionState -MemberType NoteProperty -Value $ESXi.ConnectionState
        $MainObj | Add-Member -Name PowerState -MemberType NoteProperty -Value $ESXi.PowerState
        $MainObj | Add-Member -Name PhysicalNics -MemberType NoteProperty -Value $PNicInfo
        $MainObj | Add-Member -Name VMKernels -MemberType NoteProperty -Value $CompleteVMKInfo
        $MainObj | Add-Member -Name VMNetwork -MemberType NoteProperty -Value $CompleteVMNetInfo
        $OverAllInfo += $MainObj
    }#foreach ($ESXi in $EsxiHosts) {
} #Process
End {$XmlFile = $JsonFile -Replace 'json', 'xml'; $OverAllInfo | Export-CliXml -Path $JsonFile
    $OverAllInfo | ConvertTo-Json | Out-File -FilePath $JsonFile 
} #End

Here I have made little arrangement to export information to XML file, In the next article I will use this XM and database to migrate VMs to standard switch portgroup earlier created.

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

12086104

Follow me on Blogarama