使用 PowerShell 删除虚拟网络网关Delete a virtual network gateway using PowerShell

可以使用多种不同的方法来删除 VPN 网关配置中的虚拟网络网关。There are a couple of different approaches you can take when you want to delete a virtual network gateway for a VPN gateway configuration.

  • 如果要删除所有信息并从头开始配置(例如,在测试环境中),可以删除资源组。If you want to delete everything and start over, as in the case of a test environment, you can delete the resource group. 删除某个资源组时,会删除该组中的所有资源。When you delete a resource group, it deletes all the resources within the group. 仅当不想保留资源组中的任何资源时,才建议使用此方法。This is method is only recommended if you don't want to keep any of the resources in the resource group. 使用这种方法时,无法做到有选择性地删除一部分资源。You can't selectively delete only a few resources using this approach.

  • 如果想要保留资源组中的某些资源,则删除虚拟网络网关的过程会略微复杂一些。If you want to keep some of the resources in your resource group, deleting a virtual network gateway becomes slightly more complicated. 在删除虚拟网络网关之前,必须先删除任何依赖于该网关的资源。Before you can delete the virtual network gateway, you must first delete any resources that are dependent on the gateway. 遵循的步骤取决于创建的连接类型,以及每个连接的依赖资源。The steps you follow depend on the type of connections that you created and the dependent resources for each connection.

开始之前Before beginning

1.下载最新的 Azure Resource Manager PowerShell cmdlet。1. Download the latest Azure Resource Manager PowerShell cmdlets.

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

2.连接到 Azure 帐户。2. Connect to your Azure account.

打开 PowerShell 控制台并连接到帐户。Open your PowerShell console and connect to your account. 使用以下示例帮助建立连接:Use the following example to help you connect:

Connect-AzAccount -Environment AzureChinaCloud

检查该帐户的订阅。Check the subscriptions for the account.

Get-AzSubscription

如果有多个订阅,请指定要使用的订阅。If you have more than one subscription, specify the subscription that you want to use.

Select-AzSubscription -SubscriptionName "Replace_with_your_subscription_name"

删除站点到站点 VPN 网关Delete a Site-to-Site VPN gateway

若要删除 S2S 配置中的虚拟网络网关,必须先删除与该网关相关的每个资源。To delete a virtual network gateway for a S2S configuration, you must first delete each resource that pertains to the virtual network gateway. 由于存在依赖关系,必须按特定的顺序删除资源。Resources must be deleted in a certain order due to dependencies. 在以下示例中,必须指定某些值,而其他值是输出结果。When working with the examples below, some of the values must be specified, while other values are an output result. 为方便演示,我们在示例中使用了以下特定值:We use the following specific values in the examples for demonstration purposes:

VNet 名称:VNet1VNet name: VNet1
资源组名称:RG1Resource Group name: RG1
虚拟网络网关名称:GW1Virtual network gateway name: GW1

以下步骤适用于 Resource Manager 部署模型。The following steps apply to the Resource Manager deployment model.

1.单击要删除的虚拟网络网关。1. Get the virtual network gateway that you want to delete.

$GW=get-Azvirtualnetworkgateway -Name "GW1" -ResourceGroupName "RG1"

2.检查该虚拟网络网关是否已建立任何连接。2. Check to see if the virtual network gateway has any connections.

get-Azvirtualnetworkgatewayconnection -ResourceGroupName "RG1" | where-object {$_.VirtualNetworkGateway1.Id -eq $GW.Id}
$Conns=get-Azvirtualnetworkgatewayconnection -ResourceGroupName "RG1" | where-object {$_.VirtualNetworkGateway1.Id -eq $GW.Id}

3.删除所有连接。3. Delete all connections.

系统可能会提示确认是否要删除每个连接。You may be prompted to confirm the deletion of each of the connections.

$Conns | ForEach-Object {Remove-AzVirtualNetworkGatewayConnection -Name $_.name -ResourceGroupName $_.ResourceGroupName}

4.删除虚拟网络网关。4. Delete the virtual network gateway.

系统可能会提示确认是否要删除该网关。You may be prompted to confirm the deletion of the gateway. 除了 S2S 配置,如果还有此 VNet 的 P2S 配置,则删除虚拟网络网关会自动断开所有 P2S 客户端且不发出警告。If you have a P2S configuration to this VNet in addition to your S2S configuration, deleting the virtual network gateway will automatically disconnect all P2S clients without warning.

