# Specify the resource group and NAT gateway name
$resourceGroupName = "test-rg"
# Specify the virtual network name and subnet name
$virtualNetworkName = "vnet-1"
$subnetName = "subnet-1"
# Get the virtual network
$vnet = @{
Name = $virtualNetworkName
ResourceGroupName = $resourceGroupName
}
$virtualNetwork = Get-AzVirtualNetwork @vnet
# Get the subnet
$subnet = $virtualNetwork.Subnets | Where-Object {$_.Name -eq $subnetName}
# Remove the NAT gateway association from the subnet
$subnet.NatGateway = $null
# Update the subnet configuration
$subConfig = @{
Name = $subnetName
VirtualNetwork = $virtualNetwork
AddressPrefix = $subnet.AddressPrefix
}
Set-AzVirtualNetworkSubnetConfig @subConfig
# Update the virtual network
Set-AzVirtualNetwork -VirtualNetwork $virtualNetwork
## Place NAT gateway into a variable. ##
$ng = @{
Name = 'nat-gateway'
ResourceGroupName = 'test-rg'
}
$nat = Get-AzNatGateway @ng
## Place the existing public IP address associated with the NAT gateway into a variable. ##
$ip = @{
Name = 'public-ip-nat'
ResourceGroupName = 'test-rg'
}
$publicIP1 = Get-AzPublicIPaddress @ip
## Place the public IP address you created previously into a variable. ##
$ip = @{
Name = 'public-ip-nat2'
ResourceGroupName = 'test-rg'
}
$publicIP2 = Get-AzPublicIPaddress @ip
## Place the public IP address variables into an array. ##
$pipArray = $publicIP1,$publicIP2
## Add the IP address to the NAT gateway. ##
$nt = @{
NatGateway = $nat
PublicIpAddress = $pipArray
}
Set-AzNatGateway @nt
删除公共 IP 地址
若要从 NAT 网关中删除公共 IP,必须创建一个数组对象,其中不包含你要删除的 IP 地址。 例如,假设你为 NAT 网关配置了两个公共 IP 地址。 你想要删除其中一个 IP 地址。 与 NAT 网关关联的 IP 地址分别命名为 public-ip-nat 和 public-ip-nat2。 若要删除 public-ip-nat2,需要为 PowerShell 命令创建一个仅包含 public-ip-nat 的数组对象。 应用该命令时,该数组将重新应用于 NAT 网关,这样,public-ip-nat 就成了唯一一个关联的公共 IP 地址。
## Place NAT gateway into a variable. ##
$ng = @{
Name = 'nat-gateway'
ResourceGroupName = 'test-rg'
}
$nat = Get-AzNatGateway @ng
## Place the existing public IP address associated with the NAT gateway into a variable. ##
$ip = @{
Name = 'public-ip-nat'
ResourceGroupName = 'test-rg'
}
$publicIP1 = Get-AzPublicIPaddress @ip
## Place the second public IP address into a variable. ##
$ip = @{
Name = 'public-ip-nat2'
ResourceGroupName = 'test-rg'
}
$publicIP2 = Get-AzPublicIPAddress @ip
## Place ONLY the public IP you wish to keep in the array. ##
$pipArray = $publicIP1
## Add the public IP address to the NAT gateway. ##
$nt = @{
NatGateway = $nat
PublicIpAddress = $pipArray
}
Set-AzNatGateway @nt
使用 az network nat gateway update 从 NAT 网关中删除公共 IP 地址。 Azure CLI 命令会替换这些值。 它不会删除值。 要删除公共 IP 地址,请在命令中添加要保留的所有 IP 地址。 省略要删除的值。 例如,假设你为 NAT 网关配置了两个公共 IP 地址。 你想要删除其中一个 IP 地址。 与 NAT 网关关联的 IP 地址分别命名为 public-ip-nat 和 public-ip-nat2。 若要删除 public-ip-nat2,必须在命令中省略该 IP 地址的名称。 该命令会将其中列出的 IP 地址重新应用于 NAT 网关。 它会删除所有未列出的 IP 地址。
## Place NAT gateway into a variable. ##
$ng = @{
Name = 'nat-gateway'
ResourceGroupName = 'test-rg'
}
$nat = Get-AzNatGateway @ng
## Place the existing public IP prefix associated with the NAT gateway into a variable. ##
$ip = @{
Name = 'public-ip-prefix-nat'
ResourceGroupName = 'test-rg'
}
$prefixIP1 = Get-AzPublicIPPrefix @ip
## Place the public IP prefix you created previously into a variable. ##
$ip = @{
Name = 'public-ip-prefix-nat2'
ResourceGroupName = 'test-rg'
}
$prefixIP2 = Get-AzPublicIPprefix @ip
## Place the public IP address variables into an array. ##
$preArray = $prefixIP1,$prefixIP2
## Add the IP address prefix to the NAT gateway. ##
$nt = @{
NatGateway = $nat
PublicIpPrefix = $preArray
}
Set-AzNatGateway @nt
删除公共 IP 前缀
若要从 NAT 网关中删除公共 IP 前缀,必须创建一个数组对象,其中不包含你要删除的 IP 地址前缀。 例如,假设你为 NAT 网关配置了两个公共 IP 前缀。 你想要删除其中一个 IP 前缀。 与 NAT 网关关联的 IP 前缀分别命名为 public-ip-prefix-nat 和 public-ip-prefix-nat2。 若要删除 public-ip-prefix-nat2,需要为 PowerShell 命令创建一个仅包含 public-ip-prefix-nat 的数组对象。 应用该命令时,该数组将重新应用于 NAT 网关,这样,public-ip-prefix-nat 就成了唯一一个关联的前缀。
## Place NAT gateway into a variable. ##
$ng = @{
Name = 'nat-gateway'
ResourceGroupName = 'test-rg'
}
$nat = Get-AzNatGateway @ng
## Place the existing public IP prefix associated with the NAT gateway into a variable. ##
$ip = @{
Name = 'public-ip-prefix-nat'
ResourceGroupName = 'test-rg'
}
$prefixIP1 = Get-AzPublicIPPrefix @ip
## Place the secondary public IP prefix into a variable. ##
$ip = @{
Name = 'public-ip-prefix-nat2'
ResourceGroupName = 'test-rg'
}
$prefixIP2 = Get-AzPublicIPprefix @ip
## Place ONLY the prefix you wish to keep in the array. DO NOT ADD THE SECONDARY VARIABLE ##
$preArray = $prefixIP1
## Add the IP address prefix to the NAT gateway. ##
$nt = @{
NatGateway = $nat
PublicIpPrefix = $preArray
}
Set-AzNatGateway @nt
添加公共 IP 前缀
对于本示例,与 NAT 网关关联的现有公共 IP 前缀名为 public-ip-prefix-nat。