【问题标题】:Azure Resource Manager IP Security Restrictions using Powershell使用 Powershell 的 Azure 资源管理器 IP 安全限制
【发布时间】:2017-05-19 13:15:50
【问题描述】:

我正在尝试使用 Powershell 设置 IP 安全限制。我的语法没有返回任何错误,但设置没有改变。 “ipSecurityRestrictions”属性是一个哈希表。

$r = Get-AzureRmResource -ResourceGroupName *resource-group-name* -ResourceType Microsoft.Web/sites/config -ResourceName resourcename/web -ApiVersion 2016-08-01
$p = $r.Properties
$p.ipSecurityRestrictions = @{ ipAddress = "0.0.0.0"; subnetMask = "0.0.0.0" }
Set-AzureRmResource -ResourceGroupName *resource-group-name* -ResourceType Microsoft.Web/sites/config -ResourceName resourcename/web -ApiVersion 2016-08-01 -PropertyObject $p

这不是权限问题,也没有返回错误。要更改不是哈希表的属性,例如 phpVersion,以下代码可以正常工作:

$p.phpVersion = "7.0"

有人使用这个方法成功设置了 ipSecurityRestrictions 吗?

【问题讨论】:

    标签: powershell azure azure-resource-manager


    【解决方案1】:

    ipSecurityRestrictions 应该是对象数组。请尝试更改代码如下。它对我来说可以正常工作。

    $r = Get-AzureRmResource -ResourceGroupName "Resoucegroup name" -ResourceType Microsoft.Web/sites/config -ResourceName resourcename/web -ApiVersion 2016-08-01
    
    $p = $r.Properties
    $p.ipSecurityRestrictions = @()
    $restriction = @{}
    $restriction.Add("ipAddress","0.0.0.0")
    $restriction.Add("subnetMask","0.0.0.0")
    $p.ipSecurityRestrictions+= $restriction
    
    Set-AzureRmResource -ResourceGroupName  "Resoucegroup name" -ResourceType Microsoft.Web/sites/config -ResourceName resourcename/web -ApiVersion 2016-08-01 -PropertyObject $p
    

    之后我们可以从资源 azure 中得到结果 (https://resources.azure.com)。

    我们也可以从资源 azure 中获取 powershell cmd。

    【讨论】:

    • 如何对 Web 应用程序中的所有插槽执行此操作? IP 限制取决于插槽..
    【解决方案2】:

    这是一个添加规则的便捷函数:

    function Add-AzureIpRestrictionRule
    {
        [CmdletBinding()]
        Param
        (
            # Name of the resource group that contains the App Service.
            [Parameter(Mandatory=$true, Position=0)]
            $ResourceGroupName, 
    
            # Name of your Web or API App.
            [Parameter(Mandatory=$true, Position=1)]
            $AppServiceName, 
    
            # rule to add.
            [Parameter(Mandatory=$true, Position=2)]
            [PSCustomObject]$rule 
        )
    
        $ApiVersions = Get-AzureRmResourceProvider -ProviderNamespace Microsoft.Web | 
            Select-Object -ExpandProperty ResourceTypes |
            Where-Object ResourceTypeName -eq 'sites' |
            Select-Object -ExpandProperty ApiVersions
    
        $LatestApiVersion = $ApiVersions[0]
    
        $WebAppConfig = Get-AzureRmResource -ResourceType 'Microsoft.Web/sites/config' -ResourceName $AppServiceName -ResourceGroupName $ResourceGroupName -ApiVersion $LatestApiVersion
    
        $WebAppConfig.Properties.ipSecurityRestrictions =  $WebAppConfig.Properties.ipSecurityRestrictions + @($rule) | 
            Group-Object name | 
            ForEach-Object { $_.Group | Select-Object -Last 1 }
    
        Set-AzureRmResource -ResourceId $WebAppConfig.ResourceId -Properties $WebAppConfig.Properties -ApiVersion $LatestApiVersion -Force    
    }
    

    用法示例:

    Login-AzureRmAccount
    # determine current ip
    $clientIp = Invoke-WebRequest 'https://api.ipify.org' | Select-Object -ExpandProperty Content
    
    $rule = [PSCustomObject]@{
        ipAddress = "$($clientIp)/32"
        action = "Allow"  
        priority = 123 
        name = '{0}_{1}' -f $env:computername, $env:USERNAME 
        description = "Automatically added ip restriction"
    }
    
    Add-AzureIpRestrictionRule -ResourceGroupName "myResourceGroup" -AppServiceName "myAppServiceName" -rule $rule
    

    来源:Configure Azure App Service IP Restrictions using PowerShell

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-07
      • 1970-01-01
      • 2016-02-22
      • 1970-01-01
      • 2016-12-23
      • 2022-08-17
      • 2016-04-10
      相关资源
      最近更新 更多