Remove-AzVirtualNetworkGateway -Name "GW1" -ResourceGroupName "RG1"

此时,虚拟网络网关已删除。At this point, your virtual network gateway has been deleted. 可以使用后续步骤删除不再使用的任何资源。You can use the next steps to delete any resources that are no longer being used.

5 删除本地网络网关。5 Delete the local network gateways.

获取相应本地网络网关的列表。Get the list of the corresponding local network gateways.

$LNG=Get-AzLocalNetworkGateway -ResourceGroupName "RG1" | where-object {$_.Id -In $Conns.LocalNetworkGateway2.Id}

删除本地网络网关。Delete the local network gateways. 系统可能会提示确认是否要删除每个本地网络网关。You may be prompted to confirm the deletion of each of the local network gateway.

$LNG | ForEach-Object {Remove-AzLocalNetworkGateway -Name $_.Name -ResourceGroupName $_.ResourceGroupName}

6.删除公共 IP 地址资源。6. Delete the Public IP address resources.

获取虚拟网络网关的 IP 配置。Get the IP configurations of the virtual network gateway.

$GWIpConfigs = $Gateway.IpConfigurations

获取此虚拟网络网关使用的公共 IP 地址资源列表。Get the list of Public IP address resources used for this virtual network gateway. 如果虚拟网络网关采用主动-主动配置,则会显示两个公共 IP 地址。If the virtual network gateway was active-active, you will see two Public IP addresses.

$PubIP=Get-AzPublicIpAddress | where-object {$_.Id -In $GWIpConfigs.PublicIpAddress.Id}

删除公共 IP 资源。Delete the Public IP resources.

$PubIP | foreach-object {remove-AzpublicIpAddress -Name $_.Name -ResourceGroupName "RG1"}

7.删除网关子网并设置配置。7. Delete the gateway subnet and set the configuration.

$GWSub = Get-AzVirtualNetwork -ResourceGroupName "RG1" -Name "VNet1" | Remove-AzVirtualNetworkSubnetConfig -Name "GatewaySubnet"
Set-AzVirtualNetwork -VirtualNetwork $GWSub

删除 VNet 到 VNet VPN 网关Delete a VNet-to-VNet VPN gateway

若要删除 V2V 配置中的虚拟网络网关,必须先删除与该网关相关的每个资源。To delete a virtual network gateway for a V2V configuration, you must first delete each resource that pertains to the virtual network gateway. 由于存在依赖关系,必须按特定的顺序删除资源。Resources must be deleted in a certain order due to dependencies. 在以下示例中,必须指定某些值,而其他值是输出结果。When working with the examples below, some of the values must be specified, while other values are an output result. 为方便演示,我们在示例中使用了以下特定值:We use the following specific values in the examples for demonstration purposes:

VNet 名称:VNet1VNet name: VNet1
资源组名称:RG1Resource Group name: RG1
虚拟网络网关名称:GW1Virtual network gateway name: GW1

以下步骤适用于 Resource Manager 部署模型。The following steps apply to the Resource Manager deployment model.

1.单击要删除的虚拟网络网关。1. Get the virtual network gateway that you want to delete.

$GW=get-Azvirtualnetworkgateway -Name "GW1" -ResourceGroupName "RG1"

2.检查该虚拟网络网关是否已建立任何连接。2. Check to see if the virtual network gateway has any connections.

get-Azvirtualnetworkgatewayconnection -ResourceGroupName "RG1" | where-object {$_.VirtualNetworkGateway1.Id -eq $GW.Id}

与虚拟网络网关建立的其他连接可能属于不同的资源组。There may be other connections to the virtual network gateway that are part of a different resource group. 检查其他每个资源组中的其他连接。Check for additional connections in each additional resource group. 在此示例中,我们检查来自 RG2 的连接。In this example, we are checking for connections from RG2. 请针对可能与虚拟网络网关建立了连接的每个资源组运行此步骤。Run this for each resource group that you have which may have a connection to the virtual network gateway.

get-Azvirtualnetworkgatewayconnection -ResourceGroupName "RG2" | where-object {$_.VirtualNetworkGateway2.Id -eq $GW.Id}

3.获取两个方向的连接列表。3. Get the list of connections in both directions.

