I have another question based on the example of adding a virtual switch. What if you also wanted to set the nicTeaming policy of the vSwitch to loadbalance_ip and network failover detection to Link Status Only? I see that those properities are under HostNetworkPolicy, but I'm not sure how you would use that to add that information to this. I'm probably just making this a lot harder than it actually is.
$HS = Find-EntityView -ViewType "HostSystem"
$nwSys = $HS.ConfigManager.NetworkSystem
$mor = Get-View -MoRef $nwSys
$networkConfig = New-Object Vmware.Vim.HostNetworkConfig
$vswtch = New-Object VMware.Vim.HostVirtualSwitchConfig
$networkConfig.vswitch = @($vswtch)
$networkConfig.vswitch[0].name = "SwitchName"
$spec = New-Object Vmware.Vim.HostVirtualSwitchSpec
$spec.numPorts = 1
$portgrp = New-Object VMware.Vim.HostPortGroupConfig
$networkConfig.portgroup = @($portgrp)
$portgropspec = New-Object VMware.Vim.HostPortGroupSpec
$portgropspec.vswitchName = "SwitchName"
$portgropspec.Name = "SwitchNamePort"
$portgropspec.Policy = New-Object VMware.Vim.HostNetworkPolicy
$networkConfig.vswitch[0].spec = $spec
$networkConfig.portgroup[0].spec = $portgropspec
$mor.AddVirtualSwitch("SwitchName",$spec ) $mor.AddPortGroup($portgropspec)
Thanks,Jaime
As you probably know NIC teaming can be defined on the switch and on the portgroup.
This example shows how it is done on a portgroup like you asked.
I left out the vSwitch objects and method so as not confuse you.
$HS = Get-View -ViewType "HostSystem" -Filter @{"Name" = <ESX-name>} $nwSys = $HS.ConfigManager.NetworkSystem $mor = Get-View $nwSys $portgropspec = New-Object VMware.Vim.HostPortGroupSpec $portgropspec.vswitchName = <vswitch-name> $portgropspec.Name = <portgroup-name> $portgropspec.Policy = New-Object VMware.Vim.HostNetworkPolicy $portgropspec.Policy.NicTeaming = New-Object VMware.Vim.HostNicTeamingPolicy $portgropspec.Policy.NicTeaming.failureCriteria = New-Object VMware.Vim.HostNicFailureCriteria $portgropspec.Policy.NicTeaming.failureCriteria.checkBeacon = $false $portgropspec.Policy.NicTeaming.failureCriteria.checkDuplex = $false $portgropspec.Policy.NicTeaming.failureCriteria.checkErrorPercent = $false $portgropspec.Policy.NicTeaming.nicOrder = New-Object VMware.Vim.HostNicOrderPolicy $portgropspec.Policy.NicTeaming.nicOrder.activeNic = @(<NIC1-name>) $portgropspec.Policy.NicTeaming.nicOrder.standbyNic = @(<NIC2-name>) $portgropspec.Policy.NicTeaming.policy = "loadbalance_ip" $mor.AddPortGroup($portgropspec)
Note1: in v1 of the VITK the functionality of the Find-EntityView cmdlet has been replaced by the Get-View cmdlet
Note2: for <vswitch-name> you use the name of an existing switch
Note3: "Link Status Only" is the default setting. All flags in the failureCriteria object are set to $false
Note4: for NIC1-name and NIC2-name you use 2 NICs that are already allocated to the vswitch
Note5: the choice for Active-Active or Active-Standby is up to you. If you place the 2 NICs in the activeNic array , as @(<NIC1-name>,<NIC2-name>), you will have Active-Active teaming.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
As you probably know NIC teaming can be defined on the switch and on the portgroup.
This example shows how it is done on a portgroup like you asked.
I left out the vSwitch objects and method so as not confuse you.
$HS = Get-View -ViewType "HostSystem" -Filter @{"Name" = <ESX-name>} $nwSys = $HS.ConfigManager.NetworkSystem $mor = Get-View $nwSys $portgropspec = New-Object VMware.Vim.HostPortGroupSpec $portgropspec.vswitchName = <vswitch-name> $portgropspec.Name = <portgroup-name> $portgropspec.Policy = New-Object VMware.Vim.HostNetworkPolicy $portgropspec.Policy.NicTeaming = New-Object VMware.Vim.HostNicTeamingPolicy $portgropspec.Policy.NicTeaming.failureCriteria = New-Object VMware.Vim.HostNicFailureCriteria $portgropspec.Policy.NicTeaming.failureCriteria.checkBeacon = $false $portgropspec.Policy.NicTeaming.failureCriteria.checkDuplex = $false $portgropspec.Policy.NicTeaming.failureCriteria.checkErrorPercent = $false $portgropspec.Policy.NicTeaming.nicOrder = New-Object VMware.Vim.HostNicOrderPolicy $portgropspec.Policy.NicTeaming.nicOrder.activeNic = @(<NIC1-name>) $portgropspec.Policy.NicTeaming.nicOrder.standbyNic = @(<NIC2-name>) $portgropspec.Policy.NicTeaming.policy = "loadbalance_ip" $mor.AddPortGroup($portgropspec)
Note1: in v1 of the VITK the functionality of the Find-EntityView cmdlet has been replaced by the Get-View cmdlet
Note2: for <vswitch-name> you use the name of an existing switch
Note3: "Link Status Only" is the default setting. All flags in the failureCriteria object are set to $false
Note4: for NIC1-name and NIC2-name you use 2 NICs that are already allocated to the vswitch
Note5: the choice for Active-Active or Active-Standby is up to you. If you place the 2 NICs in the activeNic array , as @(<NIC1-name>,<NIC2-name>), you will have Active-Active teaming.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Awesome, thanks for helping me out again. I am using v1 so I have been using the Get-View cmlet. I just copied the example from the FAQ page.
Jaime
Could someone give me the proper usage of updatevirtualswitch? I have the code below, but cannot figure out how to tell it to update vSwitch1 using $vswitchspec. I see that Updatevirtualswitch has parameters of vswitchname and spec, but I can't seem to make this work.
$nwsys = Get-View($HostView.ConfigManager.NetworkSystem)
$vswitchspec = New-Object Vmware.Vim.HostVirtualSwitchSpec
$vswitchspec.Policy = New-Object VMware.Vim.HostNetworkPolicy
$vswitchspec.Policy.NicTeaming = New-Object VMware.Vim.HostNicTeamingPolicy
$vswitchspec.Policy.NicTeaming.failureCriteria = New-Object VMware.Vim.HostNicFailureCriteria
$vswitchspec.Policy.NicTeaming.failureCriteria.checkBeacon = $false
$vswitchspec.Policy.NicTeaming.failureCriteria.checkDuplex = $false
$vswitchspec.Policy.NicTeaming.failureCriteria.checkErrorPercent = $false
$vswitchspec.Policy.NicTeaming.nicOrder = New-Object VMware.Vim.HostNicOrderPolicy
$vswitchspec.Policy.NicTeaming.nicOrder.activeNic = @("vmnic0","vmnic1")
$vswitchspec.Policy.NicTeaming.policy = "loadbalance_ip"
$nwsys.UpdateVirtualSwitch <==== What goes here??
Thanks,Jaime
The UpdateVirtualSwitch method needs 2 parameters.
1. The name of the vSwitch you are updating, for example "vswitch0"
2. A HostVirtualSwitchSpec object
The last line would be something like this
$nwsys.UpdateVirtualSwitch("vswitch0",$vswitchspec)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Now I'm getting this:
Exception calling "UpdateVirtualSwitch" with "2" argument(s): "A specified parameter was not correct.
"
At line 39, position 27
$nwsys.UpdateVirtualSwitch("vSwitch1",$vswitchspec)
My Code:
$nwsys = Get-View($HostView.ConfigManager.NetworkSystem)
$vswitchspec = New-Object Vmware.Vim.HostVirtualSwitchSpec
$vswitchspec.Policy = New-Object VMware.Vim.HostNetworkPolicy
$vswitchspec.Policy.NicTeaming = New-Object VMware.Vim.HostNicTeamingPolicy
$vswitchspec.Policy.NicTeaming.failureCriteria = New-Object VMware.Vim.HostNicFailureCriteria
$vswitchspec.Policy.NicTeaming.failureCriteria.checkBeacon = $false
$vswitchspec.Policy.NicTeaming.failureCriteria.checkDuplex = $false
$vswitchspec.Policy.NicTeaming.failureCriteria.checkErrorPercent = $false
$vswitchspec.Policy.NicTeaming.nicOrder = New-Object VMware.Vim.HostNicOrderPolicy
$vswitchspec.Policy.NicTeaming.nicOrder.activeNic = @("vmnic0","vmnic1")
$vswitchspec.Policy.NicTeaming.policy = "loadbalance_ip"
$nwsys.UpdateVirtualSwitch("vSwitch1",$vswitchspec)
There must be something wrong or missing in the $vswitchspec parameter.
If you look at the HostVirtualSwitchSpec page in the SDK API Reference you will notice that some of the properties "need not be set".
In this case I suspect that the $vswitchspec.numPorts property needs to be set.
You can first query the switch and enter the same number of ports in $vswitchspec.numPorts.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I added that number of ports, but it still fails with the same error. I'm not sure what could be causing this. Here's my output
Apparently it does not like the policy portion of the script. If I remove all of that and only include the "$vswitchspec.numPorts = 56" line the script runs fine. I'm not sure why it won't take the policy part, my output shows it setting the values as show in the image i posted in my previous post.
It seems I forgot to post a reply to this, sorry about that.
Hal encountered the same problem.
See
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for the follow up and answer.