0

I have an Azure Devops release pipeline in place for deploying files to a webapp.
At the beginning of this process, the current IP addresses are stored in a variable ($ipJson in script below). I then remove all existing IP addresses and add the build agent ip.

After my files are deployed, I need to restore the original IP addresses. The script I'm using works, but is slow and takes over 10 minutes to complete. The number of IP's are between 30-50.

How can I increase the speed of this process?

  • Should I change anything to the script?
  • Should I be doing it in a different way?

Right now it adds each IP address separately. Can't figure out how to do it in bulk - doesn't seem possible with CLI.

$jsonStr= @"
$(ipJson)
"@

$ipObj = $jsonStr | ConvertFrom-Json
foreach($rule in $ipObj.ipSecurityRestrictions){
  if($rule.name -ne "Deny all" -and $rule.name -ne "Allow all" -and $rule.priority -ne 2147483647){
      $params = "-g", "$(ResourceGroupName)", 
      "-n", "$(WebAppName)"

      if(-not [string]::IsNullOrWhiteSpace($rule.priority)){
        $params += "--priority", "$($rule.priority)"
      }
      if(-not [string]::IsNullOrWhiteSpace($rule.ip_address)){
        $params += "--ip-address", "$($rule.ip_address)"
      }
      if(-not [string]::IsNullOrWhiteSpace($rule.action)){
        $params += "--action", "$($rule.action)"
      }
      if(-not [string]::IsNullOrWhiteSpace($rule.description)){
        $params += "--description", "$($rule.description)"
      }
      if(-not [string]::IsNullOrWhiteSpace($rule.name)){
        $params += "--rule-name", "$($rule.name)"
      }
      if(-not [string]::IsNullOrWhiteSpace($rule.vnet_subnet_resource_id)){
        $params += "--vnet-name", "$($rule.vnet_subnet_resource_id)"
      }
      if(-not [string]::IsNullOrWhiteSpace($rule.subnet_mask)){
        $params += "--subnet", "$($rule.subnet_mask)"
      }

      Write-Host "Adding IP rule $($rule.ip_address) with params $params"
      az webapp config access-restriction add @params
      
  }
}
Neo_
  • 282
  • 2
  • 6

1 Answers1

0

To reduce the time of your process you need to use background jobs in a cli.

 $scriptBlock = {
    Process
    }
Start-Job -ScriptBlock $scriptBlock

Foreach loop is being used “foreach($rule in $ipObj.ipSecurityRestrictions)” the $rule is a variable inside your script.

Here $rule will a have a different scope if that is define in a ScriptBlock, So need to pass variables from the local scope to a job, i.e. ArgumentList parameter on Start-Job and change the $rule variable to $args[0] to reflect the automatic array $args in the ScriptBlock. enter image description here

For more info

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15