使用 PowerShell 修改本地网络网关设置Modify local network gateway settings using PowerShell

有时本地网络网关 AddressPrefix 或 GatewayIPAddress 的设置会变更。Sometimes the settings for your local network gateway AddressPrefix or GatewayIPAddress change. 本文演示如何修改本地网络网关设置。This article shows you how to modify your local network gateway settings. 还可以通过选择以下列表中的其他选项,使用另一种方法来修改这些设置:You can also modify these settings using a different method by selecting a different option from the following list:

准备工作Before you begin

安装最新版本的 Azure Resource Manager PowerShell cmdlet。Install the latest version of the Azure Resource Manager PowerShell cmdlets. 有关安装 PowerShell cmdlet 的详细信息,请参阅 如何安装和配置 Azure PowerShellSee How to install and configure Azure PowerShell for more information about installing the PowerShell cmdlets.

修改 IP 地址前缀Modify IP address prefixes

修改本地网关 IP 地址前缀 - 无网关连接To modify local network gateway IP address prefixes - no gateway connection

添加其他地址前缀:To add additional address prefixes:

  1. 设置 LocalNetworkGateway 的变量。Set the variable for the LocalNetworkGateway.

    $local = Get-AzLocalNetworkGateway -Name Site1 -ResourceGroupName TestRG1
    
  2. 修改前缀。Modify the prefixes.

    Set-AzLocalNetworkGateway -LocalNetworkGateway $local `
    -AddressPrefix @('10.101.0.0/24','10.101.1.0/24','10.101.2.0/24')
    

删除地址前缀:To remove address prefixes:

省去你不再需要的前缀。Leave out the prefixes that you no longer need. 在此示例中,我们不再需要前缀 10.101.2.0/24(来自前面的示例),因此需更新本地网关,排除该前缀。In this example, we no longer need prefix 10.101.2.0/24 (from the previous example), so we update the local network gateway, excluding that prefix.

  1. 设置 LocalNetworkGateway 的变量。Set the variable for the LocalNetworkGateway.

    $local = Get-AzLocalNetworkGateway -Name Site1 -ResourceGroupName TestRG1
    
  2. 使用更新的前缀设置网关。Set the gateway with the updated prefixes.

    Set-AzLocalNetworkGateway -LocalNetworkGateway $local `
    -AddressPrefix @('10.101.0.0/24','10.101.1.0/24')
    

修改本地网关 IP 地址前缀 - 现有网关连接To modify local network gateway IP address prefixes - existing gateway connection

