Save Octopus Output Variable With Scoping

Octopus.Script exported 2021-10-21 by harrisonmeister belongs to ‘Octopus’ category.

Saves an output variable to the given project / library variable set with scoping

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

API Key

StepTemplate_ApiKey =

Provide an Octopus API Key with appropriate permissions to save the variable

Deployment Step

StepTemplate_DeploymentStep =

Select the step with the output variable

Variable Name

StepTemplate_VariableName =

Name of the variable used when it was set

Variable Set Type

StepTemplate_VariableSetType = project

If the output variable is being saved to a project or library variable set

Variable Set Name

StepTemplate_VariableSetName = #{Octopus.Project.Name}

Name of the project or library variable set where the output variable should be saved

Target Variable Name

StepTemplate_TargetName = #{StepTemplate_VariableName}

The name to use when saving the variable.

Note: The original variable name the default value.

Is Sensitive?

StepTemplate_IsSensitive = False

If the variable should be marked as sensitive and the value masked from logs

Environment Scope

StepTemplate_EnvironmentScope =

The environment scope to use when creating or updating the variable.

This can be a list delimited by the Scope Delimiter (, by default)

Role Scope

StepTemplate_RoleScope =

The role scope to use when creating or updating the variable.

This can be a list delimited by the Scope Delimiter (, by default)

Target Scope

StepTemplate_MachineScope =

The target scope to use when creating or updating the variable.

This can be a list delimited by the Scope Delimiter (, by default)

Action Scope

StepTemplate_ActionScope =

The action scope to use when creating or updating the variable.

This can be a list delimited by the Scope Delimiter (, by default)

Channel Scope

StepTemplate_ChannelScope =

The channel scope to use when creating or updating the variable.

This can be a list delimited by the Scope Delimiter (, by default)

Tenant Tag Scope

StepTemplate_TenantTagScope =

The tenant tag scope to use when creating or updating the variable. The value should be in the format TagSetName/TagValue.

This can be a list delimited by the Scope Delimiter (, by default)

Scope Delimiter

StepTemplate_ScopeDelimiter = ,

The delimiter used by scope lists.

Script body

Steps based on this template will execute the following PowerShell script.

$ErrorActionPreference = 'Stop'

$StepTemplate_BaseUrl = $OctopusParameters['#{if Octopus.Web.ServerUri}Octopus.Web.ServerUri#{else}Octopus.Web.BaseUrl#{/if}'].Trim('/')
if ([string]::IsNullOrWhiteSpace($StepTemplate_ApiKey)) {
    throw "The step parameter 'API Key' was not found. This step requires an API Key to function, please provide one and try again."
}

function Invoke-OctopusApi {
    param(
        [Parameter(Position = 0, Mandatory)]$Uri,
        [ValidateSet("Get", "Put")]$Method = 'Get',
        $Body
    )
    $requestParameters = @{
        Uri = ('{0}/{1}' -f $StepTemplate_BaseUrl, $Uri.TrimStart('/'))
        Method = $Method
        Headers = @{ "X-Octopus-ApiKey" = $StepTemplate_ApiKey }
        UseBasicParsing = $true
    }
    if ($null -ne $Body) { $requestParameters.Add('Body', ($Body | ConvertTo-Json -Depth 10)) }
    Write-Verbose "$($Method.ToUpperInvariant()) $($requestParameters.Uri)"   
    Invoke-WebRequest @requestParameters | % Content | ConvertFrom-Json | Write-Output
}

function Test-SpacesApi {
	Write-Verbose "Checking API compatibility";
	$rootDocument = Invoke-OctopusApi 'api/';
    if($rootDocument.Links -ne $null -and $rootDocument.Links.Spaces -ne $null) {
    	Write-Verbose "Spaces API found"
    	return $true;
    }
    Write-Verbose "Pre-spaces API found"
    return $false;
}

if(Test-SpacesApi) {
	$spaceId = $OctopusParameters['Octopus.Space.Id'];
    if([string]::IsNullOrWhiteSpace($spaceId)) {
        throw "This step needs to be run in a context that provides a value for the 'Octopus.Space.Id' system variable. In this case, we received a blank value, which isn't expected - please reach out to our support team at https://help.octopus.com if you encounter this error.";
    }
	$baseApiUrl = "api/$spaceId" ;
} else {
	$baseApiUrl = "api" ;
}

