适用于 Azure 虚拟网络的常见 PowerShell 命令Common PowerShell commands for Azure Virtual Networks

如果想要创建虚拟机,需要创建虚拟网络或了解可在其中添加 VM 的现有虚拟网络。If you want to create a virtual machine, you need to create a virtual network or know about an existing virtual network in which the VM can be added. 通常情况下,创建 VM 时,还需考虑创建本文所述资源。Typically, when you create a VM, you also need to consider creating the resources described in this article.

有关安装最新版 Azure PowerShell、选择订阅和登录到帐户的信息,请参阅如何安装和配置 Azure PowerShellSee How to install and configure Azure PowerShell for information about installing the latest version of Azure PowerShell, selecting your subscription, and signing in to your account.

如果运行本文中的多个命令,以下一些变量可能将对你有用:Some variables might be useful for you if running more than one of the commands in this article:

  • $location - 网络资源的位置。$location - The location of the network resources. 可以使用 Get-AzLocation 查找适合你的地理区域You can use Get-AzLocation to find a geographical region that works for you.
  • $myResourceGroup - 网络资源所在的资源组名称。$myResourceGroup - The name of the resource group where the network resources are located.

创建网络资源Create network resources

任务Task 命令Command
创建子网配置Create subnet configurations $subnet1 = New-AzVirtualNetworkSubnetConfig -Name "mySubnet1" -AddressPrefix XX.X.X.X/XX$subnet1 = New-AzVirtualNetworkSubnetConfig -Name "mySubnet1" -AddressPrefix XX.X.X.X/XX
$subnet2 = New-AzVirtualNetworkSubnetConfig -Name "mySubnet2" -AddressPrefix XX.X.X.X/XX$subnet2 = New-AzVirtualNetworkSubnetConfig -Name "mySubnet2" -AddressPrefix XX.X.X.X/XX

典型的网络可能包含用于面向 Internet 的负载均衡器的子网,以及用于内部负载均衡器的独立子网。A typical network might have a subnet for an internet facing load balancer and a separate subnet for an internal load balancer.
创建虚拟网络Create a virtual network $vnet = New-AzVirtualNetwork -Name "myVNet" -ResourceGroupName $myResourceGroup -Location $location -AddressPrefix XX.X.X.X/XX -Subnet $subnet1, $subnet2$vnet = New-AzVirtualNetwork -Name "myVNet" -ResourceGroupName $myResourceGroup -Location $location -AddressPrefix XX.X.X.X/XX -Subnet $subnet1, $subnet2
测试唯一域名Test for a unique domain name Test-AzDnsAvailability -DomainNameLabel "myDNS" -Location $locationTest-AzDnsAvailability -DomainNameLabel "myDNS" -Location $location

可以为公共 IP 资源指定一个 DNS 域名,以便在 Azure 托管的 DNS 服务器中创建 domainname.location.cloudapp.chinacloudapi.cn 到公共 IP 地址的映射。You can specify a DNS domain name for a public IP resource, which creates a mapping for domainname.location.cloudapp.chinacloudapi.cn to the public IP address in the Azure-managed DNS servers. 字段只能包含字母、数字和连字符。The name can contain only letters, numbers, and hyphens. 第一个和最后一个字符必须是字母或数字,域名在其 Azure 位置内必须是唯一的。The first and last character must be a letter or number and the domain name must be unique within its Azure location. 如果返回 True ,则建议的名称是全局唯一的。If True is returned, your proposed name is globally unique.
创建公共 IP 地址Create a public IP address $pip = New-AzPublicIpAddress -Name "myPublicIp" -ResourceGroupName $myResourceGroup -DomainNameLabel "myDNS" -Location $location -AllocationMethod Dynamic$pip = New-AzPublicIpAddress -Name "myPublicIp" -ResourceGroupName $myResourceGroup -DomainNameLabel "myDNS" -Location $location -AllocationMethod Dynamic

公共 IP 地址使用前面测试过的并由负载均衡器前端配置使用的域名。The public IP address uses the domain name that you previously tested and is used by the frontend configuration of the load balancer.
创建前端 IP 配置Create a frontend IP configuration $frontendIP = New-AzLoadBalancerFrontendIpConfig -Name "myFrontendIP" -PublicIpAddress $pip$frontendIP = New-AzLoadBalancerFrontendIpConfig -Name "myFrontendIP" -PublicIpAddress $pip

