建立 VM 與網路或 VLAN 連接至 Virtual 房客Create a VM and Connect to a Tenant Virtual Network or VLAN

適用於:Windows Server(以每年次管道)、Windows Server 2016Applies To: Windows Server (Semi-Annual Channel), Windows Server 2016

您可以使用此主題建立房客 virtual 電腦 (VM) 並連接到任一 Virtual 的網路,您的 HYPER-V 網路模擬建立或 virtual 本機的區域網路 (VLAN) VM。You can use this topic to create a tenant virtual machine (VM) and connect the VM to either a Virtual Network that you created with Hyper-V Network Virtualization or to a virtual Local Area Network (VLAN).

本主題包含下列各節。This topic contains the following sections.

需求Requirements

之前,請先執行下列區段中的程序,請注意下列需求。Before performing the procedures in the following sections, note the following requirements.

  1. 您必須建立 VM 網路介面卡的靜態媒體存取控制 (MAC) 位址,讓 VM 生命不會變更 VM 的 MAC 地址。You must create VM network adapters with static media access control (MAC) addresses so that the MAC address of the VM does not change during the VM lifetime.

    注意

    如果 VM MAC 位址變更 VM 生命,Network Controller 無法設定所需的網路介面卡的原則。If the VM MAC address changes during the VM lifetime, Network Controller cannot configure the necessary policy for the network adapter. 若並未設定為網路介面卡的原則,將無法處理網路流量的網路介面卡和所有通訊網路的問題將會都失敗。If the policy for the network adapter is not configured, the network adapter is prevented from processing network traffic, and all communication with the network fails.

  2. 如果 VM 需要在開機網路存取權,請務必不開始在最後的設定步驟-上 VM 網路介面卡連接埠設定介面 ID 後 VM。If the VM requires network access on startup, it is important that you do not start the VM until after the final configuration step - setting the Interface ID on the VM network adapter port. 如果您開始 VM,才能完成此步驟,VM 無法通訊網路上 Network Controller 中建立網路介面控制器已套用所有適用的原則-Virtual 的網路原則,直到存取控制清單 (ACLs) 和服務 (QoS) 品質。If you start the VM before you complete this step, the VM cannot communicate on the network until the network interface is created in Network Controller and the controller has applied all applicable policies - Virtual Network policy, access control lists (ACLs), and quality of service (QoS).

您也可以使用的程序部署 virtual 設備本主題中所述。You can also use the processes that are described in this topic for deploying virtual appliances. 有幾個額外的步驟,您可以設定設備處理,或查看 flow 或其他 Vm Virtual 網路上的資料封包。With a few additional steps, you can configure appliances to process or inspect data packets that flow to or from other VMs on the Virtual Network.

重要

以下的各節包含包含許多參數值範例範例 Windows PowerShell 命令。The following sections include example Windows PowerShell commands that contain example values for many parameters. 請確認值是適用於您的部署,執行下列命令之前,先取代範例值這些命令列中。Ensure that you replace example values in these commands with values that are appropriate for your deployment before you run these commands.

建立 VM 及使用 Windows PowerShell Network Controller cmdlet 連接 Virtual 網路Create a VM and connect to a Virtual Network by using the Windows PowerShell Network Controller cmdlets

這一節包含下列主題。This section includes the following topics.

  1. 建立 VM VM 網路介面卡的靜態的 MAC 位址Create a VM with a VM network adapter that has a static MAC address
  2. 取得,其中包含您要的網路介面卡連接子的網路 Virtual 網路Get the Virtual Network that contains the subnet to which you want to connect the network adapter
  3. Network Controller 中建立網路介面物件Create a network interface object in Network Controller
  4. 執行個體識別碼取得 Network Controller 的網路介面Get the InstanceId for the network interface from Network Controller
  5. 設定介面 ID HYPER-V VM 網路介面卡連接埠Set the Interface ID on the Hyper-V VM network adapter port
  6. [開始] VMStart the VM

建立 VM VM 網路介面卡的靜態的 MAC 位址Create a VM with a VM network adapter that has a static MAC address