function Check-Scope {
	param(
    	[Parameter(Position = 0, Mandatory)]
        [string]$ScopeName,
        [Parameter(Position = 1, Mandatory)]
        [AllowEmptyCollection()]
        [array]$ScopeValues,
        [Parameter(Position = 2)]
        [array]$ExistingScopeValue,
        [Parameter(Position = 3)]
        [string]$LookingForScopeValue
    )
    
    if ($LookingForScopeValue) {
     	
    	Write-Host "Checking $ScopeName Scope"
        
        $scopes = Create-Scope $ScopeName $ScopeValues $LookingForScopeValue
        
        if (-not ($ExistingScopeValue -and (Compare-Object $ExistingScopeValue $scopes) -eq $null)) {
        	Write-Host "$ScopeName scope does not match"
        	return $false
        }
        Write-Host "$ScopeName scope matches"
    } else {
    	if ($ExistingScopeValue) {
        	Write-Host "$ScopeName scope does not match"
        	return $false
        }
    }
    
    return $true
}

function Create-Scope {
	param(
    	[Parameter(Position = 0, Mandatory)]
        [string]$ScopeName,
        [Parameter(Position = 1, Mandatory)]
        [array]$ScopeValues,
        [Parameter(Position = 2)]
        [string]$ScopeValue
    )
    
    $scopes = @()
    
    foreach ($scope in $ScopeValue.Split($StepTemplate_ScopeDelimiter)) {
    	if ($ScopeName -eq "TenantTag") {
    		$value = $ScopeValues | Where { $_.Id -eq $scope } | Select -First 1
    	}
        else {
    		$value = $ScopeValues | Where { $_.Name -eq $scope } | Select -First 1
    	}
    	$scopes += $value.Id
    }
    
    return $scopes
}

$outputVariableKey = "Octopus.Action[${StepTemplate_DeploymentStep}].Output.${StepTemplate_VariableName}"
if (!$OctopusParameters.ContainsKey($outputVariableKey)) {
    throw "Variable '$StepTemplate_VariableName' has not been output from '$StepTemplate_DeploymentStep'"
}
$isSensitive = [System.Convert]::ToBoolean($StepTemplate_IsSensitive)
$variableType = if ($isSensitive) { "Sensitive" } else { "String" }

$variableValue = $OctopusParameters[$outputVariableKey]
Write-Host "Name: $StepTemplate_VariableName"
Write-Host "Type: $variableType"
Write-Host "Value: $(if ($isSensitive) { "********" } else { $variableValue })"
Write-Host ' '

Write-Host "Retrieving $StepTemplate_VariableSetType variable set..."
if ($StepTemplate_VariableSetType -eq 'project') {
    $variableSet = Invoke-OctopusApi "$baseApiUrl/projects/all" | ? Name -eq $StepTemplate_VariableSetName | % { Invoke-OctopusApi $_.Links.Variables }
}
if ($StepTemplate_VariableSetType -eq 'library') {
    $variableSet = Invoke-OctopusApi "$baseApiUrl/libraryvariablesets/all?ContentType=Variables" | ? Name -eq $StepTemplate_VariableSetName | % { Invoke-OctopusApi $_.Links.Variables }
}
if ($null -eq $variableSet) {
    throw "Unable to find $StepTemplate_VariableSetType variable set '$StepTemplate_VariableSetName'"
}

$variableExists = $false

$variableSet.Variables | ? Name -eq $StepTemplate_TargetName | % {
	if (-not (Check-Scope 'Environment' $variableSet.ScopeValues.Environments $_.Scope.Environment $StepTemplate_EnvironmentScope)) {
    	return
    }

	if (-not (Check-Scope 'Machine' $variableSet.ScopeValues.Machines $_.Scope.Machine $StepTemplate_MachineScope)) {
    	return
    }
    
    if (-not (Check-Scope 'Role' $variableSet.ScopeValues.Roles $_.Scope.Role $StepTemplate_RoleScope)) {
    	return
    }
    
    if (-not (Check-Scope 'Action' $variableSet.ScopeValues.Actions $_.Scope.Action $StepTemplate_ActionScope)) {
    	return
    }
    
    if (-not (Check-Scope 'Channel' $variableSet.ScopeValues.Channels $_.Scope.Channel $StepTemplate_ChannelScope)) {
    	return
    }
    
    if (-not (Check-Scope 'TenantTag' $variableSet.ScopeValues.TenantTags $_.Scope.TenantTag $StepTemplate_TenantTagScope)) {
    	return
    }

    Write-Host "Updating existing variable..."
    Write-Host "Existing value:"
	Write-Host "$(if ($isSensitive) { "********" } else { $_.Value })"
    $_.Value = $variableValue
    $_.Type = $variableType
    $_.IsSensitive = $isSensitive
    $variableExists = $true
}

