Create an Inbound NSG rule and propogate to all NSG

Anonymous
2023-01-05T22:26:24.947+00:00

Hi There
We have a requirement were we need to create an ALLOW rule in NSG and have the same rule applied to all NSG within a subscription, Is this possible at all? I have tried various methods to no avail. Just wondering if anyone else has come across a similar situation.

Thank you in advance.

Regards
Parul

Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,778 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.9K Reputation points MVP Volunteer Moderator
    2023-01-06T01:08:50.117+00:00

    Hi @Anonymous ,

    this script is working here with the two NSGs (testNSG1 and testNSG2):

    $NSGs = "testNSG1", "testNSG2"  
    $Params = @{  
      'Name'                     = 'Allow_NessusScanner_IN'  
      'Protocol'                 = '*'  
      'Direction'                = 'Inbound'  
      'Priority'                 = 2970  
      'SourceAddressPrefix'      = '10.96.2.69'  
      'SourcePortRange'          = '1234'  
      'DestinationAddressPrefix' = 'VirtualNetwork'  
      'DestinationPortRange'     = '*'  
      'Access'                   = 'Allow'  
    }  
    foreach ($NSG in $NSGs) {  
      Get-AzNetworkSecurityGroup -Name $NSG | Add-AzNetworkSecurityRuleConfig @Params | Set-AzNetworkSecurityGroup  
    }  
    

    The result looks like this:

    276721-image.png


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

8 additional answers

Sort by: Most helpful
  1. Anonymous
    2023-01-05T23:50:07.853+00:00

    Thaks @Andreas Baumgarten will try now cheers

    0 comments No comments

  2. Anonymous
    2023-01-05T23:57:13.443+00:00

    Hi @Andreas Baumgarten
    Just tried your script, no errors however nothing happened, the rule did not get created at all.
    Any other ideas?
    Cheers
    Parul


  3. Anonymous
    2023-01-06T00:42:35.19+00:00

    @Andreas Baumgarten Thanks will wait 30 Mins odd to see if it applies, will keep you posted, I appreciate your help.

    0 comments No comments

  4. Anonymous
    2023-01-06T01:29:33.807+00:00
    Connect-AzAccount  
    $NSGs = Get-AzNetworkSecurityGroup  
     $Params = @{  
       'Name' = 'Allow_NessusScanner_IN'  
       'Protocol' = '*'  
       'Direction' = 'Inbound'  
       'Priority' = 2970  
       'SourceAddressPrefix' = '10.96.2.69'  
       'SourcePortRange' = '*'  
       'DestinationAddressPrefix' = 'VirtualNetwork'  
       'DestinationPortRange' = '*'  
       'Access' = 'Allow'  
       }  
       foreach ($NSG in $NSGs) {  
         Get-AzNetworkSecurityGroup -Name $NSG.Name | Add-AzNetworkSecurityRuleConfig @Params | Set-AzNetworkSecurityGroup  
       }  
    

    Thanks @Andreas Baumgarten it works now, thank you for your help and guidance.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.