建立 VM 網路介面卡的靜態的 MAC 位址,請使用下列命令範例。To create a VM with a network adapter that has a static MAC address, use the following example command.

New-VM -Generation 2 -Name "MyVM" -Path "C:\VMs\MyVM" -MemoryStartupBytes 4GB -VHDPath "c:\VMs\MyVM\Virtual Hard Disks\WindowsServer2016.vhdx" -SwitchName "SDNvSwitch" 

Set-VM -Name "MyVM" -ProcessorCount 4

Set-VMNetworkAdapter -VMName "MyVM" -StaticMacAddress "00-11-22-33-44-55" 

取得,其中包含您要的網路介面卡連接子的網路 Virtual 網路Get the Virtual Network that contains the subnet to which you want to connect the network adapter

確定您已經有建立 Virtual 網路才能使用此範例中的命令。Ensure that you have already created a Virtual Network before using this example command. 如需詳細資訊,請查看建立、Delete 或更新承租人 Virtual 網路For more information, see Create, Delete, or Update Tenant Virtual Networks.

若要取得 Virtual 網路,使用下列命令範例。To get the Virtual Network, use the following example command.

$vnet = get-networkcontrollervirtualnetwork -connectionuri $uri -ResourceId “Contoso_WebTier”

注意

如果您需要自訂 Acl 此網路介面,然後建立 ACL 現在主題中使用的指示來使用存取控制清單 (Acl) 來管理 Datacenter 網路流量流程If you require custom ACLs for this network interface, then create the ACL now by using instructions in the topic Use Access Control Lists (ACLs) to Manage Datacenter Network Traffic Flow

Network Controller 中建立網路介面物件Create a network interface object in Network Controller

Network Controller 中建立網路介面物件,請使用下列命令範例。To create a network interface object in Network Controller, use the following example command.

注意

如果您建立自訂 ACL 上述步驟之後,您現在使用。If you created a custom ACL after the previous step, you can use it now.

$vmnicproperties = new-object Microsoft.Windows.NetworkController.NetworkInterfaceProperties
$vmnicproperties.PrivateMacAddress = "001122334455" 
$vmnicproperties.PrivateMacAllocationMethod = "Static" 

$vmnicproperties.DnsSettings = new-object Microsoft.Windows.NetworkController.NetworkInterfaceDnsSettings
$vmnicproperties.DnsSettings.DnsServers = @("24.30.1.11", "24.30.1.12")

$ipconfiguration = new-object Microsoft.Windows.NetworkController.NetworkInterfaceIpConfiguration
$ipconfiguration.resourceid = "MyVM_IP1"
$ipconfiguration.properties = new-object Microsoft.Windows.NetworkController.NetworkInterfaceIpConfigurationProperties
$ipconfiguration.properties.PrivateIPAddress = “24.30.1.101”
$ipconfiguration.properties.PrivateIPAllocationMethod = "Static"

$ipconfiguration.properties.Subnet = new-object Microsoft.Windows.NetworkController.Subnet
$ipconfiguration.properties.subnet.ResourceRef = $vnet.Properties.Subnets[0].ResourceRef

$vmnicproperties.IpConfigurations = @($ipconfiguration)
New-NetworkControllerNetworkInterface –ResourceID “MyVM_Ethernet1” –Properties $vmnicproperties –ConnectionUri $uri

執行個體識別碼取得 Network Controller 的網路介面Get the InstanceId for the network interface from Network Controller

若要執行個體識別碼網路介面 Network Controller,使用下列命令範例。To get the InstanceId for the network interface from Network Controller, use the following example command.

$nic = Get-NetworkControllerNetworkInterface -ConnectionUri $uri -ResourceId "MyVM-Ethernet1"

設定介面 ID HYPER-V VM 網路介面卡連接埠Set the Interface ID on the Hyper-V VM network adapter port

設定介面 ID 上 HYPER-V VM 網路介面卡連接埠,使用下列命令範例。To set the Interface ID on the Hyper-V VM network adapter port, use the following example command.

注意