由于这是一种 VNet 到 VNet 配置,因此需要获取两个方向的连接列表。Because this is a VNet-to-VNet configuration, you need the list of connections in both directions.

$ConnsL=get-Azvirtualnetworkgatewayconnection -ResourceGroupName "RG1" | where-object {$_.VirtualNetworkGateway1.Id -eq $GW.Id}

在此示例中,我们检查来自 RG2 的连接。In this example, we are checking for connections from RG2. 请针对可能与虚拟网络网关建立了连接的每个资源组运行此步骤。Run this for each resource group that you have which may have a connection to the virtual network gateway.

 $ConnsR=get-Azvirtualnetworkgatewayconnection -ResourceGroupName "<NameOfResourceGroup2>" | where-object {$_.VirtualNetworkGateway2.Id -eq $GW.Id}

4.删除所有连接。4. Delete all connections.

系统可能会提示确认是否要删除每个连接。You may be prompted to confirm the deletion of each of the connections.

$ConnsL | ForEach-Object {Remove-AzVirtualNetworkGatewayConnection -Name $_.name -ResourceGroupName $_.ResourceGroupName}
$ConnsR | ForEach-Object {Remove-AzVirtualNetworkGatewayConnection -Name $_.name -ResourceGroupName $_.ResourceGroupName}

5.删除虚拟网络网关。5. Delete the virtual network gateway.

系统可能会提示确认是否要删除该虚拟网络网关。You may be prompted to confirm the deletion of the virtual network gateway. 除了 V2V 配置,如果还有此 VNet 的 P2S 配置,则删除虚拟网络网关会自动断开所有 P2S 客户端且不发出警告。If you have P2S configurations to your VNets in addition to your V2V configuration, deleting the virtual network gateways will automatically disconnect all P2S clients without warning.

Remove-AzVirtualNetworkGateway -Name "GW1" -ResourceGroupName "RG1"

此时,虚拟网络网关已删除。At this point, your virtual network gateway has been deleted. 可以使用后续步骤删除不再使用的任何资源。You can use the next steps to delete any resources that are no longer being used.

6.删除公共 IP 地址资源6. Delete the Public IP address resources

获取虚拟网络网关的 IP 配置。Get the IP configurations of the virtual network gateway.

$GWIpConfigs = $Gateway.IpConfigurations

获取此虚拟网络网关使用的公共 IP 地址资源列表。Get the list of Public IP address resources used for this virtual network gateway. 如果虚拟网络网关采用主动-主动配置,则会显示两个公共 IP 地址。If the virtual network gateway was active-active, you will see two Public IP addresses.

$PubIP=Get-AzPublicIpAddress | where-object {$_.Id -In $GWIpConfigs.PublicIpAddress.Id}

删除公共 IP 资源。Delete the Public IP resources. 系统可能会提示确认是否要删除该公开 IP。You may be prompted to confirm the deletion of the Public IP.

$PubIP | foreach-object {remove-AzpublicIpAddress -Name $_.Name -ResourceGroupName "<NameOfResourceGroup1>"}

7.删除网关子网并设置配置。7. Delete the gateway subnet and set the configuration.

$GWSub = Get-AzVirtualNetwork -ResourceGroupName "RG1" -Name "VNet1" | Remove-AzVirtualNetworkSubnetConfig -Name "GatewaySubnet"
Set-AzVirtualNetwork -VirtualNetwork $GWSub

删除点到站点 VPN 网关Delete a Point-to-Site VPN gateway

若要删除 P2S 配置中的虚拟网络网关,必须先删除与该网关相关的每个资源。To delete a virtual network gateway for a P2S configuration, you must first delete each resource that pertains to the virtual network gateway. 由于存在依赖关系,必须按特定的顺序删除资源。Resources must be deleted in a certain order due to dependencies. 在以下示例中,必须指定某些值,而其他值是输出结果。When working with the examples below, some of the values must be specified, while other values are an output result. 为方便演示,我们在示例中使用了以下特定值:We use the following specific values in the examples for demonstration purposes:

VNet 名称:VNet1VNet name: VNet1
资源组名称:RG1Resource Group name: RG1
虚拟网络网关名称:GW1Virtual network gateway name: GW1

以下步骤适用于 Resource Manager 部署模型。The following steps apply to the Resource Manager deployment model.

备注

