This script logs into an Azure subscription, retrieves all virtual networks, and ensures that each subnet has a Network Security Group (NSG) associated with it. If a subnet does not have an NSG, a default NSG is created and associated with the subnet.
This script performs the following steps:
- Logs into the specified Azure subscription.
- Retrieves all virtual networks in the subscription.
- Iterates through each subnet in each virtual network.
- Checks if the subnet has an NSG associated with it.
- If the subnet does not have an NSG and is not in the exclusion list, creates a default NSG and associates it with the subnet.
- Generates a report of the subnets that were modified.
- Running this script may cause connectivity issues with the application.
- Ensure you test this script in a non-production environment first.
- Modify the script as needed.
- Only apply it to the production environment after thorough testing.
.\nsg-subnet-association.ps1 -Subid "your-subscription-id"This example logs into the specified subscription and ensures all subnets have an NSG associated with them, excluding the default subnets.
.\nsg-subnet-association.ps1 -Subid "your-subscription-id" -subnet_to_exclude @("CustomSubnet1", "CustomSubnet2")
# or
.\nsg-subnet-association.ps1 -Subid "your-subscription-id" -subnet_to_exclude "CustomSubnet1", "CustomSubnet2"This example logs into the specified subscription and ensures all subnets have an NSG associated with them, excluding the default subnets and the custom subnets "CustomSubnet1" and "CustomSubnet2".
| Parameter | Type | example |
|---|---|---|
| Subid | String | "your-subscription-id" |
| subnet_to_exclude | Array | @("CustomSubnet1", "CustomSubnet2") |