Iterate variables via powershell and deploy project

Hey

  1. I am trying to figure out how to iterate over all the variables in a project scope.
    I have set up 10 Variables and would like to use powershell for each, to identify which vars are true?
    note: Vars set as checkbox.

  2. Base on Var is true, i would like to deploy a release ( In a Different project ) which the package in the step will be set to name of the var.

Var name = PackageID/Name Value = True

Thanks
Ofer

Greetings Ofer! The variables that you are referring to, are they in a Step Template? Or are they Project variables?

Thanks Shawn

Project Vars.

Oh! These are Prompted variables, yes?

However, to get you started, is something like this what you’re looking for?

Function Get-OctopusProject
{
    # Define parameters
    param(
        $OctopusServerUrl,
        $ApiKey,
        $ProjectName
    )

    # Call API to get all projects, then filter on name
    $octopusProject = Invoke-RestMethod -Method "get" -Uri "$OctopusServerUrl/api/projects/all" -Headers @{"X-Octopus-ApiKey"="$ApiKey"}

    # return the specific project
    return ($octopusProject | Where-Object {$_.Name -eq $ProjectName})
}

Function Get-OctopusProjectVariables
{
    # Define parameters
    param(
        $OctopusDeployProject,
        $OctopusServerUrl,
        $ApiKey
    )

    # Get reference to the variable list
    return (Invoke-RestMethod -Method "get" -Uri "$OctopusServerUrl/api/variables/$($OctopusDeployProject.VariableSetId)" -Headers @{"X-Octopus-ApiKey"="$ApiKey"})
}

$OctopusServerUrl = "https://YourSerber"
$ApiKey = "API-YOURKEY"
$ProjectName = "Project"


try
{
    # Get reference to project
    $octopusProject = Get-OctopusProject -OctopusServerUrl $OctopusServerUrl -ApiKey $ApiKey -ProjectName $ProjectName

    # Get list of existing variables
    $octopusProjectVariables = Get-OctopusProjectVariables -OctopusDeployProject $octopusProject -OctopusServerUrl $OctopusServerUrl -ApiKey $ApiKey

    foreach ($projectVariable in $octopusProjectVariables.Variables)
    {
       Write-Host "$($projectVariable.Value)"
    }
}
catch
{
    Write-Error $_.Exception.Message

    throw
}


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