Add Server to Azure Load Balancer

Octopus.AzurePowerShell exported 2020-06-22 by benjimac93 belongs to ‘Azure’ category.

Uses Service Principal to Add Server to Azure Load Balancer.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Azure Account

psbilbAzureAccount =

Service Principal used to connect to Azure

Azure Subscription

psbilbAzureSubscription =

Subscription Load Balancer belongs to. Eg. 00000000-0000-0000-0000-000000000000

Azure Resource Group

psbilbAzureResourceGroup =

Resource Group Load Balancer belongs to. Eg. ProductionAus

Azure Machine Name

psbilbAzureMachineName =

Name of Virtual Machine to add to Load Balancer. Eg Web01

Azure Load Balancer

psbilbAzureLoadBalancer =

Name of Load Balancer. Eg. LoadBalacer01

Azure Load Balancer Back End Pool Name

psbilbAzureLoadBalancerBackEndPoolName =

Name of Backend Pool on Azure Load Balancer. Eg. WebServers

Script body

Steps based on this template will execute the following PowerShell script.

#region Verify variables

#Verify psbilbAzureSubscription is not null.
If ([string]::IsNullOrEmpty($OctopusParameters['psbilbAzureSubscription']))
{
  Throw 'Azure Subscription cannot be null.'
}
$azureSubscription = $OctopusParameters['psbilbAzureSubscription']
Write-Host ('Azure Subscription: ' + $azureSubscription)

#Verify psbilbAzureResourceGroup is not null.
If ([string]::IsNullOrEmpty($OctopusParameters['psbilbAzureResourceGroup']))
{
  Throw 'Azure Resource Group cannot be null.'
}
$azureResourceGroup = $OctopusParameters['psbilbAzureResourceGroup']
Write-Host ('Azure Resource Group: ' + $azureResourceGroup)

#Verify psbilbAzureMachineName is not null.
If ([string]::IsNullOrEmpty($OctopusParameters['psbilbAzureMachineName']))
{
  Throw 'Azure Machine Name cannot be null.'
}
$azureMachineName = $OctopusParameters['psbilbAzureMachineName']
Write-Host ('Azure Machine Name: ' + $azureMachineName)

#Verify psbilbAzureLoadBalancer is not null.
If ([string]::IsNullOrEmpty($OctopusParameters['psbilbAzureLoadBalancer']))
{
  Throw 'Azure Load Balancer cannot be null.'
}
$azureLoadBalancer = $OctopusParameters['psbilbAzureLoadBalancer']
Write-Host ('Azure Load Balancer: ' + $azureLoadBalancer)

#Verify psbilbAzureLoadBalancerBackEndPoolName is not null.
If ([string]::IsNullOrEmpty($OctopusParameters['psbilbAzureLoadBalancerBackEndPoolName']))
{
  Throw 'Azure Load Balancer Backend Pool Name cannot be null.'
}
$azureLoadBalancerBackendPoolName = $OctopusParameters['psbilbAzureLoadBalancerBackEndPoolName']
Write-Host ('Azure Load Balancer Backend Pool Name: ' + $azureLoadBalancerBackendPoolName)

#endregion

#region Process

Set-AzureRmContext -SubscriptionName $azureSubscription

$azureVM = Get-AzureRmVM -ResourceGroupName $azureResourceGroup -Name $azureMachineName
If (!$azureVM)
{
  Throw 'Could not retrieve server from Azure needed to remove from Load Balancer.'
}

$nic = (Get-AzureRmNetworkInterface -ResourceGroupName $azureResourceGroup | Where-Object {$_.VirtualMachine.Id -eq $azureVM.Id})
If (!$nic)
{
  Throw 'Could not retrieve NIC from Azure needed to remove from Load Balancer.'
}

$loadBalancer = Get-AzureRmLoadBalancer -Name $azureLoadBalancer -ResourceGroupName $azureResourceGroup
If (!$loadBalancer)
{
  Throw 'Could not retrieve Load Balancer info from Azure.'
}

$ap = Get-AzureRmLoadBalancerBackendAddressPoolConfig -Name #{psbilbAzureLoadBalancerBackEndPoolName} -LoadBalancer $loadBalancer

$nic.IpConfigurations[0].LoadBalancerBackendAddressPools = $ap
$nic | Set-AzureRmNetworkInterface

#endregion

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "540de6f6-f1c6-4e1c-9062-9e78570074f6",
  "Name": "Add Server to Azure Load Balancer",
  "Description": "Uses Service Principal to Add Server to Azure Load Balancer.",
  "Version": 2,
  "ExportedAt": "2020-06-22T10:39:16.450Z",
  "ActionType": "Octopus.AzurePowerShell",
  "Author": "benjimac93",
  "Packages": [],
  "Parameters": [
    {
      "Id": "85e7fd91-8976-415a-82b7-1303e150b447",
      "Name": "psbilbAzureAccount",
      "Label": "Azure Account",
      "HelpText": "Service Principal used to connect to Azure",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "AzureAccount"
      }
    },
    {
      "Id": "e494b221-0b8e-4f02-84f1-079199e9f4b4",
      "Name": "psbilbAzureSubscription",
      "Label": "Azure Subscription",
      "HelpText": "Subscription Load Balancer belongs to. Eg. 00000000-0000-0000-0000-000000000000",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "4e0b35a8-c9f7-49e0-b9d2-e8121cf589af",
      "Name": "psbilbAzureResourceGroup",
      "Label": "Azure Resource Group",
      "HelpText": "Resource Group Load Balancer belongs to. Eg. ProductionAus",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "44dfd700-28eb-463c-923a-58cc5ab76eb0",
      "Name": "psbilbAzureMachineName",
      "Label": "Azure Machine Name",
      "HelpText": "Name of Virtual Machine to add to Load Balancer. Eg Web01",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "337bb455-a17c-4251-9543-26cb9ffd7087",
      "Name": "psbilbAzureLoadBalancer",
      "Label": "Azure Load Balancer",
      "HelpText": "Name of Load Balancer. Eg. LoadBalacer01",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "0162154d-83ac-41ca-bd6d-8f983187eccd",
      "Name": "psbilbAzureLoadBalancerBackEndPoolName",
      "Label": "Azure Load Balancer Back End Pool Name",
      "HelpText": "Name of Backend Pool on Azure Load Balancer. Eg. WebServers",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.ScriptBody": "#region Verify variables\n\n#Verify psbilbAzureSubscription is not null.\nIf ([string]::IsNullOrEmpty($OctopusParameters['psbilbAzureSubscription']))\n{\n  Throw 'Azure Subscription cannot be null.'\n}\n$azureSubscription = $OctopusParameters['psbilbAzureSubscription']\nWrite-Host ('Azure Subscription: ' + $azureSubscription)\n\n#Verify psbilbAzureResourceGroup is not null.\nIf ([string]::IsNullOrEmpty($OctopusParameters['psbilbAzureResourceGroup']))\n{\n  Throw 'Azure Resource Group cannot be null.'\n}\n$azureResourceGroup = $OctopusParameters['psbilbAzureResourceGroup']\nWrite-Host ('Azure Resource Group: ' + $azureResourceGroup)\n\n#Verify psbilbAzureMachineName is not null.\nIf ([string]::IsNullOrEmpty($OctopusParameters['psbilbAzureMachineName']))\n{\n  Throw 'Azure Machine Name cannot be null.'\n}\n$azureMachineName = $OctopusParameters['psbilbAzureMachineName']\nWrite-Host ('Azure Machine Name: ' + $azureMachineName)\n\n#Verify psbilbAzureLoadBalancer is not null.\nIf ([string]::IsNullOrEmpty($OctopusParameters['psbilbAzureLoadBalancer']))\n{\n  Throw 'Azure Load Balancer cannot be null.'\n}\n$azureLoadBalancer = $OctopusParameters['psbilbAzureLoadBalancer']\nWrite-Host ('Azure Load Balancer: ' + $azureLoadBalancer)\n\n#Verify psbilbAzureLoadBalancerBackEndPoolName is not null.\nIf ([string]::IsNullOrEmpty($OctopusParameters['psbilbAzureLoadBalancerBackEndPoolName']))\n{\n  Throw 'Azure Load Balancer Backend Pool Name cannot be null.'\n}\n$azureLoadBalancerBackendPoolName = $OctopusParameters['psbilbAzureLoadBalancerBackEndPoolName']\nWrite-Host ('Azure Load Balancer Backend Pool Name: ' + $azureLoadBalancerBackendPoolName)\n\n#endregion\n\n#region Process\n\nSet-AzureRmContext -SubscriptionName $azureSubscription\n\n$azureVM = Get-AzureRmVM -ResourceGroupName $azureResourceGroup -Name $azureMachineName\nIf (!$azureVM)\n{\n  Throw 'Could not retrieve server from Azure needed to remove from Load Balancer.'\n}\n\n$nic = (Get-AzureRmNetworkInterface -ResourceGroupName $azureResourceGroup | Where-Object {$_.VirtualMachine.Id -eq $azureVM.Id})\nIf (!$nic)\n{\n  Throw 'Could not retrieve NIC from Azure needed to remove from Load Balancer.'\n}\n\n$loadBalancer = Get-AzureRmLoadBalancer -Name $azureLoadBalancer -ResourceGroupName $azureResourceGroup\nIf (!$loadBalancer)\n{\n  Throw 'Could not retrieve Load Balancer info from Azure.'\n}\n\n$ap = Get-AzureRmLoadBalancerBackendAddressPoolConfig -Name #{psbilbAzureLoadBalancerBackEndPoolName} -LoadBalancer $loadBalancer\n\n$nic.IpConfigurations[0].LoadBalancerBackendAddressPools = $ap\n$nic | Set-AzureRmNetworkInterface\n\n#endregion\n",
    "Octopus.Action.Azure.AccountId": "#{psbilbAzureAccount}",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "Azure",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/add-server-to-azure-load-balancer.json",
  "Website": "/step-templates/540de6f6-f1c6-4e1c-9062-9e78570074f6",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////AHjXf7vrv931QJrh7/f8EIDaIIncMJHfYKvmz+b3n8zw3+76j8Ttr9XycLPpUKLkkKvYFAAABGZJREFUeNrsnNmCqjoQRc1MEiD8/9cer7Yt2KBJZQC8ez07sKlKTQlcLgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAzoUSnt8YxXlFuGHSbIaxvj+fip4btkLn1blkWLaF5v03yLhLOYlVuGYfMOMZzNGxCOzhjTJqFkXnjq3Dr1yyvPI3hGl3Ih3zzHHNKudRstRhX5O58vIcShY67Gq6EPIESlzUWvazaGAOGbvU7ArDu/g8M4o8opDZWvbvPzlL/MMBE8jT9T9W7PbAJlHPTBFRf9yVTEcs63msXz2UHLSgf650G/d5t+wjbxxB2UCMqGrk8/LFSD7uJMeNt5bcJCyQZy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