How to create multiple Subnets in pre-existing Vnet Powershell

DeHaven Graham 101 Reputation points
2022-05-17T20:48:39.363+00:00

I have the following code which works good for adding a few subnets but I would like for this script to leverage a CSV file to import from and add the subnets into a pre-existing Vnet?

$appssubnet = New-AzVirtualNetworkSubnetConfig -Name servers -AddressPrefix "172.16.1.0/24" -NetworkSecurityGroupId "/subscriptions/xxxxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx/resourceGroups/powershell-grp/providers/Microsoft.Network/networkSecurityGroups/app-nsg1"`
-RouteTableId "/subscriptions/xxxxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx/resourceGroups/powershell-grp/providers/Microsoft.Network/routeTables/powershell-rt"
$serversubnet = New-AzVirtualNetworkSubnetConfig -Name apps -AddressPrefix "172.16.2.0/24" -RouteTableId "/subscriptions/xxxxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx/resourceGroups/powershell-grp/providers/Microsoft.Network/routeTables/powershell-rt"
$dmz = New-AzVirtualNetworkSubnetConfig -Name dmz -AddressPrefix "172.16.3.0/24"
$updatedvnet = New-AzVirtualNetwork -Name "testsubnet" -ResourceGroupName "powershell-grp" -Location "North Europe" -AddressPrefix "172.16.0.0/16" -Subnet $serversubnet, $dmz, $appssubnet -Force:$true
$updatedvnet | Set-AzVirtualNetwork
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,472 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,537 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Andriy Bilous 11,421 Reputation points MVP
    2022-05-18T04:14:46.713+00:00

    Hello @DeHaven Graham

    Here is a code:

    Import-Csv .\subnets.csv | ForEach-Object {  
          
        $appssubnet = New-AzVirtualNetworkSubnetConfig -Name servers -AddressPrefix $_.appssubnet -NetworkSecurityGroupId "/subscriptions/xxxxxxxxxx-xxxx-xxxx-xxxxx-   
        xxxxxxxxxxxx/resourceGroups/powershell-grp/providers/Microsoft.Network/networkSecurityGroups/app-nsg1"`  
        -RouteTableId "/subscriptions/xxxxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx/resourceGroups/powershell-grp/providers/Microsoft.Network/routeTables/powershell-rt"  
        $serversubnet = New-AzVirtualNetworkSubnetConfig -Name apps -AddressPrefix $_.serversubnet -RouteTableId "/subscriptions/xxxxxxxxxx-xxxx-xxxx-xxxxx-   
        xxxxxxxxxxxx/resourceGroups/powershell-grp/providers/Microsoft.Network/routeTables/powershell-rt"  
        $dmz = New-AzVirtualNetworkSubnetConfig -Name dmz -AddressPrefix $_.dmz  
        $updatedvnet = New-AzVirtualNetwork -Name "testsubnet" -ResourceGroupName "powershell-grp" -Location "North Europe" -AddressPrefix $_.addressprefix -Subnet $serversubnet, $dmz,   
        $appssubnet -Force:$true  
        $updatedvnet | Set-AzVirtualNetwork  
    }  
    

    CSV:
    202946-image.png

    0 comments No comments

  2. ChaitanyaNaykodi-MSFT 26,201 Reputation points Microsoft Employee
    2022-05-18T05:30:04.217+00:00

    Hello @DeHaven Graham ,

    If I have understood the questions correctly you want to add multiple Subnets in pre-existing Vnet using a PowerShell script. You can try running below mentioned code.

    Connect-AzAccount -Identity  
    Set-AzContext -Subscription "<Sub_Id>"  
    Import-Csv "<file path>\Book1.csv" |`  
        ForEach-Object {  
            $Name = $_.Subnet_Name  
            $Address = $_.Subnet_Address  
            $virtualNetwork= Get-AzVirtualNetwork -Name myVNet -ResourceGroupName <rg_name>  
            Add-AzVirtualNetworkSubnetConfig -Name $Name -AddressPrefix $Address -VirtualNetwork $virtualNetwork  
            $virtualNetwork | Set-AzVirtualNetwork  
              
        }  
    

    Example csv. You can add the extra field as per your requirements
    203026-image.png

    I tried it on my end to add subnets in myVnet virtual network.
    203037-image.png

    Hope this helps! Please let me know if you have ny additional questions. Thank you!

    0 comments No comments

  3. Limitless Technology 39,651 Reputation points
    2022-05-25T07:08:41.357+00:00

    Hi,

    The best method for creating multiple subnets will be importing from csv file.
    Copy all the subnets in to excel file and save it as csv(Comma-separated values).

    Since you already have a VNET, before creating subnets first set the subscription in which VNET is created.

    Set-AzContext -Subscription "XXXXXXXXXX"

    then import the file,

    Import-Csv -Path "C:\Documents and Settings\subnets"

    then run the foreach loop,

    ForEach-Object {
    $Name = $_.Subnet_Name
    $Address = $_.Subnet_Address
    $virtualNetwork= Get-AzVirtualNetwork -Name myVNet -ResourceGroupName <rgname>
    Add-AzVirtualNetworkSubnetConfig -Name $Name -AddressPrefix $Address -VirtualNetwork $virtualNetwork
    $virtualNetwork | Set-AzVirtualNetwork

     }
    

    Hope this will help you !!


    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

  4. Gabor Lakatos 0 Reputation points
    2024-03-06T17:15:01.8566667+00:00

    Same using hash-table

    PS /home/gabor> [hashtable]$subnets = @{                                                                                 
    
    "subnet1" = "10.1.1.0/24" 
    "subnet2" = "10.1.2.0/24" 
    "AzureBastion" = "10.1.30.0/27"
    "GatewaySubnet" = "10.1.3.0/27"
    
    }
    

    PS /home/gabor> foreach ($subnet in $subnets.GetEnumerator()) {
    
    $vnet = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $rg
    Add-AzVirtualNetworkSubnetConfig -Name $($subnet.Name) -AddressPrefix $($subnet.Value) -VirtualNetwork $vnet
    $vnet | Set-AzVirtualNetwork
    
    }
    

    PS /home/gabor> Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $rg | Select-Object -Property Subnets
    
    Subnets
    -------
    {AzureBastion, subnet1, GatewaySubnet, subnet2}
    
    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.