What’s wrong with this code?

Posted by

Code:

$vCenterIP ="vc.vmzoneblog.com"
Connect-VIServer $vCenterIP
$myDatacenter ="nukescloud"
$Cluster ="sgp"
$NewDVS ="DSwitch"
$NicName ="vmnic4"
$UplinkName="Uplink 4"
$hostnames=Get-Datacenter -Name $myDatacenter | Get-Cluster -Name $Cluster | Get-VMHost
$hostnames=$hostnames.name
ForEach ($hostname in $hostnames)
{
Write-Host -ForegroundColor red "The $hostname already has 1 or more physical Nics on $NewDVS"
$EsxHost = Get-VMHost -Name $hostname
$vds = Get-VDSwitch -Name $NewDVS -VMHost $EsxHost
$uplinks = Get-VDPort -VDSwitch $vds -Uplink | where {$_.ProxyHost -like $hostname}
$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].Uuid = $vds.ExtensionData.Uuid
$config.ProxySwitch[0].ChangeOperation = 'edit'
$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[] (2)
$config.ProxySwitch[0].Spec.Backing.PnicSpec[0] = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicSpec
$config.ProxySwitch[0].Spec.Backing.PnicSpec[0].PnicDevice = $NicName
$config.ProxySwitch[0].Spec.Backing.PnicSpec[0].UplinkPortKey = $uplinks | where{$_.Name -eq $UplinkName} | Select -ExpandProperty Key
$config.ProxySwitch[0].Spec.Backing.PnicSpec[1] = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicSpec
$config.ProxySwitch[0].Spec.Backing.PnicSpec[1].PnicDevice = $uplink.ConnectedEntity | Where-Object {$_.ConnectedEntity.Name -like "vmnic*"}
$config.ProxySwitch[0].Spec.Backing.PnicSpec[1].UplinkPortKey = $uplink.Key | Where-Object {$_.ConnectedEntity.Name -like "vmnic*"}
$changeMode = 'modify'
$netSys = Get-View -Id $EsxHost.ExtensionData.ConfigManager.NetworkSystem
$netSys.UpdateNetworkConfig($config, $changeMode)
Write-Host -ForegroundColor Green "Moved $NicName for $hostname to $NewDVS $UplinkName"
}
Disconnect-viserver -confirm:$false

Error:

Exception calling "UpdateNetworkConfig" with "2" argument(s): "There is an error in the XML document."
At line:32 char:1
+ $netSys.UpdateNetworkConfig($config, $changeMode)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException

3 comments

  1. Does this makes any difference?
    $vCenterIP =”vc.vmzoneblog.com”
    Connect-VIServer $vCenterIP
    $myDatacenter =”nukescloud”
    $Cluster =”sgp”
    $NewDVS =”DSwitch”
    $NicName =”vmnic4″
    $UplinkName=”Uplink 4″
    $hostnames=Get-Datacenter -Name $myDatacenter | Get-Cluster -Name $Cluster | Get-VMHost
    $hostnames=$hostnames.name
    ForEach ($hostname in $hostnames)
    {
    Write-Host -ForegroundColor red “The $hostname already has 1 or more physical Nics on $NewDVS”
    $EsxHost = Get-VMHost -Name $hostname
    $vds = Get-VDSwitch -Name $NewDVS -VMHost $EsxHost
    $uplinks = Get-VDPort -VDSwitch $vds -Uplink | where {$_.ProxyHost -like $hostname}
    $netSys = Get-View -Id $EsxHost.ExtensionData.ConfigManager.NetworkSystem
    $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].Uuid = $vds.ExtensionData.Uuid
    $config.ProxySwitch[0].ChangeOperation = [VMware.Vim.HostConfigChangeOperation]::edit
    $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[] (2)
    $config.ProxySwitch[0].Spec.Backing.PnicSpec[0] = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicSpec
    $config.ProxySwitch[0].Spec.Backing.PnicSpec[0].PnicDevice = $NicName
    $config.ProxySwitch[0].Spec.Backing.PnicSpec[0].UplinkPortKey = $uplinks | where{$_.Name -eq $UplinkName} | Select -ExpandProperty Key
    $config.ProxySwitch[0].Spec.Backing.PnicSpec[1] = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicSpec
    $config.ProxySwitch[0].Spec.Backing.PnicSpec[1].PnicDevice = $uplink.ConnectedEntity | Where-Object {$_.ConnectedEntity.Name -like “vmnic*”}
    $config.ProxySwitch[0].Spec.Backing.PnicSpec[1].UplinkPortKey = $uplink.Key | Where-Object {$_.ConnectedEntity.Name -like “vmnic*”}
    $changeMode = [VMware.Vim.HostConfigChangeMode]::modify
    $netSys.UpdateNetworkConfig($config, $changeMode)
    Write-Host -ForegroundColor Green “Moved $NicName for $hostname to $NewDVS $UplinkName”
    }
    Disconnect-viserver -confirm:$false

  2. Found a Fix. The lines 28,29 had incorrect syntax correct code below
    $config.ProxySwitch[0].Spec.Backing.PnicSpec[1].PnicDevice = ($uplinks| Where-Object {$_.ConnectedEntity.Name -like “vmnic*”}).ConnectedEntity
    $config.ProxySwitch[0].Spec.Backing.PnicSpec[1].UplinkPortKey = ($uplinks| Where-Object {$_.ConnectedEntity.Name -like “vmnic*”}).Key

Comments are closed.