Hi, ive been using the following script UpdateVariableInProject.ps1 to add and update a variable in my project, which behaves perfectly while I have 1 value and 1 Environment Scope.
The problem i’m having is when I give the existing variable name a new value and a new Environment Scope, the previous value is updated but the Scope stays as is.
Ideally when I pass in a new variable value and new scope id like this to appended to the existing variable, and if the variable value and scope already exist then the value is updated.
I’ve tried playing around with the script to filter on ?{$_.name -eq $VariableToModify} and also removing the if ($variable) { block, but this just keeps adding extra rows of the same value and scope into the variable.
Sorry my powershell is woeful so any help would be appreciated
Can you please take a look at this script and see if it fits your use case?
I have written these functions and intend to do some testing on them and commit them to my script above, but if you want to tinker with them if they will help your use case, please let me know how they go in testing.
Function Remove-Variable {
param(
$VariableSet,
$VariableName
)
$tempVars = @()
foreach ($variable in $VariableSet.Variables){
if ($variable.Name -ne $VariableName){
$tempVars += $variable
}
}
$variableset.Variables = $tempVars
}
Function Add-Scope {
param(
$VariableSet,
$VariableName,
$VariableEnvScope,
$VariableRoleScope,
$IsSensitive = $false
)
$tempVars = @()
#Create the variable object
$obj = New-Object -Type PSObject -Property @{
'Name' = $($VariableName)
'Value' = $($VariableValue)
'Type' = 'String'
'IsSensitive' = $IsSensitive
'Scope' = @{
'Environment' =@()
'Role' =@()
}
}
if ($VariableRoleScope){
#If the role passed was an array, add them all
if ($VariableRoleScope -is [array]){
foreach ($role in $VariableRoleScope){
$obj.scope.role += $VariableRoleScope
}
}
#If it's not an array, just add the one.
else{
$obj.scope.role += $VariableRoleScope
}
}
#Check to see if an environment was passed, add it to the object if it was
if ($VariableEnvScope){
#If the environment passed was an array, add them all
if ($VariableEnvScope -is [array]){
foreach ($environment in $variableenvscope){
$environmentObj = $VariableSet.ScopeValues.Environments | Where { $_.Name -eq $environment } | Select -First 1
$obj.scope.Environment += $environmentObj.Id
}
}
#If it's not an array, just add the one.
else{
$environmentObj = $VariableSet.ScopeValues.Environments | Where { $_.Name -eq $VariableEnvScope } | Select -First 1
$obj.scope.environment += $environmentObj.Id
}
}
foreach ($variable in $VariableSet.Variables){
if ($variable.Name -ne $VariableName){
$tempVars += $variable
}
if ($variable.Name -eq $VariableName -and ($variable.Scope.Environment -or $variable.Scope.Role)){
$tempVars += $variable
}
if ($variable.Name -eq $VariableName -and !$variable.Scope.Environment -and !$variable.Scope.Role){
$obj.Value = $variable.Value
$tempVars += $obj
}
}
$variableset.Variables = $tempVars
#Find the variable that needs edited based on name and scoping
}
As always, any scripts we give you aren’t guaranteed to work in all scenarios so please read them thoroughly and test them in a test environment.
Please let me know if that helps or if we need to possibly go another direction with this one.