0

我有一个脚本,在进行部署之前删除然后添加对 Azure WebApp 的防火墙限制。您将在下面找到脚本

az webapp config access-restriction remove -g $(qa-rg) -n $(qa-app) --rule-name myip --action Allow --ip-address 157.71.103.203/32 --priority 1011
az webapp config access-restriction remove -g $(qa-rg) -n $(qa-app) --rule-name myip --action Allow --ip-address 157.71.173.703/32 --priority 1012

az webapp config access-restriction add -g $(qa-rg) -n $(qa-app) --rule-name myip --action Allow --ip-address 157.71.103.203/32 --priority 1011
az webapp config access-restriction add -g $(qa-rg) -n $(qa-app) --rule-name myip --action Allow --ip-address 157.71.173.703/32 --priority 1012

上述命令的问题是,假设有人手动删除了防火墙或者该用户不存在防火墙,那么在这种情况下脚本会失败并出现错误。

有没有办法首先检查为不同用户启用的所有防火墙,然后遍历并删除它们中的每一个,最后再次为删除的用户添加所有防火墙规则。

有人可以帮我创建这个脚本,因为我刚刚学习脚本

谢谢

4

1 回答 1

0

首先,您使用的是 Azure CLI 命令而不是 Power Shell 命令。

这是使用 power shell删除访问限制规则的命令:

Remove-AzWebAppAccessRestrictionRule -ResourceGroupName "Default-Web-WestUS" -WebAppName "ContosoSite" -Name IpRule

要检查规则是否存在,您可以使用Get-AzWebAppAccessRestrictionConfig.

如果要自动检查和删除,请尝试以下操作:

$results = (Get-AzWebAppAccessRestrictionConfig -ResourceGroupName "ResourceGroup" -Name "yourweb").MainSiteAccessRestrictions
$results

foreach($result in $results)
{
    if($result){
    Remove-AzWebAppAccessRestrictionRule -ResourceGroupName "ResourceGroup" -WebAppName "yourweb" -Name $result.RuleName
    sleep 10
    }
}
于 2021-02-16T09:47:02.447 回答