使用 PowerCLI 向 vCloud Director 中的虚拟机添加网络适配器

使用 PowerCLI 向 vCloud Director 中的虚拟机添加网络适配器

我正在使用 PowerCLI 版本 12,并且有一个当前没有网络适配器的虚拟机。

有 CmdletGet-CINetworkAdapter但是它返回 null(因为没有适配器)并且有Set-CINetworkAdapter但是如果没有 CINetworkAdapter 可以传递则不起作用。

我是否New-CINetworkAdapter遗漏了什么地方?

答案1

这是我目前用来执行此操作的代码:

$NIC = New-Object -TypeName 'VMware.VimAutomation.Cloud.Views.OvfRASD'
$NIC.AddressOnParent = '0'
$NIC.AutomaticAllocation = New-Object -TypeName 'VMware.VimAutomation.Cloud.Views.CimBoolean'
$NIC.AutomaticAllocation.Value = $true
$NICConnectionString = New-Object -TypeName 'VMware.VimAutomation.Cloud.Views.CimString'
$connectionAttributes = New-Object 'VMware.VimAutomation.Cloud.Views.XmlAttributeCollection'
$connectionAttributes.Add('ns5','ipAddressingMode','http://www.vmware.com/vcloud/v1.5','POOL')
$connectionAttributes.Add('ns5','primaryNetworkConnection','http://www.vmware.com/vcloud/v1.5','true')
$NICConnectionString.AnyAttr = $connectionAttributes
$NICConnectionString.Value = 'None'
$NIC.Connection = $NICConnectionString
$NIC.Description = 'VMXNET3 ethernet adapter on ''none'''
$NIC.ElementName = 'Network adapter 0'
$NIC.InstanceID = '0'
$NIC.ResourceSubType = 'VMXNET3'
$NICResourceType = New-Object -TypeName 'VMware.VimAutomation.Cloud.Views.CimRASDResource'
$NICResourceType.Value = '10'
$NIC.ResourceType = $NICResourceType

$vm = Get-CIVM -Name '[VM Name]'
$vmVirtualHardwareSection = $vm.ExtensionData.GetVirtualHardwareSection()
$vmNetworkCards = $vmVirtualHardwareSection.GetNetworkCards()
$vmNetworkCards.Item = $NIC
$vmNetworkCards.UpdateServerData()

相关内容