Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Uses Powershell to interact with the Octopus REST API to create a deployment to a specific machine / release / environment combination.
Import-Module Octopus-Cmdlets
#$machineName = 'DOGEN09'
#$desiredVersion = '0.666.420.709'
#$environmentString = 'NEW'
#$project = 'OPS_SelfServe'
$releaseId = $null
$baseUrl = 'http://vmoctopusdeploy:8090/'
# connect using Octopus-Cmdlets
# (use choco install psget to 'import-module octopus-cmdlets' if you don't have this)
Connect-OctoServer $baseUrl $apiKey
# Find the ID of the release we want within the project
write-host "Searching for releases in project '$project'..."
$releases = get-octorelease -project $project
foreach ($release in $releases) {
if ($release.version -eq $desiredVersion) {
$releaseId = $release.Id
if ($releaseId -eq $null) {
write-host "ERROR: No release could be found with the version '$desiredVersion'. Exiting"
Exit -1
# Find the Environment ID we want
write-host "Searching for ID of Environment '$environmentString'..."
$environment = Get-OctoEnvironment -name $environmentString
$environmentId = $
if ($environmentId -eq $null) {
write-host "ERROR: No environment could be found with the name '$environmentString'. Exiting"
Exit -1
write-host "Environment ID = "
# find the machine ID
write-host "Searching for ID of machine '$machineName'..."
$machine = Get-OctoMachine -name $machineName
$machineId = $
if ($machine -eq $null) {
write-host "ERROR: No machine could be found with the name '$machineName'. Exiting"
Exit -1
write-host "Machine ID = " $machineId
#first update the release variables
write-host "Updating release variables for release '$releaseId'"
$uri = $baseUrl + '/api/releases/' + $releaseId + '/snapshot-variables'
Invoke-RestMethod -Uri $uri -Method POST -Header @{ "x-Octopus-ApiKey" = $apiKey } -Body $null
# build the JSON with a here-string
<# this would be better with hash but this works too
for more complicated POSTS you can run fiddler while
you kick off a deployment and get a feel for how the
web GUI is building the API call
$json = @"
# couldn't get this to work
$hash = @{
ReleaseId = $releaseId
EnvironmentId = $
SkipActions = @()
SpecificMachineIds = @($
QueueTime = null
FormValues =
$json = $hash | convertto-json
write-host "JSON we're sending = " $json
$uri = $baseUrl + '/api/deployments'
# call the API
$results = Invoke-RestMethod -Uri $uri -Method POST -Header @{ "x-Octopus-ApiKey" = $apiKey } -Body $json
write-host $results
foreach ($link in $results.links) { $uuu = $baseUrl + $link.Web }
# output the URL that the user can go to to watch deployment steps status
write-host "See status here: " $uuu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.