![使用 PowerCLI 向 vCloud Director 中的虚拟机添加网络适配器](https://linux22.com/image/751718/%E4%BD%BF%E7%94%A8%20PowerCLI%20%E5%90%91%20vCloud%20Director%20%E4%B8%AD%E7%9A%84%E8%99%9A%E6%8B%9F%E6%9C%BA%E6%B7%BB%E5%8A%A0%E7%BD%91%E7%BB%9C%E9%80%82%E9%85%8D%E5%99%A8.png)
我正在使用 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()