前端配置包括前面针对传入网络流量创建的公共 IP 地址。The frontend configuration includes the public IP address that you previously created for incoming network traffic.
创建后端地址池Create a backend address pool $beAddressPool = New-AzLoadBalancerBackendAddressPoolConfig -Name "myBackendAddressPool"$beAddressPool = New-AzLoadBalancerBackendAddressPoolConfig -Name "myBackendAddressPool"

提供可通过网络接口访问的负载均衡器后端内部地址。Provides internal addresses for the backend of the load balancer that are accessed through a network interface.
创建探测Create a probe $healthProbe = New-AzLoadBalancerProbeConfig -Name "myProbe" -RequestPath 'HealthProbe.aspx' -Protocol http -Port 80 -IntervalInSeconds 15 -ProbeCount 2$healthProbe = New-AzLoadBalancerProbeConfig -Name "myProbe" -RequestPath 'HealthProbe.aspx' -Protocol http -Port 80 -IntervalInSeconds 15 -ProbeCount 2

包含用于检查后端地址池中虚拟机实例的可用性的运行状况探测。Contains health probes used to check availability of virtual machines instances in the backend address pool.
创建负载均衡规则Create a load balancing rule $lbRule = New-AzLoadBalancerRuleConfig -Name HTTP -FrontendIpConfiguration $frontendIP -BackendAddressPool $beAddressPool -Probe $healthProbe -Protocol Tcp -FrontendPort 80 -BackendPort 80$lbRule = New-AzLoadBalancerRuleConfig -Name HTTP -FrontendIpConfiguration $frontendIP -BackendAddressPool $beAddressPool -Probe $healthProbe -Protocol Tcp -FrontendPort 80 -BackendPort 80

包含将负载均衡器上公用端口分配给后端地址池中端口的规则。Contains rules that assign a public port on the load balancer to a port in the backend address pool.
创建入站 NAT 规则Create an inbound NAT rule $inboundNATRule = New-AzLoadBalancerInboundNatRuleConfig -Name "myInboundRule1" -FrontendIpConfiguration $frontendIP -Protocol TCP -FrontendPort 3441 -BackendPort 3389$inboundNATRule = New-AzLoadBalancerInboundNatRuleConfig -Name "myInboundRule1" -FrontendIpConfiguration $frontendIP -Protocol TCP -FrontendPort 3441 -BackendPort 3389

包含将负载均衡器上公用端口映射到后端地址池中特定虚拟机端口的规则。Contains rules mapping a public port on the load balancer to a port for a specific virtual machine in the backend address pool.
创建负载均衡器Create a load balancer $loadBalancer = New-AzLoadBalancer -ResourceGroupName $myResourceGroup -Name "myLoadBalancer" -Location $location -FrontendIpConfiguration $frontendIP -InboundNatRule $inboundNATRule -LoadBalancingRule $lbRule -BackendAddressPool $beAddressPool -Probe $healthProbe$loadBalancer = New-AzLoadBalancer -ResourceGroupName $myResourceGroup -Name "myLoadBalancer" -Location $location -FrontendIpConfiguration $frontendIP -InboundNatRule $inboundNATRule -LoadBalancingRule $lbRule -BackendAddressPool $beAddressPool -Probe $healthProbe
创建网络接口Create a network interface $nic1= New-AzNetworkInterface -ResourceGroupName $myResourceGroup -Name "myNIC" -Location $location -PrivateIpAddress XX.X.X.X -Subnet $subnet2 -LoadBalancerBackendAddressPool $loadBalancer.BackendAddressPools[0] -LoadBalancerInboundNatRule $loadBalancer.InboundNatRules[0]$nic1= New-AzNetworkInterface -ResourceGroupName $myResourceGroup -Name "myNIC" -Location $location -PrivateIpAddress XX.X.X.X -Subnet $subnet2 -LoadBalancerBackendAddressPool $loadBalancer.BackendAddressPools[0] -LoadBalancerInboundNatRule $loadBalancer.InboundNatRules[0]

创建一个使用前面所创建公共 IP 地址和虚拟网络子网的网络接口。Create a network interface using the public IP address and virtual network subnet that you previously created.

获取有关网络资源的信息Get information about network resources

任务Task 命令Command
列出虚拟网络List virtual networks Get-AzVirtualNetwork -ResourceGroupName $myResourceGroupGet-AzVirtualNetwork -ResourceGroupName $myResourceGroup

