Windows Azure – Create a Network

Hi,

In two previous posts i was talking about how to :

Ok, now we have almost everything in order to build our first VM, actually, we can build a VM right now, but! But, if you wanna set up a full environement and have you VMs & services connect each others, you need a network. Indeed, Microsoft provides virtual ip by default for your VM.

Let’s look what we have in Azure module about VNet

Get-Command -Module Azure | ? { $_.Name -like "*Vnet*" }

CommandType     Name                                               ModuleName
-----------     ----                                               ----------
Cmdlet          Get-AzureVNetConfig                                Azure
Cmdlet          Get-AzureVNetConnection                            Azure
Cmdlet          Get-AzureVNetGateway                               Azure
Cmdlet          Get-AzureVNetGatewayKey                            Azure
Cmdlet          Get-AzureVNetSite                                  Azure
Cmdlet          Get-WAPackVNet                                     Azure
Cmdlet          New-AzureVNetGateway                               Azure
Cmdlet          Remove-AzureVNetConfig                             Azure
Cmdlet          Remove-AzureVNetGateway                            Azure
Cmdlet          Set-AzureVNetConfig                                Azure
Cmdlet          Set-AzureVNetGateway                               Azure

Like you see, there is only one New-* cmdlet : New-AzureVNetGateway. Actually, this is not what we are looking for, we’ll come back later to discuss this and configure a VPN to make your Azure VMs accessing in your network. All cmdlets *-AzureVNetConfig interest us,  let’s take a look about them.

man set-AzureVNetConfig

NOM
Set-AzureVNetConfig

RÉSUMÉ
Updates the virtual network settings for a Windows Azure cloud service.

SYNTAXE
Set-AzureVNetConfig [-ConfigurationPath] <String> [<CommonParameters>]

DESCRIPTION
The Set-AzureVNetConfig cmdlet updates the network configuration for the current Windows Azure subscription by
specifying a path to anetwork configuration file (.netcfg). The network configuration file defines DNS servers and
subnets for cloud services within a subscription.

 Ok this is not our precious cmdlet, let’s see Get-AzureVNetConfig

man Get-AzureVNetConfig

NOM
    Get-AzureVNetConfig

RÉSUMÉ
    Gets the Windows Azure virtual network configuration from the current subscription.

SYNTAXE
    Get-AzureVNetConfig [-ExportToFile <String>] [<CommonParameters>]

DESCRIPTION
    The Get-AzureVNetConfig retrieves the virtual network configuration of the current Windows Azure subscription. If
    the ExportToFile parameter is specified, a network configuration file is created.

OK, so nothing possible only with PowerShell for now, let’s create it manually on web console, and how the .netcfg file looks like !

AzureManualNetwork

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Ok, now we have our Template Network configured, let’s have a look about the config file generated 🙂

(Get-AzureVNetConfig).XMLConfiguration

We got a beautiful XML file 🙂

<?xml version="1.0" encoding="utf-8"?>
<NetworkConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
" xmlns="http://schemas.microsoft.com/ServiceHosting/2011/07/NetworkConfiguration">
  <VirtualNetworkConfiguration>
    <Dns>
      <DnsServers>
        <DnsServer name="srvdns1" IPAddress="8.8.8.8" />
      </DnsServers>
    </Dns>
    <VirtualNetworkSites>
      <VirtualNetworkSite name="holland" AffinityGroup="PWRSHELL">
        <AddressSpace>
          <AddressPrefix>172.16.0.0/12</AddressPrefix>
        </AddressSpace>
        <Subnets>
          <Subnet name="rotterdam">
            <AddressPrefix>172.16.0.0/29</AddressPrefix>
          </Subnet>
          <Subnet name="brussels">
            <AddressPrefix>172.17.0.8/29</AddressPrefix>
          </Subnet>
        </Subnets>
        <DnsServersRef>
          <DnsServerRef name="srvdns1" />
        </DnsServersRef>
      </VirtualNetworkSite>
    </VirtualNetworkSites>
  </VirtualNetworkConfiguration>
</NetworkConfiguration>

Let’s see how things gone if i modify 2-3 things and try to import it !

Set-AzureVNetConfig -ConfigurationPath F:\testnetwork.netcfg

OperationDescription                    OperationId                             OperationStatus
--------------------                    -----------                             ---------------
Set-AzureVNetConfig                     dbb7b671-dfc2-7698-ad26-fd3b79265888    Succeeded

It seems to work 🙂

AzureNetwork2

 

 

 

It seems the Network is created! GOOD!

This is how you create a network with PowerShell, but one thing bothers me: You can’t have two Network it seems 🙁

The following function will help you create a new network from scratch !

