如何在Azure负载平衡器入站NAT规则上替换目标VM和NIC

时间:2019-07-03 19:23:43

标签: azure powershell azure-virtual-network azure-load-balancer

我有一个具有入站NAT规则的现有Azure前端负载均衡器。我想将这些NAT规则的目标/目标大量更改为新的目标服务器。

我目前构建了一个脚本,该脚本从LB获取所有NAT规则,然后对其进行迭代,然后尝试将它们添加到新服务器的NIC。我尝试过和不从旧服务器的NIC删除NAT规则。无论哪种方法,该方法都将返回false且不应用任何更改。

#Set Variables
#subscription ID
$subscription = "value"
#the name of the old NIC that has the LB rules
$OldNicName = "old_nic"
#the name of the NIC to be attached to the LB rules
$NewNicName = "new_nic"
#name of the loadbalancer
$lbname = "my_lb"

#Set Active Subscription
Set-AzContext -SubscriptionId $subscription

#Get the loadbalancer
$lb = Get-AzLoadBalancer -Name $lbname

#Get the old firewall interface/NIC
$OldNic = Get-AzNetworkInterface -Name $OldNicName

#Get the target firewall interface/NIC
$NewNic = Get-AzNetworkInterface -Name $NewNicName

#Attach NAT rules to the NIC
$lb.InboundNatRules | ForEach-Object -Process {$OldNic.IpConfigurations[0].LoadBalancerInboundNatRules.Remove($_); $NewNic.IpConfigurations[0].LoadBalancerInboundNatRules.Add($_)}

#Apply the configuration and reload the NIC
$OldNic | Set-AzNetworkInterface
$NewNic | Set-AzNetworkInterface

我希望每个入站NAT规则现在都与新的NIC / VM关联,但是目前,上面的Remove()和Add()函数都返回FALSE。

1 个答案:

答案 0 :(得分:0)

对于您的问题,您想要将这些NAT规则的目标/目标大量更改为新的目标服务器。与VM网络接口相关联并在接口IP配置中设置的NAT规则。因此,您需要使用两个PowerShell命令Set-AzNetworkInterfaceIpConfigSet-AzNetworkInterface来实现您的目的。脚本如下:

# Set Variables
# subscription ID
$subscription = "value"
# the name of the old NIC that has the LB rules
$OldNicName = "old_nic"
# the name of the NIC to be attached to the LB rules
$NewNicName = "new_nic"
# name of the loadbalancer
$lbname = "my_lb"
# assume all the resources in the same group
$groupname = "group_name"

Set-AzContext -SubscriptionId $subscription

# remove the NAT rules from the old NIC
$oldNic = Get-AzNetworkInterface -ResourceGroupName $groupname -Name $OldNicName
$list = @()       # this is a empty array
Set-AzNetworkInterfaceIpConfig -Name ipconfig1 -NetworkInterface $oldNic -LoadBalancerInboundNatRule $list 
$oldNic | Set-AzNetworkInterface

# associate the NAT rules to the new NIC

$newNic = Get-AzNetworkInterface -ResourceGroupName $groupname -Name $NewNicName
$lb = Get-AzLoadBalancer -ResourceGroupName $groupname -Name $lbname
$NatRules = Get-AzLoadBalancerInboundNatRuleConfig -LoadBalancer $lb
Set-AzNetworkInterfaceIpConfig -Name ipconfig1 -NetworkInterface $newNic -LoadBalancerInboundNatRule $NatRules
$newNic | Set-AzNetworkInterface
相关问题