您必須執行下列命令,HYPER-V 主機上安裝 VM 的位置。You must run these commands on the Hyper-V host where the VM is installed.

#Do not change the hardcoded IDs in this section, because they are fixed values and must not change.

$FeatureId = "9940cd46-8b06-43bb-b9d5-93d50381fd56"

$vmNics = Get-VMNetworkAdapter -VMName “MyVM”

$CurrentFeature = Get-VMSwitchExtensionPortFeature -FeatureId $FeatureId -VMNetworkAdapter $vmNics

if ($CurrentFeature -eq $null)
{
$Feature = Get-VMSystemSwitchExtensionPortFeature -FeatureId $FeatureId

$Feature.SettingData.ProfileId = "{$($nic.InstanceId)}"
$Feature.SettingData.NetCfgInstanceId = "{56785678-a0e5-4a26-bc9b-c0cba27311a3}"
$Feature.SettingData.CdnLabelString = "TestCdn"
$Feature.SettingData.CdnLabelId = 1111
$Feature.SettingData.ProfileName = "Testprofile"
$Feature.SettingData.VendorId = "{1FA41B39-B444-4E43-B35A-E1F7985FD548}"
$Feature.SettingData.VendorName = "NetworkController"
$Feature.SettingData.ProfileData = 1

Add-VMSwitchExtensionPortFeature -VMSwitchExtensionFeature  $Feature -VMNetworkAdapter $vmNics
}
else
{
$CurrentFeature.SettingData.ProfileId = "{$($nic.InstanceId)}"
$CurrentFeature.SettingData.ProfileData = 1

Set-VMSwitchExtensionPortFeature -VMSwitchExtensionFeature $CurrentFeature  -VMNetworkAdapter $vmNic
}

[開始] VMStart the VM

若要開始 VM 中,使用下列命令範例。To start the VM, use the following example command.

Get-VM -Name “MyVM” | Start-VM 

您現在順利建立 VM、連接 VM 房客 Virtual 網路,並開始 VM,使其可以處理承租人工作負載。You have now successfully created a VM, connected the VM to a tenant Virtual Network, and started the VM so that it can process tenant workloads.

建立 VM,以及連接至 VLAN 使用 NetworkControllerRESTWrappersCreate a VM and connect to a VLAN by using NetworkControllerRESTWrappers

這一節包含下列主題。This section includes the following topics.

  1. 建立 VM 和指派靜態 MAC 位址Create the VM and assign a static MAC address
  2. 設定 VLAN ID VM 網路介面卡Set the VLAN ID on the VM network adapter
  3. 取得的邏輯網路子網路,並建立網路介面Get the logical network subnet and create the network interface
  4. 執行個體識別碼設定 HYPER-V 連接埠Set the InstanceId on the Hyper-V port
  5. [開始] VMStart the VM

建立 VM 和指派靜態 MAC 位址Create the VM and assign a static MAC address

建立 VM,並將靜態媒體存取控制 (MAC) 位址指派給 VM 中,您可以使用下列命令範例。To create a VM and assign a static media access control (MAC) address to the VM, you can use the following example commands.

New-VM -Generation 2 -Name "MyVM" -Path "C:\VMs\MyVM" -MemoryStartupBytes 4GB -VHDPath "c:\VMs\MyVM\Virtual Hard Disks\WindowsServer2016.vhdx" -SwitchName "SDNvSwitch" 

Set-VM -Name "MyVM" -ProcessorCount 4

Set-VMNetworkAdapter -VMName "MyVM" -StaticMacAddress "00-11-22-33-44-55" 

設定 VLAN ID VM 網路介面卡Set the VLAN ID on the VM network adapter

若要設定 VLAN ID 的網路介面卡,您可以使用下列範例命令。To set the VLAN ID on the network adapter, you can use the following example command.

Set-VMNetworkAdapterIsolation –VMName “MyVM” -AllowUntaggedTraffic $true -IsolationMode VLAN -DefaultIsolationId 123

取得的邏輯網路子網路,並建立網路介面Get the logical network subnet and create the network interface