if (!$variableExists) {
    Write-Host "Creating new variable..."
    
    $variable = @{
        Name = $StepTemplate_TargetName
        Value = $variableValue
        Type = $variableType
        IsSensitive = $isSensitive
        Scope = @{}
    }
    
    if ($StepTemplate_EnvironmentScope) {
    	$variable.Scope['Environment'] = (Create-Scope 'Environment' $variableSet.ScopeValues.Environments $StepTemplate_EnvironmentScope)
    }
    if ($StepTemplate_RoleScope) {
    	$variable.Scope['Role'] = (Create-Scope 'Role' $variableSet.ScopeValues.Roles $StepTemplate_RoleScope)
    }
    if ($StepTemplate_MachineScope) {
    	$variable.Scope['Machine'] = (Create-Scope 'Machine' $variableSet.ScopeValues.Machines $StepTemplate_MachineScope)
    }
    if ($StepTemplate_ActionScope) {
    	$variable.Scope['Action'] = (Create-Scope 'Action' $variableSet.ScopeValues.Actions $StepTemplate_ActionScope)
    }
    if ($StepTemplate_ChannelScope) {
    	$variable.Scope['Channel'] = (Create-Scope 'Channel' $variableSet.ScopeValues.Channels $StepTemplate_ChannelScope)
    }
    if ($StepTemplate_TenantTagScope) {
        $variable.Scope['TenantTag'] = (Create-Scope 'TenantTag' $variableSet.ScopeValues.TenantTags $StepTemplate_TenantTagScope)
    }
    
    $variableSet.Variables += $variable
}