如果有一个网关连接并且想要添加或删除包含在本地网关中的 IP 地址前缀,则需要按顺序执行以下步骤。If you have a gateway connection and want to add or remove the IP address prefixes contained in your local network gateway, you need to do the following steps, in order. 这会导致 VPN 连接中断一段时间。This results in some downtime for your VPN connection. 修改 IP 地址前缀时,不需删除 VPN 网关。When modifying IP address prefixes, you don't need to delete the VPN gateway. 只需删除连接。You only need to remove the connection.

  1. 删除连接。Remove the connection.

    Remove-AzVirtualNetworkGatewayConnection -Name VNet1toSite1 -ResourceGroupName TestRG1
    
  2. 使用修改的地址前缀设置本地网络网关。Set the local network gateway with the modified address prefixes.

    设置 LocalNetworkGateway 的变量。Set the variable for the LocalNetworkGateway.

    $local = Get-AzLocalNetworkGateway -Name Site1 -ResourceGroupName TestRG1
    

    修改前缀。Modify the prefixes.

    Set-AzLocalNetworkGateway -LocalNetworkGateway $local `
    -AddressPrefix @('10.101.0.0/24','10.101.1.0/24')
    
  3. 创建连接。Create the connection. 在此示例中,我们配置 IPsec 连接类型。In this example, we configure an IPsec connection type. 重新创建连接时,请使用针对配置指定的连接类型。When you recreate your connection, use the connection type that is specified for your configuration. 有关其他连接类型,请参阅 PowerShell cmdlet 页面。For additional connection types, see the PowerShell cmdlet page.

    设置 VirtualNetworkGateway 的变量。Set the variable for the VirtualNetworkGateway.

    $gateway1 = Get-AzVirtualNetworkGateway -Name VNet1GW  -ResourceGroupName TestRG1
    

    创建连接。Create the connection. 此示例使用在步骤 2 中设置的变量 $local。This example uses the variable $local that you set in step 2.

    New-AzVirtualNetworkGatewayConnection -Name VNet1toSite1 `
     -ResourceGroupName TestRG1 -Location 'China North' `
    -VirtualNetworkGateway1 $gateway1 -LocalNetworkGateway2 $local `
    -ConnectionType IPsec `
    -RoutingWeight 10 -SharedKey 'abc123'
    

修改网关 IP 地址Modify the gateway IP address

修改本地网关的“GatewayIpAddress”- 无网关连接To modify the local network gateway 'GatewayIpAddress' - no gateway connection

如果要连接的 VPN 设备已更改其公共 IP 地址,则需根据该更改修改本地网关。If the VPN device that you want to connect to has changed its public IP address, you need to modify the local network gateway to reflect that change. 请使用此示例修改没有网关连接的本地网关。Use the example to modify a local network gateway that does not have a gateway connection.

修改此值时,还可同时修改地址前缀。When modifying this value, you can also modify the address prefixes at the same time. 请务必使用本地网关的现有名称来覆盖当前设置。Be sure to use the existing name of your local network gateway in order to overwrite the current settings. 如果使用其他名称,请创建一个新的本地网关,而不是覆盖现有本地网关。If you use a different name, you create a new local network gateway, instead of overwriting the existing one.

New-AzLocalNetworkGateway -Name Site1 `
-Location "China North" -AddressPrefix @('10.101.0.0/24','10.101.1.0/24') `
-GatewayIpAddress "5.4.3.2" -ResourceGroupName TestRG1

修改本地网关的“GatewayIpAddress”- 现有网关连接To modify the local network gateway 'GatewayIpAddress' - existing gateway connection

如果要连接的 VPN 设备已更改其公共 IP 地址,则需根据该更改修改本地网关。If the VPN device that you want to connect to has changed its public IP address, you need to modify the local network gateway to reflect that change. 如果网关连接已存在,首先需要删除该连接。If a gateway connection already exists, you first need to remove the connection. 删除连接后,可修改网关 IP 地址并重新创建一个新的连接。After the connection is removed, you can modify the gateway IP address and recreate a new connection. 此外可同时修改地址前缀。You can also modify the address prefixes at the same time. 这会导致 VPN 连接中断一段时间。This results in some downtime for your VPN connection. 修改网关 IP 地址时,不需删除 VPN 网关。When modifying the gateway IP address, you don't need to delete the VPN gateway. 只需删除连接。You only need to remove the connection.

  1. 删除连接。Remove the connection. 可以使用“Get-AzVirtualNetworkGatewayConnection”cmdlet 查找连接的名称。You can find the name of your connection by using the 'Get-AzVirtualNetworkGatewayConnection' cmdlet.

    Remove-AzVirtualNetworkGatewayConnection -Name VNet1toSite1 `
    -ResourceGroupName TestRG1
    
  2. 修改“GatewayIpAddress”值。Modify the 'GatewayIpAddress' value. 此外可同时修改地址前缀。You can also modify the address prefixes at the same time. 请务必使用本地网关的现有名称来覆盖当前设置。Be sure to use the existing name of your local network gateway to overwrite the current settings. 如果不这样做,请创建一个新的本地网关,而不是覆盖现有本地网关。If you don't, you create a new local network gateway, instead of overwriting the existing one.

    New-AzLocalNetworkGateway -Name Site1 `
    -Location "China North" -AddressPrefix @('10.101.0.0/24','10.101.1.0/24') `
    -GatewayIpAddress "104.40.81.124" -ResourceGroupName TestRG1
    
  3. 创建连接。Create the connection. 在此示例中,我们配置 IPsec 连接类型。In this example, we configure an IPsec connection type. 重新创建连接时,请使用针对配置指定的连接类型。When you recreate your connection, use the connection type that is specified for your configuration. 有关其他连接类型,请参阅 PowerShell cmdlet 页面。For additional connection types, see the PowerShell cmdlet page. 若要获取 VirtualNetworkGateway 名称,可运行“Get-AzVirtualNetworkGateway”cmdlet。To obtain the VirtualNetworkGateway name, you can run the 'Get-AzVirtualNetworkGateway' cmdlet.

    设置变量。Set the variables.

    $local = Get-AzLocalNetworkGateway -Name Site1 -ResourceGroupName TestRG1
    
    $vnetgw = Get-AzVirtualNetworkGateway -Name VNet1GW -ResourceGroupName TestRG1
    

    创建连接。Create the connection.

    New-AzVirtualNetworkGatewayConnection -Name VNet1Site1 -ResourceGroupName TestRG1 `
    -Location "China North" `
    -VirtualNetworkGateway1 $vnetgw `
    -LocalNetworkGateway2 $local `
    -ConnectionType IPsec -RoutingWeight 10 -SharedKey 'abc123'
    

后续步骤Next steps

可验证网关连接。You can verify your gateway connection. 请参阅验证网关连接See Verify a gateway connection.