取得的邏輯網路子網路,並建立網路介面使用的邏輯網路子網路,您可以使用下列命令範例。To obtain the logical network subnet and create the network interface using the logical network subnet, you can use the following example commands.

$logicalnet = get-networkcontrollerLogicalNetwork -connectionuri $uri -ResourceId "00000000-2222-1111-9999-000000000002"

$vmnicproperties = new-object Microsoft.Windows.NetworkController.NetworkInterfaceProperties
$vmnicproperties.PrivateMacAddress = "00-1D-C8-B7-01-02"
$vmnicproperties.PrivateMacAllocationMethod = "Static"

$vmnicproperties.DnsSettings = new-object Microsoft.Windows.NetworkController.NetworkInterfaceDnsSettings
$vmnicproperties.DnsSettings.DnsServers = $logicalnet.Properties.Subnets[0].DNSServers

$ipconfiguration = new-object Microsoft.Windows.NetworkController.NetworkInterfaceIpConfiguration
$ipconfiguration.resourceid = "MyVM_Ip1"
$ipconfiguration.properties = new-object Microsoft.Windows.NetworkController.NetworkInterfaceIpConfigurationProperties
$ipconfiguration.properties.PrivateIPAddress = “10.127.132.177”
$ipconfiguration.properties.PrivateIPAllocationMethod = "Static"

$ipconfiguration.properties.Subnet = new-object Microsoft.Windows.NetworkController.Subnet
$ipconfiguration.properties.subnet.ResourceRef = $logicalnet.Properties.Subnets[0].ResourceRef

$vmnicproperties.IpConfigurations = @($ipconfiguration)
$vnic = New-NetworkControllerNetworkInterface –ResourceID “MyVM_Ethernet1” –Properties $vmnicproperties –ConnectionUri $uri

$vnic.InstanceId

執行個體識別碼設定 HYPER-V 連接埠Set the InstanceId on the Hyper-V port

若要設定的執行個體識別碼 HYPER-V 連接埠,您可以使用下列命令範例 HYPER-V 主機上 VM 所在的位置。To set the InstanceId on the Hyper-V port, you can use the following example commands on the Hyper-V host where the VM is located.

#The hardcoded Ids in this section are fixed values and must not change.
$FeatureId = "9940cd46-8b06-43bb-b9d5-93d50381fd56"

$vmNics = Get-VMNetworkAdapter -VMName “MyVM”

$CurrentFeature = Get-VMSwitchExtensionPortFeature -FeatureId $FeatureId -VMNetworkAdapter $vmNic

if ($CurrentFeature -eq $null)
{
    $Feature = Get-VMSystemSwitchExtensionFeature -FeatureId $FeatureId

    $Feature.SettingData.ProfileId = "{$InstanceId}"
    $Feature.SettingData.NetCfgInstanceId = "{56785678-a0e5-4a26-bc9b-c0cba27311a3}"
    $Feature.SettingData.CdnLabelString = "TestCdn"
    $Feature.SettingData.CdnLabelId = 1111
    $Feature.SettingData.ProfileName = "Testprofile"
    $Feature.SettingData.VendorId = "{1FA41B39-B444-4E43-B35A-E1F7985FD548}"
    $Feature.SettingData.VendorName = "NetworkController"
    $Feature.SettingData.ProfileData = 1

    Add-VMSwitchExtensionFeature -VMSwitchExtensionFeature  $Feature -VMNetworkAdapter $vmNic
}        
else
{
    $CurrentFeature.SettingData.ProfileId = "{$InstanceId}"
    $CurrentFeature.SettingData.ProfileData = 1

    Set-VMSwitchExtensionPortFeature -VMSwitchExtensionFeature $CurrentFeature  -VMNetworkAdapter $vmNic
}

[開始] VMStart the VM

若要開始 VM 中,您可以使用下列範例命令。To start the VM, you can use the following example command.

Get-VM -Name “MyVM” | Start-VM 

您現在順利建立 VM、連接 VM VLAN,並開始 VM,使其可以處理承租人工作負載。You have now successfully created a VM, connected the VM to a VLAN, and started the VM so that it can process tenant workloads.