New-SCAzureNetwork.ps1


<#PSScriptInfo
 
.VERSION 1.0.0
 
.GUID 9f9173e5-1ea7-4f79-8144-25afdc6fc560
 
.AUTHOR Christopher Martin
 
.COMPANYNAME Microsoft
 
.COPYRIGHT
 
.TAGS Azure Networking IPAM IP Vnet Virtual Network
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
.PRIVATEDATA
 
#>


#Requires -Module @{ModuleName = 'PSSubnetCarver'; ModuleVersion = '2.0.0'}
#Requires -Module Az.Network
#Requires -Module Az.Resources
#Requires -Module Az.Accounts

<#
 .SYNOPSIS
  Deploy a network to Azure
 
 .DESCRIPTION
  Use the PSSubnetCarver and Az PowerShell modules together to carve and deploy a virtual network to Azure
 
 .PARAMETER RootIpAddressRange
  The root address space of the new virtual network to deploy, in CIDR format.
 
 .PARAMETER VirtualNetworkName
  The name of the virtual network to be deployed. It should match Azure naming conventions for virtual networks.
 
 .PARAMETER ResourceGroupName
  The name of the resource group to be deployed. If this group already exists, you will be asked if you want to use it, or overwrite it.
 
 .PARAMETER Location
  The Azure region code used to deploy the resources/resource group to contain the virtual network.
 
 .PARAMETER Subnet
  A hashtable representing the subnets contained within the virtual network. You must have at least one default subnet defined. The key will be the subnet name, and the value should be a CIDR value (for example, @{default=24})
#>


[CmdletBinding()]
Param(
    [Parameter(Mandatory, Position = 0)]
    [string] $RootIpAddressRange,

    [Parameter(Mandatory, Position = 1)]
    [string] $VirtualNetworkName,

    [Parameter(Mandatory, Position = 2)]
    [string] $ResourceGroupName,

    [Parameter(Mandatory, Position = 3)]
    [string] $Location,

    [Parameter(Mandatory, Position = 4)]
    [hashtable] $Subnet
)

$resourceGroup = New-AzResourceGroup -ResourceGroupName $ResourceGroupName -Location $Location

Set-SCContext -RootAddressSpace $RootIpAddressRange

$ranges = @{}

$Subnet.Keys | ForEach-Object -Process { $ranges[$_] = Get-SCSubnet -ReserveCIDR $Subnet[$_] }

$subnetConfigs = $ranges.Keys | ForEach-Object -Process { New-AzVirtualNetworkSubnetConfig -Name $_ -AddressPrefix $ranges[$_] }

$vnet = New-AzVirtualNetwork -Name $VirtualNetworkName -ResourceGroupName $ResourceGroupName -Location $Location -AddressPrefix $RootIpAddressRange -Subnet $subnetConfigs