Thank you for reaching out.
I understand you want to add Security Rules to Multiple NSG at Once using PowerShell.
I have modified the script above and was successfully able to run it at my end. Please find the modified script below.
Please do not forget to add your NSG Names, Subscription ID and resource group name in the script below.
# Define the list of NSG names you want to update
$NSGNames = @("EnterNSG1", "EnterNSG2", "EnterNSG3")
Connect-AzAccount
Set-AzContext -Subscription "EnteryourSubscriptionID"
# Define the security rule parameters
$RuleName = "Allow-Example-Rule"
$Priority = 100
$SourceAddressPrefix = "Internet"
$SourcePortRange = "*"
$DestinationAddressPrefix = "*"
$DestinationPortRange = "443"
$Protocol = "Tcp"
$Action = "Allow"
$Direction = "Inbound"
# Loop through each NSG and add the security rule
foreach ($NSGName in $NSGNames) {
# Get the NSG
$NSG = Get-AzNetworkSecurityGroup -ResourceGroupName YourResourceGroup -Name $NSGName
# Add the security rule
$NSG | Add-AzNetworkSecurityRuleConfig `
-Name $RuleName `
-Priority $Priority `
-SourceAddressPrefix $SourceAddressPrefix `
-SourcePortRange $SourcePortRange `
-DestinationAddressPrefix $DestinationAddressPrefix `
-DestinationPortRange $DestinationPortRange `
-Protocol $Protocol `
-Access $Action `
-Direction $Direction
# Update the NSG
Set-AzNetworkSecurityGroup -NetworkSecurityGroup $NSG
}
I was able to execute the script successfully at my end and add the Allow-Example-Rule as shown below.
Hope this helps! Please let me know if you have any additional questions. Thank you!
---------------------------
Please "Accept the answer" if the information helped you. This will help us and others in the community as well.