← Back to overview
November 21, 2012 · Powershell Virtual Machines

Adding multiple endpoints to your Windows Azure Virtual Machines by using a CSV file

In order to manage endpoints of a Virtual Machine you have 2 options: use the portal or use Powershell. If you use the portal it's not so easy to add a list or a range or ports, you would need to add these ports one by one. The other way you can manage these endpoints is by writing Powershell scripts and that's what I did over and over again for the past few months.

What I wanted was some way to easily add endpoints to newly created Virtual Machines. A CSV file would be perfect for this. It's easy to manage (in Excel for example) and you can create different templates to improve reusability. That's why I created the Import-AzureEndpointsFromCSV.ps1 script.

# Arguments.
param  
(
    [Microsoft.WindowsAzure.Management.ServiceManagement.Model.PersistentVMRoleContext]$vm = $(throw "'vm' is required."), 
    [string]$csvFile = $(throw "'csvFile' is required."),
    [string]$parameterSet = $(throw "'parameterSet' is required.")
)
Get-ChildItem "${Env:ProgramFiles(x86)}\Microsoft SDKs\Windows Azure\PowerShell\Azure\*.dll" | ForEach-Object {[Reflection.Assembly]::LoadFile($_) | out-null }

# Add endpoints without loadbalancer.
if ($parameterSet -eq "NoLB")  
{
    Write-Host -Fore Green "Adding NoLB endpoints:"
    $endpoints = Import-Csv $csvFile -header Name,Protocol,PublicPort,LocalPort -delimiter ';' | foreach {
        New-Object PSObject -prop @{
            Name = $_.Name;
            Protocol = $_.Protocol;
            PublicPort = [int32]$_.PublicPort;
            LocalPort = [int32]$_.LocalPort;
        }
    }

    # Add each endpoint.
    Foreach ($endpoint in $endpoints)
    {
    Add-AzureEndpoint -VM $vm -Name $endpoint.Name -Protocol $endpoint.Protocol.ToLower() -PublicPort $endpoint.PublicPort -LocalPort $endpoint.LocalPort
    }
}
# Add endpoints with loadbalancer.
elseif ($parameterSet -eq "LoadBalanced")  
{
    Write-Host -Fore Green "Adding LoadBalanced endpoints:"
    $endpoints = Import-Csv $csvFile -header Name,Protocol,PublicPort,LocalPort,LBSetName,ProbePort,ProbeProtocol,ProbePath -delimiter ';' | foreach {
        New-Object PSObject -prop @{
            Name = $_.Name;
            Protocol = $_.Protocol;
            PublicPort = [int32]$_.PublicPort;
            LocalPort = [int32]$_.LocalPort;
            LBSetName = $_.LBSetName;
            ProbePort = [int32]$_.ProbePort;
            ProbeProtocol = $_.ProbeProtocol;
            ProbePath = $_.ProbePath;
        }
    }

    # Add each endpoint.
    Foreach ($endpoint in $endpoints)
    {
        Add-AzureEndpoint -VM $vm -Name $endpoint.Name -Protocol $endpoint.Protocol.ToLower() -PublicPort $endpoint.PublicPort -LocalPort $endpoint.LocalPort -LBSetName $endpoint.LBSetName -ProbePort $endpoint.ProbePort -ProbeProtocol $endpoint.ProbeProtocol -ProbePath $endpoint.ProbePath
    }
}
else  
{
    $(throw "$parameterSet is not supported. Allowed: NoLB, LoadBalanced")
}

# Update VM.
Write-Host -Fore Green "Updating VM..."  
$vm | Update-AzureVM 
Write-Host -Fore Green "Done."  

With this script you can import 2 types of endpoints:

Creating a CSV file

So what you would do first is create the endpoints in CSV format. By using Excel it's actually very easy to duplicate the defined endpoints by dragging the cells down (but you can also use Notepad or any other text editor). The normal endpoints are defined by these columns: Name, Protocol, PublicPort, LocalPort.

The load balanced endpoints need a little more information: Name, Protocol, PublicPort, LocalPort, LBSetName, ProbePort, ProbeProtocol, ProbePath

After defining the endpoints, simply export to a CSV file:

Importing the CSV file

Calling the script and importing the CSV file is very easy. You simply call the script and pass the required parameters:

In case you want to add load-balanced endpoints you will need to add those to each virtual machine in the cloud service.

Import-AzurePublishSettingsFile 'C:\...'  
Select-AzureSubscription -SubscriptionName ...

# Import normal endpoints.
$vm = Get-AzureVM MyVirtualMachine
.\Import-AzureEndpointsFromCSV.ps1 $vm Normal-Endpoints.csv NoLB

# Import load-balanced endpoints.
$vm = Get-AzureVM MyLoadBalancedCloudSvc
.\Import-AzureEndpointsFromCSV.ps1 $vm[0] LB-Endpoints.csv LoadBalanced
.\Import-AzureEndpointsFromCSV.ps1 $vm[1] LB-Endpoints.csv LoadBalanced
.\Import-AzureEndpointsFromCSV.ps1 $vm[2] LB-Endpoints.csv LoadBalanced

After importing the endpoints you'll see them showing up in the portal:

And after you imported the load-balanced endpoints for all your machines in the same cloud service you'll see that these endpoints show up as being load balanced:

The script and the sample CSV files are available on GitHub.

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket
Comments powered by Disqus