使用 PowerShell 配置 ExpressRoute 的虚拟网络网关Configure a virtual network gateway for ExpressRoute using PowerShell

本文指导你为预先存在的 VNet 添加虚拟网络 (VNet) 网关、重设网关大小以及删除网关。This article helps you add, resize, and remove a virtual network (VNet) gateway for a pre-existing VNet. 此配置的步骤适用于使用资源管理器部署模型创建的 VNet(针对 ExpressRoute 配置)。The steps for this configuration apply to VNets that were created using the Resource Manager deployment model for an ExpressRoute configuration. 有关详细信息,请参阅关于 ExpressRoute 的虚拟网络网关For more information, see About virtual network gateways for ExpressRoute.

开始之前Before beginning

使用 PowerShellWorking with PowerShell

本文中的步骤和示例使用 Azure PowerShell Az 模块。The steps and examples in this article use Azure PowerShell Az modules. 若要在计算机上本地安装 Az 模块,请参阅安装 Azure PowerShellTo install the Az modules locally on your computer, see Install Azure PowerShell. 若要详细了解新 Az 模块,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module, see Introducing the new Azure PowerShell Az module. PowerShell cmdlet 经常更新。PowerShell cmdlets are updated frequently. 如果未运行最新版本,在说明中指定的值可能无法使用。If you are not running the latest version, the values specified in the instructions may fail. 若要在系统上查找已安装的 PowerShell 版本,请使用 Get-Module -ListAvailable Az cmdlet。To find the installed versions of PowerShell on your system, use the Get-Module -ListAvailable Az cmdlet.

配置参考列表Configuration reference list

此任务的步骤使用的 VNet 基于以下配置参考列表中的值。The steps for this task use a VNet based on the values in the following configuration reference list. 此列表中也概述了其他设置和名称。Additional settings and names are also outlined in this list. 尽管我们确实基于此列表中的值添加变量,但是我们在任何步骤中不会直接使用此列表。We don't use this list directly in any of the steps, although we do add variables based on the values in this list. 可以复制列表作为参考,并将列表中的值替换为自己的值。You can copy the list to use as a reference, replacing the values with your own.

配置参考列表Configuration reference list

  • 虚拟网络名称 = “TestVNet”Virtual Network Name = "TestVNet"
  • 虚拟网络地址空间 = 192.168.0.0/16Virtual Network address space = 192.168.0.0/16
  • 资源组 = “TestRG”Resource Group = "TestRG"
  • Subnet1 名称 = “FrontEnd”Subnet1 Name = "FrontEnd"
  • Subnet1 地址空间 =“192.168.1.0/24”Subnet1 address space = "192.168.1.0/24"
  • 网关子网名称:“GatewaySubnet”必须始终将网关子网命名为“GatewaySubnet”。Gateway Subnet name: "GatewaySubnet" You must always name a gateway subnet GatewaySubnet.
  • 网关子网地址空间 = “192.168.200.0/26”Gateway Subnet address space = "192.168.200.0/26"
  • 区域 =“China East”Region = "China East"
  • 网关名称 = “GW”Gateway Name = "GW"
  • 网关 IP 名称 = “GWIP”Gateway IP Name = "GWIP"
  • 网关 IP 配置名称 = “gwipconf”Gateway IP configuration Name = "gwipconf"
  • 类型 =“ExpressRoute” ExpressRoute 配置需要此类型。Type = "ExpressRoute" This type is required for an ExpressRoute configuration.
  • 网关公共 IP 名称 = “gwpip”Gateway Public IP Name = "gwpip"

