VMware Cloud Community
KingRaymond
Contributor
Contributor

Cannot get my Function to work correctly

Hi, 

I have this created this function (inspired by something i found from @lucd - obviously), which basically allows me to assign a vmnic to a specific uplink on a distributed switch.

Running the function, the first time, it works as advertised and adds the vmnic to the correct Uplink Port on the correct distrubuted switch. However, when I run it a second time, to add a different vmnic to a different uplink port,  this also works (e.g. attaches the vmnic to the correct uplink), however, it will remove the previous vmnic that was previously attached.

Any ideas what I could be doing wrong or what I am missing?

 

TIA

 

Function Set-VDSProperties { 
    <# 
        This function will configure vDS  
    #>
    Param(
        [parameter(Mandatory=$true)]
        [String]
        $uplinkName,

        [parameter(Mandatory=$true)]
        [String]
        $server,

        [parameter(Mandatory=$true)]
        [String]
        $vdswitch,

        [parameter(Mandatory=$true)]
        [String]
        $vmnic
    )
    
$esx = Get-VMHost -Name $Server #changed
$vds = Get-VDSwitch -Name $vdswitch -VMHost $esx
$uplinks = Get-VDPort -VDSwitch $vds -Uplink | Where-Object {$_.ProxyHost -like $Server} #changed
$netSys = Get-View -Id $esx.ExtensionData.ConfigManager.NetworkSystem
$config = New-Object VMware.Vim.HostNetworkConfig
$proxy = New-Object VMware.Vim.HostProxySwitchConfig
$proxy.Uuid = $vds.ExtensionData.Uuid
$proxy.ChangeOperation = [VMware.Vim.HostConfigChangeOperation]::edit
$proxy.Spec = New-Object VMware.Vim.HostProxySwitchSpec
$proxy.Spec.Backing = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicBacking
$pnic = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicSpec
$pnic.PnicDevice = $vmnic #changed
$pnic.UplinkPortKey = $uplinks | Where-Object{$_.Name -eq $uplinkName} | Select-Object -ExpandProperty Key
$proxy.Spec.Backing.PnicSpec += $pnic
$config.ProxySwitch += $proxy
$netSys.UpdateNetworkConfig($config,[VMware.Vim.HostConfigChangeMode]::modify)


}

0 Kudos
4 Replies
LucD
Leadership
Leadership

Can you show exactly you call this function multiple times?

And especially the parameters you pass to the function.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
KingRaymond
Contributor
Contributor

Hi Luc

The function gets defined right at the top of the script I am building, then further down, after adding te ESXi host to the distributed switches by doing:

$addhost = get-vmhost $ESXiServer
       Get-VDSwitch -Name $dvSwitch2 | Add-VDSwitchVMHost -VMHost  $addhost

I add the "second nic" to the "second uplink port"

     Set-VDSProperties -uplinkName $UplinkNameB -server $Server -vdswitch $dvSwitch2 -vmnic $vmnicB

Then I do a whole lot of other stuff in my script, and just before I complete the script, I add the "first nic" to the "first uplink port"

     Set-VDSProperties -uplinkName $UplinkNameA -server $Server -vdswitch $dvSwitch2 -vmnic $vmnicA

 

I also experience the exact same symptoms if I manually define the relevant variables, and run the function line by line.

     Set-VDSProperties -uplinkName $UplinkNameB -server $Server -vdswitch $dvSwitch2 -vmnic $vmnicB
0 Kudos
LucD
Leadership
Leadership

It is not immediately clear from the documentation, but besides the new Uplink mapping, you also have to provide existing uplinks.

Adapt the function like this

Function Set-VDSProperties

{

    <#

        This function will configure vDS 

    #>

    Param(

        [parameter(Mandatory = $true)]

        [String]

        $uplinkName,


        [parameter(Mandatory = $true)]

        [String]

        $server,


        [parameter(Mandatory = $true)]

        [String]

        $vdswitch,


        [parameter(Mandatory = $true)]

        [String]

        $vmnic

    )

  

    $esx = Get-VMHost -Name $Server #changed

    $vds = Get-VDSwitch -Name $vdswitch -VMHost $esx

    $uplinks = Get-VDPort -VDSwitch $vds -Uplink | Where-Object { $_.ProxyHost -like $Server } #changed

    $netSys = Get-View -Id $esx.ExtensionData.ConfigManager.NetworkSystem

    $config = New-Object VMware.Vim.HostNetworkConfig

    $proxy = $netSys.NetworkConfig.ProxySwitch | where { $_.Uuid -eq $vds.ExtensionData.Config.Uuid }

    $proxy.ChangeOperation = [VMware.Vim.HostConfigChangeOperation]::edit

    $pnic = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicSpec

    $pnic.PnicDevice = $vmnic #changed

    $pnic.UplinkPortKey = $uplinks | Where-Object { $_.Name -eq $uplinkName } | Select-Object -ExpandProperty Key

    $proxy.Spec.Backing.PnicSpec += $pnic

    $config.ProxySwitch += $proxy

    $netSys.UpdateNetworkConfig($config, [VMware.Vim.HostConfigChangeMode]::modify)

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
KingRaymond
Contributor
Contributor

HA! Amazing! That worked!

Will now need to run it as part of the script, will all adapters. Here's to holding thumbs. 

Thanks @luc

0 Kudos