删除 VPN 网关时,所有连接的客户端将与 VNet 断开连接且不发出警告。When you delete the VPN gateway, all connected clients will be disconnected from the VNet without warning.

1.单击要删除的虚拟网络网关。1. Get the virtual network gateway that you want to delete.

$GW=get-Azvirtualnetworkgateway -Name "GW1" -ResourceGroupName "RG1"

2.删除虚拟网络网关。2. Delete the virtual network gateway.

系统可能会提示确认是否要删除该虚拟网络网关。You may be prompted to confirm the deletion of the virtual network gateway.

Remove-AzVirtualNetworkGateway -Name "GW1" -ResourceGroupName "RG1"

此时,虚拟网络网关已删除。At this point, your virtual network gateway has been deleted. 可以使用后续步骤删除不再使用的任何资源。You can use the next steps to delete any resources that are no longer being used.

3.删除公共 IP 地址资源3. Delete the Public IP address resources

获取虚拟网络网关的 IP 配置。Get the IP configurations of the virtual network gateway.

$GWIpConfigs = $Gateway.IpConfigurations

获取此虚拟网络网关使用的公共 IP 地址列表。Get the list of Public IP addresses used for this virtual network gateway. 如果虚拟网络网关采用主动-主动配置,则会显示两个公共 IP 地址。If the virtual network gateway was active-active, you will see two Public IP addresses.

$PubIP=Get-AzPublicIpAddress | where-object {$_.Id -In $GWIpConfigs.PublicIpAddress.Id}

删除公共 IP。Delete the Public IPs. 系统可能会提示确认是否要删除该公开 IP。You may be prompted to confirm the deletion of the Public IP.

$PubIP | foreach-object {remove-AzpublicIpAddress -Name $_.Name -ResourceGroupName "<NameOfResourceGroup1>"}

4.删除网关子网并设置配置。4. Delete the gateway subnet and set the configuration.

$GWSub = Get-AzVirtualNetwork -ResourceGroupName "RG1" -Name "VNet1" | Remove-AzVirtualNetworkSubnetConfig -Name "GatewaySubnet"
Set-AzVirtualNetwork -VirtualNetwork $GWSub

通过删除资源组来删除 VPN 网关Delete a VPN gateway by deleting the resource group

如果不关心是否要保留资源组中的任何资源,而只是要从头开始配置,则可以删除整个资源组。If you are not concerned about keeping any of your resources in the resource group and you just want to start over, you can delete an entire resource group. 这种方法可以快速删除所有信息。This is a quick way to remove everything. 以下步骤仅适用于 Resource Manager 部署模型。The following steps apply only to the Resource Manager deployment model.

1.获取订阅中所有资源组的列表。1. Get a list of all the resource groups in your subscription.

Get-AzResourceGroup

2.找到想要删除的资源组。2. Locate the resource group that you want to delete.

找到想要删除的资源组,并查看该资源组中的资源列表。Locate the resource group that you want to delete and view the list of resources in that resource group. 在本示例中,资源组的名称为 RG1。In the example, the name of the resource group is RG1. 请修改示例以检索所有资源的列表。Modify the example to retrieve a list of all the resources.

Find-AzResource -ResourceGroupNameContains RG1

3.检查列表中的资源。3. Verify the resources in the list.

返回列表后,请检查该列表,确认要删除该资源组中的所有资源以及资源组本身。When the list is returned, review it to verify that you want to delete all the resources in the resource group, as well as the resource group itself. 如果要保留资源组中的某些资源,请使用本文之前部分中的步骤删除网关。If you want to keep some of the resources in the resource group, use the steps in the earlier sections of this article to delete your gateway.

4.删除资源组和资源。4. Delete the resource group and resources.

如果要删除资源组及其包含的所有资源,请修改本示例,并运行。To delete the resource group and all the resource contained in the resource group, modify the example and run.

Remove-AzResourceGroup -Name RG1

5.检查状态。5. Check the status.

Azure 需要花费一段时间来删除所有资源。It takes some time for Azure to delete all the resources. 可以使用以下 cmdlet 检查资源组的状态。You can check the status of your resource group by using this cmdlet.

Get-AzResourceGroup -ResourceGroupName RG1

返回的结果显示“Succeeded”。The result that is returned shows 'Succeeded'.

ResourceGroupName : RG1
Location          : chinanorth
ProvisioningState : Succeeded