-
Notifications
You must be signed in to change notification settings - Fork 128
/
RegisterListeningTentacle.ps1
51 lines (43 loc) · 1.4 KB
/
RegisterListeningTentacle.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# You can get this dll from NuGet
# https://www.nuget.org/packages/Octopus.Client/
# Load octopus.client assembly
Add-Type -Path "path\to\Octopus.Client.dll"
# Octopus variables
$octopusURL = "https://youroctourl"
$octopusAPIKey = "API-YOURAPIKEY"
$spaceName = "default"
$hostName = "MyHost"
$tentaclePort = "10933"
$environmentNames = @("Development", "Production")
$roles = @("MyRole")
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURL, $octopusAPIKey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
$client = New-Object Octopus.Client.OctopusClient $endpoint
try
{
# Get space
$space = $repository.Spaces.FindByName($spaceName)
$repositoryForSpace = $client.ForSpace($space)
# Get environment ids
$environments = $repositoryForSpace.Environments.GetAll() | Where-Object {$environmentNames -contains $_.Name}
# Discover host
$newTarget = $repositoryForSpace.Machines.Discover($hostName, $tentaclePort)
# Add properties to host
foreach ($environment in $environments)
{
# Add to target
$newTarget.EnvironmentIds.Add($environment.Id) | Out-Null
}
foreach ($role in $roles)
{
# Add to target
$newTarget.Roles.Add($role) | Out-Null
}
$newTarget.IsDisabled = $false
# Add to machine to space
$repositoryForSpace.Machines.Create($newTarget)
}
catch
{
Write-Host $_.Exception.Message
}