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
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
$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
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
Which PowerCLI version are you using?
Did you try without specifying the Name property, like -PortGroup $dhcpPortgroup?
yes.
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
$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
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