we are trying to update a sensitive variable by using below PowerShell code but its not updating in the existing variable value rather it is adding one more empty plane value the variable please help us with the script to update the sensitive variable in the library variable set.
Add-Type -Path ‘C:\Program Files\Octopus\Newtonsoft.Json.dll’
Add-Type -Path ‘C:\Program Files\Octopus\Octopus.Client.dll’
$apikey = ‘xxxxxxxxxxx’ # Get this from your profile
$OctopusURL = ‘http://localhost:80/’ # Your server address
$header = @{ “X-Octopus-ApiKey” = $apikey }
$endpoint = new-object Octopus.Client.OctopusServerEndpoint $OctopusURL,$apikey
$repository = new-object Octopus.Client.OctopusRepository $endpoint
$varaadtenet = “AAD Tenant”
$varenvname = “cbp02loaddev”
$varnewpwd = $password
$varname = “aadTenantPassword”
$variableSetName = $varaadtenet + " " + “-” + " " + $varenvname
$endpoint = new-object Octopus.Client.OctopusServerEndpoint $OctopusURL,$apikey
$repository = new-object Octopus.Client.OctopusRepository $endpoint
$header = @{ “X-Octopus-ApiKey” = $apikey }
$VariableSets = (Invoke-WebRequest “$OctopusURL/api/libraryvariablesets?contentType=Variables” -UseBasicParsing -Headers $header).content | ConvertFrom-Json | select -ExpandProperty Items | ?{$_.name -eq $variableSetName} #echo $VariableSets
$libraryVariableSetId = $VariableSets.Id
$libraryVariableSet = $repository.LibraryVariableSets.Get($libraryVariableSetId);
$variables = $repository.VariableSets.Get($libraryVariableSet.VariableSetId);
Hi team,
it looks like provided script is to update project varibale set but we are looking for updating the sensitive varibale in library varibale set could you please help us.
Thanks
It looks like you’re running the script from within an Octopus script step?
For the initial troubleshooting it would be easier to run the script directly in Powershell to see the error being output more clearly.
Also, are all of the required parameters filled?
$OctopusUrl = "http://localhost"
$VarName = "Variable name" #Name of the variable to modify
$newvalue = "test35" # New value to set to the variable
$libraryVariableSetName = "Test"
$APIKey = "API-GZTUFPTUJYQH3CPH3ZYQ5G0W"
I want to update the sensitive variable value in library variable set which i am unable to do it.
I am using the below code but which is adding one more empty value (with type plain text) to variable but it is not updating the existing value which is of sensitive type.
In order to update a variable instead of adding a new one then you need to retrieve the details of that specific variable before performing the update.
e.g.
$libraryvariablesetId = $libraryvariableset.VariableSetId
$variableset = $repository.VariableSets.Get($libraryvariablesetId) #retrieve variable set
$variable = $variableset.Variables | ?{$_.name -eq $VariableToModify} #retrieve specific variable by name
If you’re not able to factor that into your current script then an alternative would be to use this octopus.client script as a starting point and just remove the parts you don’t need.
You’ll need to update the parameters at the bottom of the script.
cls
function UpdateVarInVariableSet {
Param (
[Parameter(Mandatory=$true)][string] $UserApiKey,
[Parameter(Mandatory=$true)][string] $OctopusUrl,
[Parameter(Mandatory=$true)][string] $VariableSetName,
[Parameter(Mandatory=$true)][string] $VariableToModify,
[Parameter(Mandatory=$true)][string] $VariableValue,
[Parameter()][string] $EnvironmentScope,
[Parameter()][string] $RoleScope,
[Parameter()][string] $MachineScope,
[Parameter()][string] $ActionScope
)
Process {
Set-Location "C:\Program Files\Octopus Deploy\Tentacle"
Add-Type -Path 'Newtonsoft.Json.dll'
Add-Type -Path 'Octopus.Client.dll'
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $OctopusUrl,$UserApiKey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
$libraryvariableset = $repository.libraryVariableSets.FindByName($libraryVariableSetName)
$libraryvariablesetId = $libraryvariableset.VariableSetId
$variableset = $repository.VariableSets.Get($libraryvariablesetId)
$variable = $variableset.Variables | ?{$_.name -eq $VariableToModify}
if ($variable) {
$variable.Value = $VariableValue
$Variable.IsSensitive = $false
}
else {
$variable = new-object Octopus.Client.Model.VariableResource
$variable.Name = $VariableToModify
$variable.Value = $VariableValue
$variableset.Variables.Add($variable)
}
try {
if ($EnvironmentScope){
$variable.Scope.Add([Octopus.Client.Model.ScopeField]::Environment, (New-Object Octopus.Client.Model.ScopeValue($EnvironmentScope)))
}
if ($RoleScope){
$variable.Scope.Add([Octopus.Client.Model.ScopeField]::Role, (New-Object Octopus.Client.Model.ScopeValue($RoleScope)))
}
if ($MachineScope){
$variable.Scope.Add([Octopus.Client.Model.ScopeField]::Machine, (New-Object Octopus.Client.Model.ScopeValue($MachineScope)))
}
if ($ActionScope){
$variable.Scope.Add([Octopus.Client.Model.ScopeField]::Action, (New-Object Octopus.Client.Model.ScopeValue($ActionScope)))
}
}
catch {}
if ($repository.VariableSets.Modify($variableset)) {Write-Host "variabe $VariableToModify in $ProjectName successfully modified"}
}
}
$OctopusUrl = "http://localhost"
$VarName = "Variable name" #Name of the variable to modify
$newvalue = "test35" # New value to set to the variable
$libraryVariableSetName = "Test"
$APIKey = "API-GZTUFPTUJYQH3CPH3ZYQ5G0W"
#Example
UpdateVarInVariableSet -UserApiKey $APIKey -OctopusUrl $OctopusUrl -VariableSetName $libraryVariableSetName -VariableToModify $VarName -VariableValue $newvalue
As an additional question, what version of Octopus Server are you using?
I’ve just tested your original script and it is successfully updating the library set variable without creating a new one for me.
Yes it is working for me now. Thanks for your help!!
But how can we see what has been updated in the value ? Is there any way we can see the value of a variable which is of sensitive type ?
Please help because script ran successfully but my deployment is failing hence i want to see what has been updated in the value.
Due to the sensitive nature of these variables, there isn’t any “easy” way of viewing them.
The only one I can think of would be to use the variable in a variable substitution on a file.
The only problem here is that variables are snapshotted when a release is created, so if you update a sensitive variable during a deployment it won’t actually take effect until the next release.
If this is the scenario that you require then output variables may be the solution for you. These are temporary variables that only last for the deployment.