将两个虚拟网络脚本示例对等互连Peer two virtual networks script sample

此脚本示例通过 Azure 网络在同一区域创建并连接两个虚拟网络。This script sample creates and connects two virtual networks in the same region through the Azure network. 运行脚本后,会在两个虚拟网络之间创建对等互连。After running the script, you will create a peering between two virtual networks.

可以通过本地 PowerShell 安装来执行脚本。You can execute the script from a local PowerShell installation. 如果在本地使用 PowerShell,则此脚本需要 Az PowerShell 模块 5.4.1 或更高版本。If you use PowerShell locally, this script requires the Az PowerShell module version 5.4.1 or later. 要查找已安装的版本,请运行 Get-Module -ListAvailable AzTo find the installed version, run Get-Module -ListAvailable Az. 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。If you need to upgrade, see Install Azure PowerShell module. 如果在本地运行 PowerShell,则还需运行 Connect-AzAccount -Environment AzureChinaCloud 来创建与 Azure 的连接。If you are running PowerShell locally, you also need to run Connect-AzAccount -Environment AzureChinaCloud to create a connection with Azure.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

示例脚本Sample script

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

# Variables for common values
$rgName='MyResourceGroup'
$location='chinaeast'

# Create a resource group.
New-AzResourceGroup -Name $rgName -Location $location

# Create virtual network 1.
$vnet1 = New-AzVirtualNetwork -ResourceGroupName $rgName -Name 'Vnet1' -AddressPrefix '10.0.0.0/16' -Location $location

# Create virtual network 2.
$vnet2 = New-AzVirtualNetwork -ResourceGroupName $rgName -Name 'Vnet2' -AddressPrefix '10.1.0.0/16' -Location $location

# Peer VNet1 to VNet2.
Add-AzVirtualNetworkPeering -Name 'LinkVnet1ToVnet2' -VirtualNetwork $vnet1 -RemoteVirtualNetworkId $vnet2.Id

# Peer VNet2 to VNet1.
Add-AzVirtualNetworkPeering -Name 'LinkVnet2ToVnet1' -VirtualNetwork $vnet2 -RemoteVirtualNetworkId $vnet1.Id

清理部署Clean up deployment

运行以下命令来删除资源组、VM 和所有相关资源:Run the following command to remove the resource group, VM, and all related resources:

Remove-AzResourceGroup -Name myResourceGroup -Force

脚本说明Script explanation

此脚本使用以下命令创建资源组、虚拟机和所有相关资源。This script uses the following commands to create a resource group, virtual machine, and all related resources. 下表中的每条命令均链接到特定于命令的文档:Each command in the following table links to command specific documentation:

命令Command 注释Notes
New-AzResourceGroupNew-AzResourceGroup 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
New-AzVirtualNetworkNew-AzVirtualNetwork 创建 Azure 虚拟网络和子网。Creates an Azure virtual network and subnet.
Add-AzVirtualNetworkPeeringAdd-AzVirtualNetworkPeering 创建两个虚拟网络之间的对等互连。Creates a peering between two virtual networks.
Remove-AzResourceGroupRemove-AzResourceGroup 删除资源组,包括所有嵌套的资源。Deletes a resource group including all nested resources.

后续步骤Next steps

有关 Azure PowerShell 的详细信息,请参阅 Azure PowerShell 文档For more information on the Azure PowerShell, see Azure PowerShell documentation.

可在虚拟网络 PowerShell 示例中查找其他虚拟网络 PowerShell 脚本示例。Additional virtual network PowerShell script samples can be found in Virtual network PowerShell samples.