列出资源组中的所有虚拟网络。Lists all the virtual networks in the resource group.
获取有关虚拟网络的信息Get information about a virtual network Get-AzVirtualNetwork -Name "myVNet" -ResourceGroupName $myResourceGroupGet-AzVirtualNetwork -Name "myVNet" -ResourceGroupName $myResourceGroup
列出虚拟网络中的子网List subnets in a virtual network Get-AzVirtualNetwork -Name "myVNet" -ResourceGroupName $myResourceGroup | Select SubnetsGet-AzVirtualNetwork -Name "myVNet" -ResourceGroupName $myResourceGroup | Select Subnets
获取有关子网的信息Get information about a subnet Get-AzVirtualNetworkSubnetConfig -Name "mySubnet1" -VirtualNetwork $vnetGet-AzVirtualNetworkSubnetConfig -Name "mySubnet1" -VirtualNetwork $vnet

获取指定虚拟网络中子网的相关信息。Gets information about the subnet in the specified virtual network. $vnet 值表示 Get-AzVirtualNetwork 返回的对象。The $vnet value represents the object returned by Get-AzVirtualNetwork.
列出 IP 地址List IP addresses Get-AzPublicIpAddress -ResourceGroupName $myResourceGroupGet-AzPublicIpAddress -ResourceGroupName $myResourceGroup

列出资源组中的公共 IP 地址。Lists the public IP addresses in the resource group.
列出负载均衡器List load balancers Get-AzLoadBalancer -ResourceGroupName $myResourceGroupGet-AzLoadBalancer -ResourceGroupName $myResourceGroup

列出资源组中的所有负载均衡器。Lists all the load balancers in the resource group.
列出网络接口List network interfaces Get-AzNetworkInterface -ResourceGroupName $myResourceGroupGet-AzNetworkInterface -ResourceGroupName $myResourceGroup

列出资源组中的所有网络接口。Lists all the network interfaces in the resource group.
获取有关网络接口的信息Get information about a network interface Get-AzNetworkInterface -Name "myNIC" -ResourceGroupName $myResourceGroupGet-AzNetworkInterface -Name "myNIC" -ResourceGroupName $myResourceGroup

获取有关特定网络接口的信息。Gets information about a specific network interface.
获取网络接口的 IP 配置Get the IP configuration of a network interface Get-AzNetworkInterfaceIPConfig -Name "myNICIP" -NetworkInterface $nicGet-AzNetworkInterfaceIPConfig -Name "myNICIP" -NetworkInterface $nic

获取指定网络接口的 IP 配置的相关信息。Gets information about the IP configuration of the specified network interface. $nic 值表示 Get-AzNetworkInterface 返回的对象。The $nic value represents the object returned by Get-AzNetworkInterface.

管理网络资源Manage network resources

任务Task 命令Command
将子网添加到虚拟网络Add a subnet to a virtual network Add-AzVirtualNetworkSubnetConfig -AddressPrefix XX.X.X.X/XX -Name "mySubnet1" -VirtualNetwork $vnetAdd-AzVirtualNetworkSubnetConfig -AddressPrefix XX.X.X.X/XX -Name "mySubnet1" -VirtualNetwork $vnet

将子网添加到现有虚拟网络。Adds a subnet to an existing virtual network. $vnet 值表示 Get-AzVirtualNetwork 返回的对象。The $vnet value represents the object returned by Get-AzVirtualNetwork.
删除虚拟网络Delete a virtual network Remove-AzVirtualNetwork -Name "myVNet" -ResourceGroupName $myResourceGroupRemove-AzVirtualNetwork -Name "myVNet" -ResourceGroupName $myResourceGroup

从资源组中删除指定的虚拟网络。Removes the specified virtual network from the resource group.
删除网络接口Delete a network interface Remove-AzNetworkInterface -Name "myNIC" -ResourceGroupName $myResourceGroupRemove-AzNetworkInterface -Name "myNIC" -ResourceGroupName $myResourceGroup

从资源组中删除指定的网络接口。Removes the specified network interface from the resource group.
删除负载均衡器Delete a load balancer Remove-AzLoadBalancer -Name "myLoadBalancer" -ResourceGroupName $myResourceGroupRemove-AzLoadBalancer -Name "myLoadBalancer" -ResourceGroupName $myResourceGroup

从资源组中删除指定的负载均衡器。Removes the specified load balancer from the resource group.
删除公共 IP 地址Delete a public IP address Remove-AzPublicIpAddress-Name "myIPAddress" -ResourceGroupName $myResourceGroupRemove-AzPublicIpAddress-Name "myIPAddress" -ResourceGroupName $myResourceGroup

从资源组中删除指定的公共 IP 地址。Removes the specified public IP address from the resource group.

后续步骤Next Steps