Octopus.Script exported 2020-04-13 by octobob belongs to ‘Octopus’ category.
Step that will attempt to delete a target or worker from Octopus Deploy using the API. If it cannot delete the target or worker it will disable the target and rename it.
Parameters
When steps based on the template are included in a project’s deployment process, the parameters below can be set.
Octopus Base Url
DeleteTarget.Octopus.Base.Url =
The Base URL of the Octopus Deploy Server. Example: https://samples.octopus.app
Octopus API Key
DeleteTarget.Octopus.Api.Key =
The API key of a user who has permissions to delete a target from Octopus Deploy
Target Name
DeleteTarget.Target.Name =
The Name of the Target to delete from Octopus Deploy
Target Type
DeleteTarget.Target.TargetType = Target
What kind of registration is it, a worker or a deployment target
Script body
Steps based on this template will execute the following PowerShell script.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$OctopusAPIKey = $OctopusParameters["DeleteTarget.Octopus.Api.Key"]
$TargetName = $OctopusParameters["DeleteTarget.Target.Name"]
$OctopusUrl = $OctopusParameters["DeleteTarget.Octopus.Base.Url"]
$SpaceId = $OctopusParameters["Octopus.Space.Id"]
$TargetType = $OctopusParameters["DeleteTarget.Target.TargetType"]
Write-Host "Target Name: $TargetName"
Write-Host "Octopus URL: $OctopusUrl"
Write-Host "Space Id: $SpaceId"
Write-Host "Target Type: $TargetType"
$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header.Add("X-Octopus-ApiKey", $OctopusAPIKey)
$baseApiUrl = "$OctopusUrl/api"
$baseApiInformation = Invoke-RestMethod $baseApiUrl -Headers $header
if ((Get-Member -InputObject $baseApiInformation.Links -Name "Spaces" -MemberType Properties) -ne $null)
{
$baseApiUrl = "$baseApiUrl/$SpaceId"
}
$baseTargetUrl = "$baseApiUrl/machines"
if ($TargetType -eq "Worker")
{
$baseTargetUrl = "$baseApiUrl/workers"
Write-Host "Worker was selected, switching over to use the URL $baseTargetUrl"
}
$targetListUrl = "$($baseTargetUrl)?skip=0&take=1000&partialName=$TargetName"
Write-Host "Get a list of all machine using the URL $targetListUrl"
$targetList = (Invoke-RestMethod $targetListUrl -Headers $header)
foreach($target in $targetList.Items)
{
if ($target.Name -eq $TargetName)
{
$targetId = $target.Id
$itemToDeleteEndPoint = "$baseTargetUrl/$targetId"
try
{
Write-Highlight "Deleting the machine $targetId because the name $($target.Name) matches the $TargetName $itemToDeleteEndPoint"
$deleteResponse = (Invoke-RestMethod $itemToDeleteEndPoint -Headers $header -Method Delete)
Write-Highlight "Successfully deleted machine $TargetName"
Write-Host "Delete Response $deleteResponse"
}
catch
{
$currentDate = Get-Date -Format "_MMddyyyy_HHmm"
$target.Name = "$($target.Name)-old$currentdate"
$target.IsDisabled = $True
$jsonRequest = $target | ConvertTo-Json
Write-Highlight "There was an error deleting the machine, renaming it to $($target.name) and disabling it"
Write-Host $_
$machineResponse = Invoke-RestMethod $itemToDeleteEndPoint -Headers $header -Method PUT -Body $jsonRequest
}
break
}
}
Provided under the Apache License version 2.0.
To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.
{
"Id": "e4255fcb-fe7d-4d5b-8ec0-0243e5f48a9c",
"Name": "Delete Target or Worker Registration From Octopus",
"Description": "Step that will attempt to delete a target or worker from Octopus Deploy using the API. If it cannot delete the target or worker it will disable the target and rename it.",
"Version": 1,
"ExportedAt": "2020-04-13T15:37:29.866Z",
"ActionType": "Octopus.Script",
"Author": "octobob",
"Packages": [],
"Parameters": [
{
"Id": "f287ffbb-9b6b-48b8-bea6-2ba399ae570c",
"Name": "DeleteTarget.Octopus.Base.Url",
"Label": "Octopus Base Url",
"HelpText": "The Base URL of the Octopus Deploy Server. Example: https://samples.octopus.app",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "cabd24c3-3eb3-4913-b73a-0df2df02e82d",
"Name": "DeleteTarget.Octopus.Api.Key",
"Label": "Octopus API Key",
"HelpText": "The API key of a user who has permissions to delete a target from Octopus Deploy",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "Sensitive"
}
},
{
"Id": "396591f9-8bd0-4acb-9de6-2dacf0ca2c50",
"Name": "DeleteTarget.Target.Name",
"Label": "Target Name",
"HelpText": "The Name of the Target to delete from Octopus Deploy",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "e01b6695-1cef-423b-b53f-b1185cc3b894",
"Name": "DeleteTarget.Target.TargetType",
"Label": "Target Type",
"HelpText": "What kind of registration is it, a worker or a deployment target",
"DefaultValue": "Target",
"DisplaySettings": {
"Octopus.ControlType": "Select",
"Octopus.SelectOptions": "Target|Deployment Target\nWorker|Worker"
}
}
],
"Properties": {
"Octopus.Action.RunOnServer": "true",
"Octopus.Action.Script.ScriptSource": "Inline",
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptBody": "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12\n\n$OctopusAPIKey = $OctopusParameters[\"DeleteTarget.Octopus.Api.Key\"]\n$TargetName = $OctopusParameters[\"DeleteTarget.Target.Name\"]\n$OctopusUrl = $OctopusParameters[\"DeleteTarget.Octopus.Base.Url\"]\n$SpaceId = $OctopusParameters[\"Octopus.Space.Id\"]\n$TargetType = $OctopusParameters[\"DeleteTarget.Target.TargetType\"]\n\nWrite-Host \"Target Name: $TargetName\"\nWrite-Host \"Octopus URL: $OctopusUrl\"\nWrite-Host \"Space Id: $SpaceId\"\nWrite-Host \"Target Type: $TargetType\"\n\n$header = New-Object \"System.Collections.Generic.Dictionary[[String],[String]]\"\n$header.Add(\"X-Octopus-ApiKey\", $OctopusAPIKey)\n\n$baseApiUrl = \"$OctopusUrl/api\"\n$baseApiInformation = Invoke-RestMethod $baseApiUrl -Headers $header\nif ((Get-Member -InputObject $baseApiInformation.Links -Name \"Spaces\" -MemberType Properties) -ne $null)\n{\n\t$baseApiUrl = \"$baseApiUrl/$SpaceId\"\n}\n\n$baseTargetUrl = \"$baseApiUrl/machines\"\n\nif ($TargetType -eq \"Worker\")\n{\n\t$baseTargetUrl = \"$baseApiUrl/workers\"\n Write-Host \"Worker was selected, switching over to use the URL $baseTargetUrl\"\n}\n\n$targetListUrl = \"$($baseTargetUrl)?skip=0&take=1000&partialName=$TargetName\"\nWrite-Host \"Get a list of all machine using the URL $targetListUrl\"\n\n$targetList = (Invoke-RestMethod $targetListUrl -Headers $header)\n\nforeach($target in $targetList.Items)\n{\n if ($target.Name -eq $TargetName)\n {\n $targetId = $target.Id\n $itemToDeleteEndPoint = \"$baseTargetUrl/$targetId\"\n try\n { \t\n \tWrite-Highlight \"Deleting the machine $targetId because the name $($target.Name) matches the $TargetName $itemToDeleteEndPoint\"\n \t$deleteResponse = (Invoke-RestMethod $itemToDeleteEndPoint -Headers $header -Method Delete)\n Write-Highlight \"Successfully deleted machine $TargetName\"\n Write-Host \"Delete Response $deleteResponse\"\n }\n catch\n { \t\n \t$currentDate = Get-Date -Format \"_MMddyyyy_HHmm\"\n \t$target.Name = \"$($target.Name)-old$currentdate\"\n $target.IsDisabled = $True\n \n $jsonRequest = $target | ConvertTo-Json\n \n Write-Highlight \"There was an error deleting the machine, renaming it to $($target.name) and disabling it\"\n \tWrite-Host $_\n $machineResponse = Invoke-RestMethod $itemToDeleteEndPoint -Headers $header -Method PUT -Body $jsonRequest\n } \n \n break\n }\n}"
},
"Category": "Octopus",
"HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/octopus-delete-machine-registration.json",
"Website": "/step-templates/e4255fcb-fe7d-4d5b-8ec0-0243e5f48a9c",
"Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAC1QTFRFT6Tl////L5Pg8vj9Y67omsvwPJrisdfzfbzs5fL7y+T32Ov5isLucLXqvt31CJPHWwAABMJJREFUeNrs3deW4jAMAFDF3U75/89dlp0ZhiU4blJEjvQ8hYubLJsA00UCBCIQgQhEIAIRi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