How to get list of all deployment target in a space through PowerShell

Hi Team ,

Is there anyway that I can get list of all deployment target in a octopus space through powershell commands or scripts .

Any help would be appreciated ! .

Thanks ,
Atul

Good afternoon @atulbajpayee.work,

Thank you for contacting Octopus Support and welcome to the community!

Great question on ways to get a list of all deployment targets in an Octopus Space through powershell, we have written a script for you which I will post below, this will get you a list of all deployment targets within your spaces:

$OctopusUrl = "Your_Octopus_URL" # Octopus URL
$APIKey = "XXXXXXXXXXXXXXXXXXXX" # API Key that can read the number of machines

# Authenticating to the API
$header = @{ "X-Octopus-ApiKey" = $APIKey }

# Get list of Spaces
$spaces = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/spaces/all" -Headers $header)

# Getting the Deployment Targets in each space
Foreach ($space in $spaces) {
    $spaceid = "$($space.id)"
    $spacename = "$($space.name)"
    write-host "Fetching the Deployment Targets in $($spaceid) ($($spacename))"
    $machines = (Invoke-RestMethod -Method Get -Uri "$OctopusUrl/api/$($spaceid)/machines?skip=0&take=100000" -Headers $header)
    $items = $machines.items
    Foreach ($item in $items) {
             $machineid = $($item.id)
            $machinename = $($item.name)
            write-host "$($machineid) ($($machinename)) - $OctopusUrl/api/$($spaceid)/infrastructure/machines/$($machineid)"
    }
    write-host "---"
}

Please look over the code to make sure you are happy with what it does before running it. You will need to input your Octopus URL and an API key at the top.

Does that suit your needs at all? Let us know if it doesn’t and we will see if we can modify it for you.

Kind Regards,

Clare Martin

Thanks @clare.martin for looking into this .This is helpful .
Can you modify this script for below requirement .

I need to disable all deployment targets in a single space where I can pass space name .

Thanks ,
Atul

Hi @atulbajpayee.work,

Thank you for getting back to us.

No worries, I can make an adjustment for you and get back to you once I’ve run a quick test.

I’ll get back to you with the updated script shortly.

Best Regards,
Donny

Hi @atulbajpayee.work,

Thank you for your patience. Below is a script that can disable all machines in a space:

$ErrorActionPreference = "Stop";

# Define working variables
$octopusURL = "Your_Octopus_URL"
$octopusAPIKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" #Paste your API key
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
$space = "Spaces-XXXX" #Space Id here
$machineEnabled = $false #Use $false to disable targets and $true to enable targets

# Get machines
$machines = (Invoke-RestMethod -Method Get -Uri "$OctopusUrl/api/$($space)/machines?skip=0&take=100000" -Headers $header)
     $items = $machines.items
     Foreach ($item in $items) {
            $machineid = $($item.id)
            $machinename = $($item.name)
            
            # Enable/disable machine
            $item.IsDisabled = !$machineEnabled

            # Put request
            Invoke-RestMethod -Method Put -Uri "$octopusURL/api/$($space)/machines/$($machineid)" -Headers $header -Body ($item | ConvertTo-Json -Depth 10)
            write-host "---"
            write-host "$($machineid) ($($machinename)) - $OctopusUrl/api/$($spaceid)/infrastructure/machines/$($machineid) is disabled"
     }

Let me know if that works for you.

Best Regards,
Donny

Hi @donny.bell ,

Thanks for you support .Just the script I needed .

It worked perfectly fine .Thanks for taking time out .Appreciate it !

Thanks ,
Atul

1 Like

Hey @atulbajpayee.work,

Thank you for letting us know that script was what you needed, I will let Donny know,

Get in touch if you need any further help!

Kind Regards,

Clare Martin

1 Like

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.