建立 VM 並連線至租用戶虛擬網路或 VLAN

適用於:Windows Server 2022、Windows Server 2019、Windows Server 2016、Azure Stack HCI 21H2 和 20H2 版本

在本主題中,您會建立租用戶 VM,並將其連線到您使用 Hyper-V 網路虛擬化建立的虛擬網路,或連線到虛擬區域網路 (VLAN)。 您可以使用 Windows PowerShell 網路控制卡 Cmdlet 連線到虛擬網路,或使用 NetworkControllerRESTWrappers 來連線到 VLAN。

使用本主題所述的程序來部署虛擬設備。 您可以透過一些額外的步驟,設定設備來處理或檢查虛擬網路上流入或流出其他 VM 的資料封包。

本主題的章節會提供範例 Windows PowerShell 命令,其中包含許多參數的範例值。 在執行這些命令之前,請務必先將這些命令中的範例值取代為您部署所需的值。

必要條件

  1. 會在 VM 存留期使用靜態 MAC 位址建立 VM 網路介面卡。

    如果 MAC 位址在 VM 存留期期間變更,則網路控制卡無法設定網路介面卡的必要原則。 未設定網路的原則可防止網路介面卡處理網路流量,且與網路的所有通訊都會失敗。

  2. 如果 VM 在啟動時需要網路存取權,在設定 VM 網路介面卡連接埠上的介面識別碼之前,請勿啟動 VM。 如果您在設定介面識別碼之前啟動 VM,且網路介面不存在,VM 就無法在網路控制卡的網路上通訊,並且套用所有原則。

  3. 如果您需要此網路介面的自訂 ACL,請使用使用存取控制清單 (ACL) 來管理資料中心網路流量主題中的指示,立即建立 ACL

使用這個範例命令之前,請確定您已建立虛擬網路。 如需詳細資訊,請參閱建立、刪除或更新租用戶虛擬網路

使用 Windows PowerShell 網路控制卡 Cmdlet 建立 VM 並連線到虛擬網路

  1. 使用具有靜態 MAC 位址的 VM 網路介面卡來建立 VM。

    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"
    
  2. 取得虛擬網路,當中包含您要連線網路介面卡的子網路。

    $vnet = Get-NetworkControllerVirtualNetwork -ConnectionUri $uri -ResourceId "Contoso_WebTier"
    
  3. 在網路控制卡中建立網路介面物件。

    提示

    在此步驟中,您會使用自訂 ACL。

    $vmnicproperties = New-Object Microsoft.Windows.NetworkController.NetworkInterfaceProperties
    $vmnicproperties.PrivateMacAddress = "001122334455"
    $vmnicproperties.PrivateMacAllocationMethod = "Static"
    $vmnicproperties.IsPrimary = $true
    
    $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
    
  4. 從網路控制卡取得網路介面的 InstanceId。

     $nic = Get-NetworkControllerNetworkInterface -ConnectionUri $uri -ResourceId "MyVM_Ethernet1"
    
  5. 在 Hyper-V VM 網路介面卡連接埠上設定介面識別碼。

    注意

    您必須在安裝 VM 的 Hyper-V 主機上執行這些命令。

    #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 $vmNics
    }
    
  6. 啟動 VM。

     Get-VM -Name "MyVM" | Start-VM
    

您已成功建立 VM、將 VM 連線至租用戶虛擬網路,並啟動 VM,以便處理租用戶工作負載。

使用 NetworkControllerRESTWrappers 建立 VM 並連線到 VLAN

  1. 建立 VM,並將靜態 MAC 位址指派給 VM。

    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"
    
  2. 在 VM 網路介面卡上設定 VLAN 識別碼。

    Set-VMNetworkAdapterIsolation –VMName "MyVM" -AllowUntaggedTraffic $true -IsolationMode VLAN -DefaultIsolationId 123
    
  3. 取得邏輯網路子網路並建立網路介面。

     $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.IsPrimary = $true
    
     $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
    
  4. 在 Hyper-V 連接埠上設定 InstanceId。

    #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 $vmNics
    
    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 $vmNics
    } else {
        $CurrentFeature.SettingData.ProfileId = "{$InstanceId}"
        $CurrentFeature.SettingData.ProfileData = 1
    
        Set-VMSwitchExtensionPortFeature -VMSwitchExtensionFeature $CurrentFeature  -VMNetworkAdapter $vmNics
    }
    
  5. 啟動 VM。

    Get-VM -Name "MyVM" | Start-VM
    

您已成功建立 VM、將 VM 連線至 VLAN,並啟動 VM,以便處理租用戶工作負載。