Skip to content

Instantly share code, notes, and snippets.

@hnrkndrssn
Created October 20, 2016 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hnrkndrssn/1f78b2eb5cce389766182bdb7ebad616 to your computer and use it in GitHub Desktop.
Save hnrkndrssn/1f78b2eb5cce389766182bdb7ebad616 to your computer and use it in GitHub Desktop.
PowerShell script to register a new SSH target in Octopus using the REST API
$headers = @{"X-Octopus-ApiKey"="API-xxxxxxxxxxxxxxxxxxxxxxxxx"}
$environments = Invoke-RestMethod "http://url.to.octopus/api/environments/all" -Headers $headers -Method Get
$theEnvironment = $environments | ? { $_.Name -eq "TheEnvironmentName" }
$accounts = Invoke-RestMethod "http://url.to.octopus/api/accounts/all" -Headers $headers -Method Get
$theAccount = $accounts | ? { $_.Name -eq "TheAccount" }
$discovered = Invoke-RestMethod "http://url.to.octopus/api/machines/discover?host=hostnameoripaddress&type=Ssh" -Headers $headers -Method Get
#$discovered.Name = "MySshTargetName" # If you wanted to change the name of the deployment target (default is host name)
$discovered.Roles += "MyRole"
$discovered.EnvironmentIds += $theEnvironment.Id
$discovered.Endpoint.AccountId = $theAccount.Id
$discovered | ConvertTo-Json -Depth 10
Invoke-RestMethod "http://url.to.octopus/api/machines" -Headers $headers -Method Post -Body ($discovered | ConvertTo-Json -Depth 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment