Skip to content

Instantly share code, notes, and snippets.

@laurencee
Last active March 27, 2018 19:32
Show Gist options
  • Select an option

  • Save laurencee/a28d21a043d089ac37e008f5ecb63006 to your computer and use it in GitHub Desktop.

Select an option

Save laurencee/a28d21a043d089ac37e008f5ecb63006 to your computer and use it in GitHub Desktop.
Sets an octopus variable for an environment
<#
.SYNOPSIS
Create or update an octopus variable
.DESCRIPTION
Registers a machine as an octopus tentacle and will unregister any existing same-named tentacle.
.PARAMETER projectName
The name of the project in Octopus
.PARAMETER variableName
The name of the variable to be created/updated
.PARAMETER machineRoles
The environment for the variable to be scoped to
#>
param(
[Parameter(Mandatory=$True)]
[string]
$projectName,
[Parameter(Mandatory=$True)]
[string]
$variableName,
[Parameter(Mandatory=$True)]
[string]
$variableValue,
[Parameter(Mandatory=$True)]
[string]
$environmentScope
)
$VerbosePreference = "Continue"
$ErrorActionPreference = "Stop"
$variableSet = Get-OctopusVariableSet -ProjectName $projectName
if (!$variableSet) {
throw "No variable set found for project '$projectName', check the project name is correct and try again"
}
$vars = $VariableSet.Resource.Variables | where{$_.Name -eq $variableName}
$environmentToScopeVariableTo = Get-OctopusEnvironment -EnvironmentName $environmentScope -ResourceOnly
$var = $vars | where { $_.Scope["Environment"] -eq $environmentToScopeVariableTo.Id }
#If the variable doesn't have any Environment scoping, add it
if(!($var)){
$vars[0].Scope.Add([Octopus.Client.Model.ScopeField]::Environment,$environmentToScopeVariableTo.Id)
}
$var.Value = $variableValue
#Save changes made to the variable set
Update-OctopusResource -Resource $variableSet.Resource
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment