Remove Server from Azure Load Balancer

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

Uses Service Principal to Remove Server From 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

rsflbAzureAccount =

Service Principal used to connect to Azure

Azure Subscription

rsflbAzureSubscription =

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

Azure Resource Group

rsflbAzureResourceGroup =

Resource Group Load Balancer belongs to. Eg. ProductionAus

Azure Machine Name

rsflbAzureMachineName =

Name of Virtual Machine to remove from Load Balancer. Eg Web01

Script body

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

#region Verify variables

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

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

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

#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.'
}

$nic.IpConfigurations[0].LoadBalancerBackendAddressPools = $null
$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": "5658d525-2a04-47da-85a0-00244976d811",
  "Name": "Remove Server from Azure Load Balancer",
  "Description": "Uses Service Principal to Remove Server From Azure Load Balancer.",
  "Version": 3,
  "ExportedAt": "2020-06-22T10:40:51.529Z",
  "ActionType": "Octopus.AzurePowerShell",
  "Author": "benjimac93",
  "Packages": [],
  "Parameters": [
    {
      "Id": "6efd155e-8ab7-4236-bc16-7748a582e3af",
      "Name": "rsflbAzureAccount",
      "Label": "Azure Account",
      "HelpText": "Service Principal used to connect to Azure",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "AzureAccount"
      }
    },
    {
      "Id": "2cff4dbe-685c-47e0-b7c7-6d6ee25bdc48",
      "Name": "rsflbAzureSubscription",
      "Label": "Azure Subscription",
      "HelpText": "Subscription Load Balancer belongs to. Eg. 00000000-0000-0000-0000-000000000000",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "150ef843-5b2c-43e9-ac12-a8d0cc45565e",
      "Name": "rsflbAzureResourceGroup",
      "Label": "Azure Resource Group",
      "HelpText": "Resource Group Load Balancer belongs to. Eg. ProductionAus",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "7397be30-76e3-4c16-9b8b-05c451a23e70",
      "Name": "rsflbAzureMachineName",
      "Label": "Azure Machine Name",
      "HelpText": "Name of Virtual Machine to remove from Load Balancer. Eg Web01",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.ScriptBody": "#region Verify variables\n\n#Verify rsflbAzureSubscription is not null.\nIf ([string]::IsNullOrEmpty($OctopusParameters['rsflbAzureSubscription']))\n{\n  Throw 'Azure Subscription cannot be null.'\n}\n$azureSubscription = $OctopusParameters['rsflbAzureSubscription']\nWrite-Host ('Azure Subscription: ' + $azureSubscription)\n\n#Verify rsflbAzureResourceGroup is not null.\nIf ([string]::IsNullOrEmpty($OctopusParameters['rsflbAzureResourceGroup']))\n{\n  Throw 'Azure Resource Group cannot be null.'\n}\n$azureResourceGroup = $OctopusParameters['rsflbAzureResourceGroup']\nWrite-Host ('Azure Resource Group: ' + $azureResourceGroup)\n\n#Verify rsflbAzureMachineName is not null.\nIf ([string]::IsNullOrEmpty($OctopusParameters['rsflbAzureMachineName']))\n{\n  Throw 'Azure Machine Name cannot be null.'\n}\n$azureMachineName = $OctopusParameters['rsflbAzureMachineName']\nWrite-Host ('Azure Machine Name: ' + $azureMachineName)\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$nic.IpConfigurations[0].LoadBalancerBackendAddressPools = $null\n$nic | Set-AzureRmNetworkInterface\n\n#endregion",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Azure.AccountId": "#{rsflbAzureAccount}"
  },
  "Category": "Azure",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/remove-server-from-azure-load-balancer.json",
  "Website": "/step-templates/5658d525-2a04-47da-85a0-00244976d811",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl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