I'm trying to automate the following steps so that I can configure the networking for a host, end to end (using vCenter 5.1, PowerCLI 5.1 Release 2 Build 1012425 switched to PowerCLI 6.0 Release 2 build 3056836). The host has two physical nics, one of which (vmnic0) is obviously taken by vSwitch0 on initial install. So, I need to:
1) Migrate physical vmnic1 to a VDS (1000v)
2) Set the uplink portgroup for vmnic1 to 'system-uplink-vc02'.
3) Switch the vmk0 management network from vSwitch1 to a portgroup on the VDS called 'vc02-vmsc'.
4) Migrate physical vmnic0 from vSwitch1 to VDS, adding it to the uplink portgroup 'system-uplink-vc02'.
I can only manage to achieve 1-3. I can manage part of 4), moving vmnic0 to the 1000v, but only into the "Unused_or_Quarantine_Uplink" portgroup and not the one I require.
Stripped of all error handling, this is the code I've used (largely provided by
http://blogs.cisco.com/datacenter/automate-migrating-esx-host-interfaces-to-nexus-1000v)
http://www.virtuallyghetto.com/2013/10/automate-migration-from-virtual.html):
$esxihost = 'host-0201'
$vmnic = 'vmnic1'
$1000vName = 'cisco-1000v-data-centre-1'
$uplinkName = 'system-uplink-vc02'
# Get the 1000v object that that ESX host will be added to
$1000vObject = Get-VDSwitch | Where-Object -FilterScript {
$_.name -eq $1000vName
}
# Get the ESX host object
$vmHost = Get-VMHost -Name $esxihost -Erroraction Stop | Get-View
# Create a DVS Configuration Specification object
$spec = New-Object -TypeName VMware.Vim.DVSConfigSpec
# Create a target host DVS Host member configuration specification and set the operation to add
$targetHost = New-Object -TypeName VMware.Vim.DistributedVirtualSwitchHostMemberConfigSpec
$targetHost.operation = 'add'
# Create a Pnic backing object in the target host
$targetHost.backing = New-Object -TypeName VMware.Vim.DistributedVirtualSwitchHostMemberPnicBacking
# Create a Pnic Device object
$pnic = $vmHost.Config.Network.Pnic | Where-Object -FilterScript {
$_.Device -eq $vmnic
}
$targetHost.Backing.PnicSpec = New-Object -TypeName VMware.Vim.DistributedVirtualSwitchHostMemberPnicSpec
$targetHost.Backing.PnicSpec[0].pnicDevice = $pnic.Device
# Get the 1000V uplink object:
$uplinkObj = Get-VDPortgroup | Where-Object { $_.Name -eq $uplinkName }
$targetHost.Backing.PnicSpec[0].UplinkPortGroupKey = $uplinkObj.Key
# Set the target host to the ESX host object reference
$targetHost.host = $vmHost.MoRef
# Set the DVS configuration specification object host property, to the target host object reference we've created above:
$spec.Host = $targetHost
# Get the current status of the 1000v and set the version in the configuration specification version
$dvSwitch = Get-View -Id $1000vObject.ExtensionData.MoRef
$dvSwitch.UpdateViewData()
$spec.ConfigVersion = $dvSwitch.Config.ConfigVersion
# Run the task
$taskMoRef = $dvSwitch.ReconfigureDvs_Task($spec)
# Get the status
$taskID = 'Task-' + $taskMoRef.Value
while((Get-Task -Id $taskID).PercentComplete -lt "100")
{
$percentComplete = (Get-Task -Id $taskID).PercentComplete
Write-Verbose "Percent Complete: $percentComplete"
Start-Sleep -Seconds 2
}
# 3) Migrate vmk0 Management Network from vSwitch to the VDS with correct portgroup:
# Get the VMKernel port
$vNicManagement = Get-VMHostNetworkAdapter -VMHost $esxihost -Name vmk0
# Get the destination port group:
$vdPortgroupManagement = Get-VDPortgroup -VDSwitch $1000vName -Name 'vc02-vmsc'
# Set the physical NIC to use:
$pnicToUse = Get-VMHostNetworkAdapter -VMHost $esxihost -Physical | Where-Object { $_.Name -eq $vmnic }
# Migrate:
Add-VDSwitchPhysicalNetworkAdapter -DistributedSwitch $1000vName -VMHostPhysicalNic $pnicToUse -VMHostVirtualNic $vNicManagement -VirtualNicPortGroup $vdPortGroupManagement -ErrorAction Stop
The bit that will move vmnic0 to the VDS is:
# Get vmnic0 which is still connected to the vSwitch:
$lastNic = 'vmnic0'
$pnicToMove = Get-VMHostNetworkAdapter -VMHost $esxihost -Physical | Where { $_.Name -eq $lastNic }
# Migrate vmnic0 from vSwitch to VDS:
Add-VDSwitchPhysicalNetworkAdapter -DistributedSwitch $1000vName -VMHostPhysicalNic $pnicToMove -Confirm:$false
... but as I mentioned, this only puts it into the Unused_or_Quarantine_Uplink group.
I've tried repeating the code above that targets vmnic1, against vmnic0, but this reports that the host is already a member of the VDS.
I suspect the answer lies in a modification of Migrate ESXi Host Physical Adapters to Specific dvUplink Port | vBombarded but I've not had luck so far.
Help appreciated on the final bit
Thanks.
I managed to solve this using:
$config = New-Object VMware.Vim.HostNetworkConfig
$config.proxySwitch = New-Object VMware.Vim.HostProxySwitchConfig[] (1)
$config.proxySwitch[0] = New-Object VMware.Vim.HostProxySwitchConfig
$config.proxySwitch[0].changeOperation = "edit"
$config.proxySwitch[0].uuid = $1000vObject.key
$config.proxySwitch[0].spec = New-Object VMware.Vim.HostProxySwitchSpec
$config.proxySwitch[0].spec.backing = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicBacking
$config.proxySwitch[0].spec.backing.pnicSpec = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicSpec[] (1)
$config.proxySwitch[0].spec.backing.pnicSpec[0] = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicSpec
$config.proxySwitch[0].spec.backing.pnicSpec[0].pnicDevice = "vmnic0"
$config.proxySwitch[0].spec.backing.pnicSpec[0].uplinkPortgroupKey = $uplinkObj.key
$vmhostRef = ($vmhost.MoRef.value).split('-')[1]
$_this = Get-View -Id "HostNetworkSystem-networkSystem-$vmhostRef"
$_this.UpdateNetworkConfig($config, "modify")
I managed to solve this using:
$config = New-Object VMware.Vim.HostNetworkConfig
$config.proxySwitch = New-Object VMware.Vim.HostProxySwitchConfig[] (1)
$config.proxySwitch[0] = New-Object VMware.Vim.HostProxySwitchConfig
$config.proxySwitch[0].changeOperation = "edit"
$config.proxySwitch[0].uuid = $1000vObject.key
$config.proxySwitch[0].spec = New-Object VMware.Vim.HostProxySwitchSpec
$config.proxySwitch[0].spec.backing = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicBacking
$config.proxySwitch[0].spec.backing.pnicSpec = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicSpec[] (1)
$config.proxySwitch[0].spec.backing.pnicSpec[0] = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicSpec
$config.proxySwitch[0].spec.backing.pnicSpec[0].pnicDevice = "vmnic0"
$config.proxySwitch[0].spec.backing.pnicSpec[0].uplinkPortgroupKey = $uplinkObj.key
$vmhostRef = ($vmhost.MoRef.value).split('-')[1]
$_this = Get-View -Id "HostNetworkSystem-networkSystem-$vmhostRef"
$_this.UpdateNetworkConfig($config, "modify")