Tentacle Auto Role registration

for what it’s worth I put this script together to auto register my roles from octopus 1.6 into the machines that are migrated to octopus 2.0
-------------- Script follows ------------------------

function get-server-roles
($serverToFind, $server1, $apikey1)
{
$fullUri = “api/machines”
$m = Connect-to-Octopus $server1 $fullUri $apiKey1
$foundRoles = $m | select Name, Roles| ?{$_.Name -like $serverToFind}
return $foundRoles
}

function Connect-to-Octopus
( [string] $server10,
[string] $uri,
[string] $apiKey10)
{ $fulluri = “http://$server10/$uri
$result = invoke-restMethod -uri $fulluri -Method Get -Headers @{“X-Octopus-ApiKey”=$apiKey10}
return $result
}

function get-environments
($server1,$apikey1)
{ $fullUri = “api/environments”
$e = Connect-to-Octopus $server1 $fullUri $apiKey1
$n = $e | select Id, Name
return $n
}

function get-env-machines
( $env, $server1, $apikey1 ) # expects the octopus convention for environment
{
Foreach($v in $env)
{
$t = $v.Id
$fullUri = “api/environments/$t/machines”
$e = Connect-to-Octopus $server1 $fullUri $apiKey1
$n = $e | select Id, Name, Roles
$n += $n
}
Return $n
}

$apiKey10 = “octopus 1.0 api key goes here.”
$server10=“octopus 1.0 server name goes here”
$srchServer = $OctopusMachineName #assumption that is this running in a job on 1.6
$ServerRoles =get-server-roles $srchServer $server10 $apiKey10 | select Roles
$Roles = $ServerRoles.Roles -join ’ --role '
$server20 = “octopus 2.0 server goes here.”
$apikey20 = “octopus 2.0 api key goes here.”

$env = get-environments $server10 $apikey10

$object=@()
foreach($subenv in $env)
{
$machines = get-env-machines $subenv $server10 $apiKey10
foreach($machine in $machines)
{
$obj = New-Object PSObject
$obj | Add-Member -MemberType NoteProperty -name “ServerName” -Value $null
$obj | add-member -MemberType NoteProperty -name “Environment” -Value $null
$obj.Environment = $subenv.Name
$obj.ServerName = $machine.Name
$object += $obj
}

}
$inEnv = $object | ?{$_.servername -like $srchServer} | select Environment | Sort-Object -unique -property environment | select -ExpandProperty environment
#write-host “$serverName belongs to these environments”
$fEnv = $inEnv -join ’ --environment '
if ($installLoc=( get-itemproperty -path HKLM:\SOFTWARE\Octopus\Tentacle).InstallLocation)
{
cd $installLoc
$runThis = "&.\Tentacle.exe register-with --instance=Tentacle --server=http://$server20 --apiKey=$apikey20 --role $Roles --environment=$fEnv --comms-style=TentaclePassive --force --console"
out-file “.\register-tentacle.ps1” -InputObject $runThis
&.\register-tentacle.ps1
&.\Tentacle.exe --console service --stop
&.\Tentacle.exe --console service --start
del .\register-tentacle.ps1 -Force

}