VMware Cloud Community
vin01
Expert
Expert
Jump to solution

set dv portgroup for a vm

I am trying to set port group for a vm by specifying Set-NetworkAdapter -Portgroup but its failing. However if i specify as like this its working Set-NetworkAdapter -NetworkName

Not sure what is the difference?

$templatevm= Get-VM 'Win2k16_Std_64bit-testClone'

$dhcpportgroup= Get-View -Id $templatevm.VMHost.ExtensionData.Network |?{$_.config.DefaultPortConfig.Vlan.VlanId -eq '2067'}

#$dhcpportgroup.Name

Get-NetworkAdapter -VM $templatevm.Name |Set-NetworkAdapter -Portgroup $dhcpportgroup.Name -Confirm:$false |fl

pastedImage_0.png

Regards Vineeth.K
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Ok, I hadn't looked at your code in detail.
You are trying to use a vSphere object (returned by Get-View) where the parameter expects a .NET object (returned by Get-VirtualPortgroup).

Try like this

$templatevm= Get-VM 'Win2k16_Std_64bit-testClone'

$dhcpportgroup = Get-VirtualPortGroup -VM $templatevm -Distributed | where{$_.VlanId -eq 2067}

Get-NetworkAdapter -VM $templatevm.Name |Set-NetworkAdapter -Portgroup $dhcpportgroup -Confirm:$false


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Which PowerCLI version are you using?

Did you try without specifying the Name property, like -PortGroup $dhcpPortgroup?


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

0 Kudos
vin01
Expert
Expert
Jump to solution

Which PowerCLI version are you using?

pastedImage_1.png

Did you try without specifying the Name property, like -PortGroup $dhcpPortgroup?

yes.

pastedImage_0.png

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, I hadn't looked at your code in detail.
You are trying to use a vSphere object (returned by Get-View) where the parameter expects a .NET object (returned by Get-VirtualPortgroup).

Try like this

$templatevm= Get-VM 'Win2k16_Std_64bit-testClone'

$dhcpportgroup = Get-VirtualPortGroup -VM $templatevm -Distributed | where{$_.VlanId -eq 2067}

Get-NetworkAdapter -VM $templatevm.Name |Set-NetworkAdapter -Portgroup $dhcpportgroup -Confirm:$false


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

0 Kudos
vin01
Expert
Expert
Jump to solution

Yes you are right I am trying to use a vSphere object (returned by Get-View). This time I tried like this and it worked.

$templatevm= Get-VM 'Win2k16_Std_64bit-testClone'

$pg1=Get-VirtualPortGroup -VMHost $templatevm.VMHost.Name

$dhcpportgroup=Get-VirtualPortGroup -VMHost $templatevm.VMHost |?{$_.ExtensionData.config.DefaultPortConfig.Vlan.VlanId -eq '2067'}

Get-NetworkAdapter -VM $templatevm.Name |Set-NetworkAdapter -Portgroup $dhcpportgroup -Confirm:$false

Regards Vineeth.K
0 Kudos