How using it ?

$SubnetObject = @()
$SubnetObject += New-Object -TypeName PSObject -Property @{Name='Holland';SubnetNetwork='10.0.1.0';AddressPrefix=29}
$SubnetObject += New-Object -TypeName PSObject -Property @{Name='France';SubnetNetwork='10.0.2.0';AddressPrefix=29}
$SubnetObject += New-Object -TypeName PSObject -Property @{Name='Bzh';SubnetNetwork='10.0.3.0';AddressPrefix=29}

$DNSList = @()
$DNSList += New-Object -TypeName PSObject -Property @{DNSServer='srvdc1';DNSServerIp='8.8.8.8'}
$DNSList += New-Object -TypeName PSObject -Property @{DNSServer='srvdc2';DNSServerIp='10.10.10.10'}

New-AzureVNetwork -VirtualNetworkSiteName "test" -AffinityGroup "PWRSHELL" -NetworkAddress 10.0.0.0 -Prefix 24 -Subnet $SubnetObject -DNSConfig $DNSList -TempConfigFile E:\temp\azure.nef.cfg

FIrst create two collections for subnets and DNS Servers and then use New-AzureVNetwork to create the Network.

Function New-AzureVNetwork {
    [CmdletBinding()]
    param (
        [String]$VirtualNetworkSiteName,
        [String]$AffinityGroup,
        [String]$NetworkAddress,
        [String]$Prefix,
        $DNSConfig,
        $Subnet,
        [String]$TempConfigFile
    )
    BEGIN {
        $FileStart = @"
<?xml version="1.0" encoding="utf-8"?>
<NetworkConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
" xmlns="http://schemas.microsoft.com/ServiceHosting/2011/07/NetworkConfiguration">
  <VirtualNetworkConfiguration>
    <Dns>
      <DnsServers>
"@

        $FileEnd = @"    
 </DnsServersRef>
     </VirtualNetworkSite>
    </VirtualNetworkSites>
 </VirtualNetworkConfiguration>
</NetworkConfiguration>
"@

        $FilePartOne = @"
      </DnsServers>
    </Dns>
    <VirtualNetworkSites>
    <VirtualNetworkSite name="$VirtualNetworkSiteName" AffinityGroup="$AffinityGroup">
   <AddressSpace>
        <AddressPrefix>$NetworkAddress/$Prefix</AddressPrefix>
    </AddressSpace>
    <Subnets>
"@

        $FilePartTwo = @"
        </Subnets>
      <DnsServersRef>
"@
    }
    PROCESS {
        # Checkin if Affinty group exists
        Try {
            if (Get-AzureAffinityGroup | ? Name -neq $AffinityGroup) {
                throw "Affinity group does not exist!"
            } 
        }
        Catch [AadAuthenticationCantRenewException] {
            throw "Please login to Azure first!"
        }
        Catch {
            Throw "Error listing Affinity group: $($_.Exception.Message)"
        }

        Try {
            # Generating net.config file
            $FileStart | Out-File $TempConfigFile
            $DNSConfig | % {
                '<DnsServer name="' + $($_.DNSServer) + '" IPAddress="' + $($_.DNSServerIp) + '" />' | Out-File $TempConfigFile -Append
            }

            $FilePartOne | Out-File $TempConfigFile -Append
            $Subnet | % {
                '<Subnet name="' + $($_.Name) + '">' | Out-File $TempConfigFile -Append
                '<AddressPrefix>' + $($_.SubnetNetwork) + '/' + $($_.AddressPrefix) + '</AddressPrefix>' | Out-File $TempConfigFile -Append
                '</Subnet>' | Out-File $TempConfigFile -Append
            }
            $FilePartTwo | Out-File $TempConfigFile -Append
            $DNSConfig | % {
                '<DnsServerRef name="' + $($_.DNSServer) + '" />' | Out-File $TempConfigFile -Append
            }
            $FileEnd | Out-File $TempConfigFile -Append
        }
        Catch {
            Throw "Error creating netconfig file: $($_.Exception.Message)"
        }

        # Push net.config file to Azure
        # And Remove the net.config file on system.
        Try {
            if (Test-Path $TempConfigFile) {
                Set-AzureVNetConfig -ConfigurationPath $TempConfigFile
                Remove-Item $TempConfigFile -force
            }
        }
        Catch {
            Throw "Error creating VNetwork: $($_.Exception.Message)"
        }
    }
    END {

    }
}

With that you should be enable to create a network without any web console 😉

Azure8

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

NB: Keep in mind that this function is very basic and may need some improvements ;). Also, i don’t have VPN here to check Site-To-Site connecitvity

Regards,