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 this third part of article I will be continuing configuration of virtual machine network and moving it to another network. I have already generated Json and XML file with complete information which I used to migrate Esxi server from one vCenter to another vCenter with distributed virtual switch, and before esxi movement I will be using same XML file to change VM network from DVswitch to standards witch or vice versa. To view portgroup info from xml file use Import-Clixml command, This information is used in the script to change each VM network adapter settings portgroup.
Import-Clixml Completeinfo.xml | Select-Object -ExpandProperty VMNetwork
VMWARE VSPHERE POWERCLI INSTALLATION AND CONFIGURATION STEP BY STEP
POWERCLI INITIALIZE-POWERCLIENVIRONMENT.PS1 CANNOT BE LOADED BECAUSE RUNNING SCRIPTS IS DISABLED
I am running this ps1 script with below parameters
-vCenter: This is vCenter name you are connecting, it prompts for username and password.
-VirtualSwitch: If this parameter is not specified, by default takes standard switch name SvSwitch100, as same name standard virtual switch is created in earlier article Copy or clone distributed virtual switch portgroups to standard switch portgroups - Powercli.
-XmlFile: This file is created in earlier article VMware Powercli : Gather distributed virtual switch information to JSON file to migrate standard switch, and I will be using same file to get remaining task done.
.\Move-VMPortGroup.ps1 -vCenter vcsa65.vcloud-lab.com -VirtualSwitch SvSwitch100 -XmlFile C:\Temp\CompleteInfo.xml
If I execute only .\Move-VMPortGroup.ps1 it prompts me for vCenter and xmlfile only. default VirtualSwitch name is taken automatically. If VM portgroup is moved successfully it shows green checkbox and incase of error shows red cross.
Download this script here, it is also available on github.com.
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 |
#requires -version 4 <# .SYNOPSIS Migrate VMs from DVSwitches to Standard Switches and vice versa. .DESCRIPTION The Move-VMPortgroup cmdlets migrates VMs Network. This script is a part of earlier written script Copy-DvsPortGroupToSSwitch. .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 VirtualSwitch This ask for existing distributed virtual switch (dvswitch) or standard switch. Default value is SvSwitch100, if you have same Virtual standard swtich. VMs will be migrated to this portgroups on this virtual switch, it helps you to migrate VMs from VDSwitch to Standard swtich and vice versa .PARAMETER XmlFile This is xml file I created earlier, It will migrate these VMs to given virtual switch. .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: 2.0 Author: Kunal Udapi Creation Date: 30 April 2018 Purpose/Change: Part 1: VMware Powercli : Gather distributed virtual switch information to JSON file to migrate standard 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=2, ValueFromPipelineByPropertyName=$true, HelpMessage='Type valid virtual switch name')] [alias('Switch')] [String]$VirtualSwitch = 'SvSwitch100', [parameter(Position=3, Mandatory=$true, ValueFromPipelineByPropertyName=$true, HelpMessage='Type valid virtual switch name')] [alias('File')] [String]$XmlFile ) Begin { if (Test-Path -Path $XmlFile) { $XmlFile = Import-Clixml $XmlFile } else { Write-Host "Provided XML file $XmlFile is not valid" -BackgroundColor DarkRed break } try { if (!(Import-Module vmware.vimautomation.Core) -and !(Import-Module vmware.vimautomation.vds)) { Import-Module VMware.VimAutomation.Core -ErrorAction SilentlyContinue Import-Module vmware.vimautomation.vds -ErrorAction SilentlyContinue } else { Write-Host 'Download and install PowerCLI version 6.5 and above' -BackgroundColor DarkRed } Connect-viserver $vCenter -ErrorAction Stop } catch { Write-Host $Error[0].Exception.Message -BackgroundColor DarkRed break } } Process { #$VirtualSwitch = 'DVSwitch-NonProd-01' # 'SvSwitch100' # foreach ($esxiVms in $XmlFile) { $VMNetwork = $esxiVms.VMNetwork $VMHost = Get-VMHost $esxiVms.EsxiHost $VMHostname = $Vmhost.name if ($VMHost.ConnectionState -ne 'Connected') { "$VMHostname is $($VMHost.ConnectionState), Cannot continue on this host" } else { $SvSwitch100 = $VMHost | Get-VirtualSwitch -Name $VirtualSwitch $GroupdVMs = $esxiVms.VMNetwork | Group-Object PortGroupName foreach ($PortGroups in $GroupdVMs) { Write-Host "$([char]8734) " -ForegroundColor Yellow -NoNewline Write-Host "Migrating '$VMHostname' VMs from portGroup '$($PortGroups.Name)'" -BackgroundColor Yellow -ForegroundColor Black foreach ($VM in $PortGroups.Group) { $VMNicAdapter = $VMHost | Get-VM -Name $VM.VMName | Get-NetworkAdapter -Name $VM.AdapterName $SSwitchPortGroup = $SvSwitch100 | Get-VirtualPortGroup -Name $VM.PortGroupName try { $SetNicAdapter = $VMNicAdapter | Set-NetworkAdapter -Portgroup $SSwitchPortGroup -Confirm:$false -ErrorAction Stop Write-Host "`t $([char]8730) " -ForegroundColor Green -NoNewline Write-Host "`tMigrated VM '$($SetNicAdapter.Parent)' Nic '$($SetNicAdapter.Name)' to portGroup '$($PortGroups.Name)' on vSwitch '$($SSwitchPortGroup.VirtualSwitch)'" } catch { Write-Host "`t $([char]215) " -ForegroundColor DarkRed -NoNewline Write-Host "`tMigrated VM '$($SetNicAdapter.Parent)' Nic '$($SetNicAdapter.Name)' to portGroup '$($PortGroups.Name)' on vSwitch '$($SSwitchPortGroup.VirtualSwitch)'" } } } } } } end { Disconnect-VIServer * -Confirm:$false -ErrorAction SilentlyContinue } |
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