PowerShell script for automated Octopus Tentacle registering fails to register new machine

Viewed 34

I used an official recommendation to create my automated Octopus Tentacle installation script, and the part of the interest in the script is as follows:

.\Tentacle.exe create-instance --instance "$hostname" --config "$home_path\Tentacle.config" --console
.\Tentacle.exe configure --instance "$hostname" --trust "$trusted_cert" --console
.\Tentacle.exe configure --instance "$hostname" --home "$home_path" --app "$home_path\Applications" --port $port --console
& netsh advfirewall firewall add rule name="Octopus Deploy Tentacle" dir=in action=allow protocol=TCP localport=$port

$envs_cmd = ''
foreach ($env in $environments){
    $envs_cmd += "--environment=$env "
}
$roles_cmd = ''
foreach ($role in $roles){
    $roles_cmd += "--role='$role' "
}

.\Tentacle.exe new-certificate --instance="$hostname" --if-blank --console
    
.\Tentacle.exe register-with --instance "$hostname" --server "$server" --name="$hostname" --apiKey="$api_key" $roles_cmd $envs_cmd --server-comms-port "$port" --comms-style TentaclePassive --console --force

.\Tentacle.exe service --instance "$hostname" --install --start --console

Everything works except the pre-last line

.\Tentacle.exe register-with --instance "$hostname" --server "$server" --name="$hostname" --apiKey="$api_key" $roles_cmd $envs_cmd --server-comms-port "$port" --comms-style TentaclePassive --console --force

that throws the exception:

CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Untitled3.ps1

If I print this particular line to get all the variables values, and copy/paste it to PowerShell console - it works. What withdraws my attention in the line is $roles_cmd $envs_cmd which expression should be something like:

--role="Role1" --environment="Env1" --environment="Env2"

This is the only part of the questionable line where both parameter name and its value are the expression. Can that cause this issue? How to handle that?

1 Answers

I didn't manage to complete the job on this way. For anyone, if needed, the only working script at the end was:

$home_path     = "C:\Octopus"
$roles         = @()
$environments  = @()
$hostname      = [System.NET.DNS]::GetHostByName($null).HostName
$octopusApiKey = ''
$octopusURI    = ''
$header        = @{ "X-Octopus-ApiKey" = $octopusApiKey }
$space         = ''

function GetEnvironmentIds($environments)
{
    
    [System.Collections.ArrayList] $environmentIds = @()
    $collectedEnvironments = (Invoke-RestMethod -Method Get -Uri "$octopusURI/api/$space/environments/all" -Headers $header) | Where-Object {$environmentNames -contains $_.Name}
    foreach ($environment in $collectedEnvironments)
    {
        $environmentIds.Add($environment.Id) | Out-Null
    }
    return $environmentIds
}

cd $home_path
Add-Type -Path 'Newtonsoft.Json.dll'
Add-Type -Path 'Octopus.Client.dll'

$tentacleThumbprint = .\Tentacle.exe show-thumbprint --instance "$hostname" --nologo | Out-String -ErrorAction SilentlyContinue
if ($LASTEXITCODE -ne 0)
{
    $tentacleThumbprint = .\Tentacle.exe new-certificate --instance="$hostname" --if-blank --console | Out-String
}
$endpoint = new-object Octopus.Client.OctopusServerEndpoint $octopusURI, $octopusApiKey
$repository = new-object Octopus.Client.OctopusRepository $endpoint
   
$tentacleEndpoint = New-Object Octopus.Client.Model.EndPoints.ListeningTentacleEndpointResource
$tentacleEndpoint.Thumbprint = $tentacleThumbprint 
$tentacleEndpoint.Uri = "https://"+$hostname+":10933"

$tentacle = New-Object Octopus.Client.Model.MachineResource
$tentacle.Endpoint = $tentacleEndpoint
$tentacle.Uri = "https://"+$hostname+":10933"
$environmentsIds = GetEnvironmentIds($environments)
foreach($environmentId in $environmentsIds) { $tentacle.EnvironmentIds.Add($environmentId) }
foreach($role in $roles){ $tentacle.Roles.Add($role); }
$tentacle.Thumbprint = $tentacleThumbprint
$tentacle.Name = $hostname;

$repository.Machines.Create($tentacle);
Related