添加网关Add a gateway

  1. 连接到 Azure 订阅。Connect to your Azure Subscription.

    Connect-AzureRmAccount -Environment AzureChinaCloud
    Get-AzureRmSubscription 
    Select-AzureRmSubscription -SubscriptionName "Name of subscription"
    
  2. 声明此练习的变量。Declare your variables for this exercise. 请务必编辑此示例,使之反映想要使用的设置。Be sure to edit the sample to reflect the settings that you want to use.

     $RG = "TestRG"
     $Location = "China East"
     $GWName = "GW"
     $GWIPName = "GWIP"
     $GWIPconfName = "gwipconf"
     $VNetName = "TestVNet"
    
  3. 将虚拟网络对象存储为变量。Store the virtual network object as a variable.

    $vnet = Get-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $RG
    
  4. 将网关子网添加到虚拟网络中。Add a gateway subnet to your Virtual Network. 网关子网必须命名为“GatewaySubnet”。The gateway subnet must be named "GatewaySubnet". 应创建 /27 或更大(/26、/25 等)的网关子网。You should create a gateway subnet that is /27 or larger (/26, /25, etc.).

    Add-AzureRmVirtualNetworkSubnetConfig -Name GatewaySubnet -VirtualNetwork $vnet -AddressPrefix 192.168.200.0/26
    
  5. 设置配置。Set the configuration.

    $vnet = Set-AzureRmVirtualNetwork -VirtualNetwork $vnet
    
  6. 将网关子网存储为变量。Store the gateway subnet as a variable.

    $subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name 'GatewaySubnet' -VirtualNetwork $vnet
    
  7. 请求公共 IP 地址。Request a public IP address. 创建网关之前请求 IP 地址。The IP address is requested before creating the gateway. 无法指定要使用的 IP 地址;它会进行动态分配。You cannot specify the IP address that you want to use; it’s dynamically allocated. 后面的配置部分会用到此 IP 地址。You'll use this IP address in the next configuration section. AllocationMethod 必须是动态的。The AllocationMethod must be Dynamic.

    $pip = New-AzureRmPublicIpAddress -Name $GWIPName  -ResourceGroupName $RG -Location $Location -AllocationMethod Dynamic
    
  8. 创建网关配置。Create the configuration for your gateway. 网关配置定义要使用的子网和公共 IP 地址。The gateway configuration defines the subnet and the public IP address to use. 在此步骤中,将指定创建网关时使用的配置。In this step, you are specifying the configuration that will be used when you create the gateway. 此步骤不会实际创建网关对象。This step does not actually create the gateway object. 使用下面的示例创建网关配置。Use the sample below to create your gateway configuration.

    $ipconf = New-AzureRmVirtualNetworkGatewayIpConfig -Name $GWIPconfName -Subnet $subnet -PublicIpAddress $pip
    
  9. 创建网关。Create the gateway. 在此步骤中, -GatewayType 尤其重要。In this step, the -GatewayType is especially important. 必须使用值 ExpressRouteYou must use the value ExpressRoute. 运行这些 cmdlet 后,可能需要 45 分钟或更长时间才能创建好网关。After running these cmdlets, the gateway can take 45 minutes or more to create.

    New-AzureRmVirtualNetworkGateway -Name $GWName -ResourceGroupName $RG -Location $Location -IpConfigurations $ipconf -GatewayType Expressroute -GatewaySku Standard
    

验证是否已创建网关Verify the gateway was created

使用以下命令验证是否已创建网关:Use the following commands to verify that the gateway has been created:

Get-AzVirtualNetworkGateway -ResourceGroupName $RG

重设网关大小Resize a gateway

有许多网关 SKUThere are a number of Gateway SKUs. 可以使用以下命令随时更改网关 SKU。You can use the following command to change the Gateway SKU at any time.

Important

此命令对 UltraPerformance 网关不起作用。This command doesn't work for UltraPerformance gateway. 要将网关更改为 UltraPerformance 网关,首先要删除现有的 ExpressRoute 网关,然后创建新的 UltraPerformance 网关。To change your gateway to an UltraPerformance gateway, first remove the existing ExpressRoute gateway, and then create a new UltraPerformance gateway. 要将网关从 UltraPerformance 网关降级,首先要删除 UltraPerformance 网关,然后创建新网关。To downgrade your gateway from an UltraPerformance gateway, first remove the UltraPerformance gateway, and then create a new gateway.

$gw = Get-AzVirtualNetworkGateway -Name $GWName -ResourceGroupName $RG
Resize-AzVirtualNetworkGateway -VirtualNetworkGateway $gw -GatewaySku HighPerformance

删除网关Remove a gateway

使用以下命令删除网关:Use the following command to remove a gateway:

Remove-AzVirtualNetworkGateway -Name $GWName -ResourceGroupName $RG

后续步骤Next steps

创建 VPN 网关之后,可以将 VNet 链接到 ExpressRoute 线路。After you have created the VPN Gateway, you can link your VNet to an ExpressRoute circuit. 请参阅将虚拟网络链接到 ExpressRoute 线路See Link a Virtual Network to an ExpressRoute circuit.