# 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,請為僅包含 public-ip-nat 的 PowerShell 命令建立數位物件。 當您套用命令時,陣列會重新套用至 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,請為僅包含 public-ip-prefix-nat 的 PowerShell 命令建立數位物件。 當您套用命令時,陣列會重新套用至 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