VMware Cloud Community
johndavidd
Enthusiast
Enthusiast
Jump to solution

Add Host to Distributed Virtual Switch

Trying to automate the addition of a new host to an existing dVs.

Anyone know the syntax? I was not able to find it in my inital search.

Thanks,

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

When there are 2 pNICs per ESXi host then you could do

$dvSw = Get-VirtualSwitch -Distributed -Name dvSw1 
$esx
= Get-VMHost -Name MyEsx
$pnics
= "vmnic4","vmnic5"
Add-dvSwHost
-dvswitch $dvsW.ExtensionData -hostmoref $esx.ExtensionData.MoRef -pnic $pnics -nruplink $pnics.Count


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

View solution in original post

0 Kudos
14 Replies
LucD
Leadership
Leadership
Jump to solution

You can have a look at my dvSwitch scripting – Part 1 – Creation post and the Add-dvSwHost function in there


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

0 Kudos
johndavidd
Enthusiast
Enthusiast
Jump to solution

Thanks, I skimmed through that earlier and must have missed it.

I will give it a try.

As always, thanks for your prompt reply.

0 Kudos
johndavidd
Enthusiast
Enthusiast
Jump to solution

Couple of questions about the params

I am attmepting to use like this:

$dvs = get-virtualswitch -distributed

$moref = (get-vmhost test1).id

add-dvSwHost -hostmoref $moref -dvswitch $dvs

Would this work? And what values do i need to use for pnic and nruplink?

Thanks,

0 Kudos
johndavidd
Enthusiast
Enthusiast
Jump to solution

In addition to the previous post let me explain what i am trying to do....

I am trying to automate the addition of a host to a dVs and then add VMK ports to that dVs.

Am i better off just creating a vSwitch and manually adding the host to the dVs and migrating the port groups?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It isn't too difficult to automate this.

In line 57 of my script I create the dvSwitch, the New-dvSwitch function returns a MoRef to the dvSwitch that was just created.

In line 61 the script finds the ESXi that can be connected to this dvSwitch.

In lines 62-82 the script finds free pNIC on the ESXi that can connect to the dvSwitch

And in line 84 the ESXi is added to the dvSwitch, provided there are enough pNICs available on that ESXi host.

But if you already have the dvSwitch created and you know which pNIC to use, you can do something like this

$dvSw = Get-VirtualSwitch -Distributed -Name dvSw1 
$esx = Get-VMHost -Name MyEsx
Add-dvSwHost
-dvswitch $dvsW.ExtensionData.MoRef -hostmoref $esx.ExtensionData.MoRef -pnic "vmnic4" -nruplink 1

You will have to repeat this for each ESXi you want to connect to the dvSwitch.

Make sure you have sufficient uplink ports defined when you create the dvSwitch


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

0 Kudos
johndavidd
Enthusiast
Enthusiast
Jump to solution

what does your nruplink specify?

How about adding the VMK ports to the dVs once I have the host added? Is that supported with a native command or another custom function?

0 Kudos
johndavidd
Enthusiast
Enthusiast
Jump to solution

each host will have 2 pNICs connected and three VMK ports.... just some added info.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

With the nrUplink parameter you specify how many pNICs are to be connected for that specific ESXi host.

I have a function for adding a vmkernel portgroup in my dvSwitch scripting – Part 3 – Service Console & vmKernel post.


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

When there are 2 pNICs per ESXi host then you could do

$dvSw = Get-VirtualSwitch -Distributed -Name dvSw1 
$esx
= Get-VMHost -Name MyEsx
$pnics
= "vmnic4","vmnic5"
Add-dvSwHost
-dvswitch $dvsW.ExtensionData -hostmoref $esx.ExtensionData.MoRef -pnic $pnics -nruplink $pnics.Count


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

0 Kudos
johndavidd
Enthusiast
Enthusiast
Jump to solution

Your expertise and wealth of knowledge is always appreciated.

Question has been answered... THANKS! Smiley Happy

0 Kudos
johndavidd
Enthusiast
Enthusiast
Jump to solution

getting the follwing error when running

Something to mention is I only loaded the one function add-dvswhost ... as i did not need the new piece.

Method invocation failed because [VMware.Vim.ManagedObjectReference] doesn't co
ntain a method named 'UpdateViewData'.
At line:13 char:25
+ $dvSwitch.UpdateViewData <<<< ()
    + CategoryInfo          : InvalidOperation: (UpdateViewData:String) [], Ru
   ntimeException
    + FullyQualifiedErrorId : MethodNotFound

Method invocation failed because [VMware.Vim.ManagedObjectReference] doesn't co
ntain a method named 'ReconfigureDvs_Task'.
At line:15 char:43
+ $taskMoRef = $dvSwitch.ReconfigureDvs_Task <<<< ($spec)
    + CategoryInfo          : InvalidOperation: (ReconfigureDvs_Task:String) [
   ], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Get-View : Cannot validate argument on parameter 'VIObject'. The argument is nu
ll or empty. Supply an argument that is not null or empty and then try the comm
and again.
At line:16 char:17
+ $task = Get-View <<<<  $taskMoRef
    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingVal
   idationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom
   ation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

0 Kudos
LucD
Leadership
Leadership
Jump to solution

My mistake, the dvSwitch parameter expects the object not the MoRef.

Like this

Add-dvSwHost -dvswitch $dvsW.ExtensionData -hostmoref $esx.ExtensionData.MoRef -pnic $pnics -nruplink $pnics.Count


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

0 Kudos
johndavidd
Enthusiast
Enthusiast
Jump to solution

That did the trick!!

Now on to adding the VMK ports.. I will post in another thread if I have issues.

Thanks again.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

No problem, feel free to ask


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

0 Kudos