Abduazim Sobitov
Abduazim Sobitov

Reputation: 61

Loop an array though another array in powershell

I have to allow subnets as networking restrictions in Azure Webapp. We have number of app services and each app service has individual subnets. My script should add all subnets into each app service. My issue is I don't know how to loop subnets so it will add all of them into each app service. Shall somehow increment it or create another nested loop.

$apps = @('trustledger','librarian','statements-document','statement-docmosis','statements-reporting','publicapi', 'payment-consumer', 'address', 'rules','worker','common','caouser','comms','mailroom','docgen-browserless','payment','graphqldocuments','workflow','graphql','property','user','property-consumer','docgen','docmosis','docgen-consumer','reporting','template-generator','document','statement')
$env = 'test'
$region = 'aue'
    
    
ForEach ($app in $apps) {
        
     az webapp config access-restriction add -g "kol-$($env)-$($region)-apps-rg" -n "kol-$($env)-$($region)-$($app)-net6-app" --rule-name "Allow_$($app)"  --action Allow --vnet-name "kol-$($env)-$($region)-vnet" --subnet "$($app)-subnet" --vnet-resource-group kol-$($env)-$($region)-network-rg --priority 300
        
 } 

Upvotes: 2

Views: 153

Answers (1)

Abduazim Sobitov
Abduazim Sobitov

Reputation: 61

No worries guys. All done. Found answer by myself. This is my solution:

# https://learn.microsoft.com/en-us/cli/azure/webapp/config/access-restriction?view=azure-cli-latest#az-webapp-config-access-restriction-add
$apps = @('trustledger','librarian','statements-document','statement-docmosis','statements-reporting','publicapi', 'payment-consumer', 'address', 'rules','worker','common','caouser','comms','mailroom','docgen-browserless','payment','graphqldocuments','workflow','graphql','property','user','property-consumer','docgen','docmosis','docgen-consumer','reporting','template-generator','document','statement')
$env = 'test' 
$region = 'aue' 


foreach ($app in $apps) {
  $filtered = $apps.where{$_ -notin  $app} #filering subnets, so app service does not add its own subnet as restriction rule
  foreach ($a in $filtered) {
    az webapp config access-restriction add -g "kol-$($env)-$($region)-apps-rg" -n kol-$($env)-$($region)-$($app)-net6-app --rule-name "Allow_$($a)"  --action Allow --vnet-name "kol-$($env)-$($region)-vnet" --subnet "$($a)-subnet" --vnet-resource-group kol-$($env)-$($region)-network-rg --priority 300
  }
}

Upvotes: 2

Related Questions