Write-Host "Saving updated variable set..."
Invoke-OctopusApi $variableSet.Links.Self -Method Put -Body $variableSet | Out-Null

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "75a8f263-b690-4a55-84b7-9165ac9b6196",
  "Name": "Save Octopus Output Variable With Scoping",
  "Description": "Saves an [output variable](https://octopus.com/docs/deploying-applications/variables/output-variables) to the given project / library variable set with scoping",
  "Version": 6,
  "ExportedAt": "2021-10-21T15:24:33.519Z",
  "ActionType": "Octopus.Script",
  "Author": "harrisonmeister",
  "Packages": [],
  "Parameters": [
    {
      "Id": "bdbd77a9-cc0f-49f0-afac-6403e8ba87e4",
      "Name": "StepTemplate_ApiKey",
      "Label": "API Key",
      "HelpText": "Provide an Octopus API Key with appropriate permissions to save the variable",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "Sensitive"
      }
    },
    {
      "Id": "ed5cf637-06ed-4c38-8a8d-f17c523ec6ba",
      "Name": "StepTemplate_DeploymentStep",
      "Label": "Deployment Step",
      "HelpText": "Select the step with the [output variable](https://octopus.com/docs/deploying-applications/variables/output-variables)",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "StepName"
      }
    },
    {
      "Id": "3d6f35a9-99ae-481e-b393-4319b70e0ac0",
      "Name": "StepTemplate_VariableName",
      "Label": "Variable Name",
      "HelpText": "Name of the [variable used when it was set](https://octopus.com/docs/deploying-applications/variables/output-variables#Outputvariables-Settingoutputvariablesusingscripts)",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "9265ca02-9c0f-48ad-9cb1-65d28d5a446e",
      "Name": "StepTemplate_VariableSetType",
      "Label": "Variable Set Type",
      "HelpText": "If the output variable is being saved to a project or [library variable set](https://octopus.com/docs/deploying-applications/variables/library-variable-sets)",
      "DefaultValue": "project",
      "DisplaySettings": {
        "Octopus.ControlType": "Select",
        "Octopus.SelectOptions": "project|Project Variable\nlibrary|Library Variable Set"
      }
    },
    {
      "Id": "f511bbd7-1b75-430f-be48-d3b0787d839e",
      "Name": "StepTemplate_VariableSetName",
      "Label": "Variable Set Name",
      "HelpText": "Name of the project or [library](https://octopus.com/docs/deploying-applications/variables/library-variable-sets) variable set where the output variable should be saved",
      "DefaultValue": "#{Octopus.Project.Name}",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "50a10535-b36b-4972-a9a0-ff9bb42451c7",
      "Name": "StepTemplate_TargetName",
      "Label": "Target Variable Name",
      "HelpText": "The name to use when saving the variable.\n\n_Note: The original variable name the default value._",
      "DefaultValue": "#{StepTemplate_VariableName}",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "204ad3d5-804a-43cd-90ad-8bb359800c78",
      "Name": "StepTemplate_IsSensitive",
      "Label": "Is Sensitive?",
      "HelpText": "If the variable should be marked as [sensitive](https://octopus.com/docs/deploying-applications/variables/sensitive-variables) and the value masked from logs",
      "DefaultValue": "False",
      "DisplaySettings": {
        "Octopus.ControlType": "Checkbox"
      }
    },
    {
      "Id": "fdbcad6b-ff48-4f4f-8cda-afc404fb7f10",
      "Name": "StepTemplate_EnvironmentScope",
      "Label": "Environment Scope",
      "HelpText": "The environment scope to use when creating or updating the variable.\n\n_This can be a list delimited by the Scope Delimiter (, by default)_",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "c4df3719-1f46-4d10-a3b7-52227f78815f",
      "Name": "StepTemplate_RoleScope",
      "Label": "Role Scope",
      "HelpText": "The role scope to use when creating or updating the variable.\n\n_This can be a list delimited by the Scope Delimiter (, by default)_",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "1782abd1-ed68-4cbb-85b2-efd2baaa1eae",
      "Name": "StepTemplate_MachineScope",
      "Label": "Target Scope",
      "HelpText": "The target scope to use when creating or updating the variable.\n\n_This can be a list delimited by the Scope Delimiter (, by default)_",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "85f14553-6425-4298-ba77-bdf31f852d0b",
      "Name": "StepTemplate_ActionScope",
      "Label": "Action Scope",
      "HelpText": "The action scope to use when creating or updating the variable.\n\n_This can be a list delimited by the Scope Delimiter (, by default)_",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "e4152038-fc42-4ed6-8ad5-4aa94d76a5d1",
      "Name": "StepTemplate_ChannelScope",
      "Label": "Channel Scope",
      "HelpText": "The channel scope to use when creating or updating the variable.\n\n_This can be a list delimited by the Scope Delimiter (, by default)_",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "a5bcd57b-d0bb-4b1c-be1b-56314e0b9684",
      "Name": "StepTemplate_TenantTagScope",
      "Label": "Tenant Tag Scope",
      "HelpText": "The tenant tag scope to use when creating or updating the variable. The value should be in the format TagSetName/TagValue.\n\n_This can be a list delimited by the Scope Delimiter (, by default)_",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "7de754f7-d8b6-4b6e-a7d1-6306cf8f7ab8",
      "Name": "StepTemplate_ScopeDelimiter",
      "Label": "Scope Delimiter",
      "HelpText": "The delimiter used by scope lists.",
      "DefaultValue": ",",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.RunOnServer": "true",
    "Octopus.Action.Script.ScriptBody": "$ErrorActionPreference = 'Stop'\n\n$StepTemplate_BaseUrl = $OctopusParameters['#{if Octopus.Web.ServerUri}Octopus.Web.ServerUri#{else}Octopus.Web.BaseUrl#{/if}'].Trim('/')\nif ([string]::IsNullOrWhiteSpace($StepTemplate_ApiKey)) {\n    throw \"The step parameter 'API Key' was not found. This step requires an API Key to function, please provide one and try again.\"\n}\n\nfunction Invoke-OctopusApi {\n    param(\n        [Parameter(Position = 0, Mandatory)]$Uri,\n        [ValidateSet(\"Get\", \"Put\")]$Method = 'Get',\n        $Body\n    )\n    $requestParameters = @{\n        Uri = ('{0}/{1}' -f $StepTemplate_BaseUrl, $Uri.TrimStart('/'))\n        Method = $Method\n        Headers = @{ \"X-Octopus-ApiKey\" = $StepTemplate_ApiKey }\n        UseBasicParsing = $true\n    }\n    if ($null -ne $Body) { $requestParameters.Add('Body', ($Body | ConvertTo-Json -Depth 10)) }\n    Write-Verbose \"$($Method.ToUpperInvariant()) $($requestParameters.Uri)\"   \n    Invoke-WebRequest @requestParameters | % Content | ConvertFrom-Json | Write-Output\n}\n\nfunction Test-SpacesApi {\n\tWrite-Verbose \"Checking API compatibility\";\n\t$rootDocument = Invoke-OctopusApi 'api/';\n    if($rootDocument.Links -ne $null -and $rootDocument.Links.Spaces -ne $null) {\n    \tWrite-Verbose \"Spaces API found\"\n    \treturn $true;\n    }\n    Write-Verbose \"Pre-spaces API found\"\n    return $false;\n}\n\nif(Test-SpacesApi) {\n\t$spaceId = $OctopusParameters['Octopus.Space.Id'];\n    if([string]::IsNullOrWhiteSpace($spaceId)) {\n        throw \"This step needs to be run in a context that provides a value for the 'Octopus.Space.Id' system variable. In this case, we received a blank value, which isn't expected - please reach out to our support team at https://help.octopus.com if you encounter this error.\";\n    }\n\t$baseApiUrl = \"api/$spaceId\" ;\n} else {\n\t$baseApiUrl = \"api\" ;\n}\n\nfunction Check-Scope {\n\tparam(\n    \t[Parameter(Position = 0, Mandatory)]\n        [string]$ScopeName,\n        [Parameter(Position = 1, Mandatory)]\n        [AllowEmptyCollection()]\n        [array]$ScopeValues,\n        [Parameter(Position = 2)]\n        [array]$ExistingScopeValue,\n        [Parameter(Position = 3)]\n        [string]$LookingForScopeValue\n    )\n    \n    if ($LookingForScopeValue) {\n     \t\n    \tWrite-Host \"Checking $ScopeName Scope\"\n        \n        $scopes = Create-Scope $ScopeName $ScopeValues $LookingForScopeValue\n        \n        if (-not ($ExistingScopeValue -and (Compare-Object $ExistingScopeValue $scopes) -eq $null)) {\n        \tWrite-Host \"$ScopeName scope does not match\"\n        \treturn $false\n        }\n        Write-Host \"$ScopeName scope matches\"\n    } else {\n    \tif ($ExistingScopeValue) {\n        \tWrite-Host \"$ScopeName scope does not match\"\n        \treturn $false\n        }\n    }\n    \n    return $true\n}\n\nfunction Create-Scope {\n\tparam(\n    \t[Parameter(Position = 0, Mandatory)]\n        [string]$ScopeName,\n        [Parameter(Position = 1, Mandatory)]\n        [array]$ScopeValues,\n        [Parameter(Position = 2)]\n        [string]$ScopeValue\n    )\n    \n    $scopes = @()\n    \n    foreach ($scope in $ScopeValue.Split($StepTemplate_ScopeDelimiter)) {\n    \tif ($ScopeName -eq \"TenantTag\") {\n    \t\t$value = $ScopeValues | Where { $_.Id -eq $scope } | Select -First 1\n    \t}\n        else {\n    \t\t$value = $ScopeValues | Where { $_.Name -eq $scope } | Select -First 1\n    \t}\n    \t$scopes += $value.Id\n    }\n    \n    return $scopes\n}\n\n$outputVariableKey = \"Octopus.Action[${StepTemplate_DeploymentStep}].Output.${StepTemplate_VariableName}\"\nif (!$OctopusParameters.ContainsKey($outputVariableKey)) {\n    throw \"Variable '$StepTemplate_VariableName' has not been output from '$StepTemplate_DeploymentStep'\"\n}\n$isSensitive = [System.Convert]::ToBoolean($StepTemplate_IsSensitive)\n$variableType = if ($isSensitive) { \"Sensitive\" } else { \"String\" }\n\n$variableValue = $OctopusParameters[$outputVariableKey]\nWrite-Host \"Name: $StepTemplate_VariableName\"\nWrite-Host \"Type: $variableType\"\nWrite-Host \"Value: $(if ($isSensitive) { \"********\" } else { $variableValue })\"\nWrite-Host ' '\n\nWrite-Host \"Retrieving $StepTemplate_VariableSetType variable set...\"\nif ($StepTemplate_VariableSetType -eq 'project') {\n    $variableSet = Invoke-OctopusApi \"$baseApiUrl/projects/all\" | ? Name -eq $StepTemplate_VariableSetName | % { Invoke-OctopusApi $_.Links.Variables }\n}\nif ($StepTemplate_VariableSetType -eq 'library') {\n    $variableSet = Invoke-OctopusApi \"$baseApiUrl/libraryvariablesets/all?ContentType=Variables\" | ? Name -eq $StepTemplate_VariableSetName | % { Invoke-OctopusApi $_.Links.Variables }\n}\nif ($null -eq $variableSet) {\n    throw \"Unable to find $StepTemplate_VariableSetType variable set '$StepTemplate_VariableSetName'\"\n}\n\n$variableExists = $false\n\n$variableSet.Variables | ? Name -eq $StepTemplate_TargetName | % {\n\tif (-not (Check-Scope 'Environment' $variableSet.ScopeValues.Environments $_.Scope.Environment $StepTemplate_EnvironmentScope)) {\n    \treturn\n    }\n\n\tif (-not (Check-Scope 'Machine' $variableSet.ScopeValues.Machines $_.Scope.Machine $StepTemplate_MachineScope)) {\n    \treturn\n    }\n    \n    if (-not (Check-Scope 'Role' $variableSet.ScopeValues.Roles $_.Scope.Role $StepTemplate_RoleScope)) {\n    \treturn\n    }\n    \n    if (-not (Check-Scope 'Action' $variableSet.ScopeValues.Actions $_.Scope.Action $StepTemplate_ActionScope)) {\n    \treturn\n    }\n    \n    if (-not (Check-Scope 'Channel' $variableSet.ScopeValues.Channels $_.Scope.Channel $StepTemplate_ChannelScope)) {\n    \treturn\n    }\n    \n    if (-not (Check-Scope 'TenantTag' $variableSet.ScopeValues.TenantTags $_.Scope.TenantTag $StepTemplate_TenantTagScope)) {\n    \treturn\n    }\n\n    Write-Host \"Updating existing variable...\"\n    Write-Host \"Existing value:\"\n\tWrite-Host \"$(if ($isSensitive) { \"********\" } else { $_.Value })\"\n    $_.Value = $variableValue\n    $_.Type = $variableType\n    $_.IsSensitive = $isSensitive\n    $variableExists = $true\n}\n\nif (!$variableExists) {\n    Write-Host \"Creating new variable...\"\n    \n    $variable = @{\n        Name = $StepTemplate_TargetName\n        Value = $variableValue\n        Type = $variableType\n        IsSensitive = $isSensitive\n        Scope = @{}\n    }\n    \n    if ($StepTemplate_EnvironmentScope) {\n    \t$variable.Scope['Environment'] = (Create-Scope 'Environment' $variableSet.ScopeValues.Environments $StepTemplate_EnvironmentScope)\n    }\n    if ($StepTemplate_RoleScope) {\n    \t$variable.Scope['Role'] = (Create-Scope 'Role' $variableSet.ScopeValues.Roles $StepTemplate_RoleScope)\n    }\n    if ($StepTemplate_MachineScope) {\n    \t$variable.Scope['Machine'] = (Create-Scope 'Machine' $variableSet.ScopeValues.Machines $StepTemplate_MachineScope)\n    }\n    if ($StepTemplate_ActionScope) {\n    \t$variable.Scope['Action'] = (Create-Scope 'Action' $variableSet.ScopeValues.Actions $StepTemplate_ActionScope)\n    }\n    if ($StepTemplate_ChannelScope) {\n    \t$variable.Scope['Channel'] = (Create-Scope 'Channel' $variableSet.ScopeValues.Channels $StepTemplate_ChannelScope)\n    }\n    if ($StepTemplate_TenantTagScope) {\n        $variable.Scope['TenantTag'] = (Create-Scope 'TenantTag' $variableSet.ScopeValues.TenantTags $StepTemplate_TenantTagScope)\n    }\n    \n    $variableSet.Variables += $variable\n}\n\nWrite-Host \"Saving updated variable set...\"\nInvoke-OctopusApi $variableSet.Links.Self -Method Put -Body $variableSet | Out-Null\n"
  },
  "Category": "Octopus",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/save-octopus-output-variable-with-scoping.json",
  "Website": "/step-templates/75a8f263-b690-4a55-84b7-9165ac9b6196",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAC1QTFRFT6Tl////L5Pg8vj9Y67omsvwPJrisdfzfbzs5fL7y+T32Ov5isLucLXqvt31CJPHWwAABMJJREFUeNrs3deW4jAMAFDF3U75/89dlp0ZhiU4blJEjvQ8hYubLJsA00UCBCIQgQhEIAIRiEAEIhCBCEQgAhGIQAQiEIEIhD8kJm+t+QprfdKfB9HbYpx6CWfspj8HMi+gMgHL/AmQA8W3JTKH+ALFvzCeL0RbpyoCPE9IJeNOSQwh5Z3qd6yRGWQ2qi2cZQWxqj1WzQYSjeoJmJlAklOd4VlArOqPhQEkqBERToeMcfRJBkC0Uep8CfBpjz4JsHJ0zF3dkEWNje0kiB/sUC6eApndaIiCMyAa1PiwJ0AWhRGJHJJQHG2dC7h1rNbO1QOxSA7lNCkkKrQIpJCAB1GREILYIC1NAiwbpKFJgGWDNExcwGstfExcZBCHC6nOglshHtmhViLIig1RNBCN7qjtW8C0Z1UvJcC1Z9XmwMBzzvobmgAyEzgq91dtEEsBsQSQQAFZCSBAATEEEApHZbrVBIkkEIUPSVeB+KtALA0kXQUSrwKZBCIQBnk8Y4i5CsReBeKvkqLM+BCSDWJlrZFvGk9SRTHshkgjZCGAaArIxm3H3grhVzFlW2msfl1ca79UJ1bofYvsDHHlNdTZnlh5MghuPd5NdBDUNZHyCkfktIh03XzALGRPlBDPac7qgWjHZzWcmF5zmmkhidMQ6boKiDXcDTUEaylZqCGJ0Vjvu/fLJtHqhSANEvqb2OYqkOUqEHuVMbJcZdZCGiPhKhC4yjqiIjEE7XThMp8fAWII3mY3kUIQD+AMKQTzPiBhgQ63HlT/KSvgtoi0dq5mCPah1UIE0eh3sT0NhOByvKeAkFzi8PgQomumFhsyOxpIzZN4gLOj5plVwNpR0b2AuePWKBEHQu24pSsJA+LVCeHHQxZ1SiyDIdqok8IOhSSnTottHEQTdyt4ettAj4KkzA4dMikk2Dht2S5ptm1vswnPDxn0YyDZ5oDM3iToo2T5voWaYe+Q+vdjH80QyAzZhCgcDtLMI1Tmtz9w++XHgziHQHJJu/OZ3bs9Xn8gQ72NcP3dKqEfkp10F51xhoIi2I91R+LurXV/5q7pH+wx061CzO16oSQleMyr8fXvwMA0Pro8432DPD/ySx8XrHfSuDAM8n6UhnjQabaiXf5Bq/lREHvEeNtn1rJ08+C/uXkQZHeguxAPC3UvtcJYUogLzZX5hhZZvS6onG5lxXtzWGaygwb79vT/IXhdlNibwlKYOR6T8xjI7W8n+xV7T+GH4tMzWwR+lZhRkJYSsC0thpmCYqyngOz3rN2FLBZ2wZflBCggUHF0Vnp88JKienzIXLSEZCZqU7IKr/gQW9yx3pzV7Y9kvWZWTRRIqDmTtRUnU7b2lLcTYmoqHqnmiO1poER0SPkAeZMAZxaJx0Y3TCdAclsIqDz03ALcyxfTCZBsthoGXWmigGyVhWPLFJJfuuKQWycoEFdXbH4dJJoJxNR1eD/kshz6yn48cF8yW8sFoitflB1w6Q8n+/15Za7oA17/pYNmYgP5fmWm8L1NOHPWgK8kuFew1/JXtOA0yJCv7ah7X8ObUuT5kObU30+fDZm8+zqP+HTIpK0xQ796b5Kv2hSIQAQiEIEIRCACEYhABCIQgQhEIAIRiEAEIpBf8UeAAQAEjtYmlDTcCgAAAABJRU5ErkJggg==",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Thursday, October 21, 2021