Hi,
Problem: Need help with updating or scoping deployment target in exiting project variable set.
Reference Points: I have download the project variable json and using PowerShell I am converting it using “ConvertFrom-Json” method and using loops & conditions I am able to add “Environment Variable”. However, While adding “Machines” as an nested array under “Variables.Scope” property hash table, I am not having expected result. I am putting expected & in-correct json content along with power shell script.
Expected Json Content:
"Variables": [
{
"Id": "",
"Name": "FullName",
"Value": "A",
"Description": null,
"Scope": {
"Environment": [
"Environments-1",
"Environments-2",
"Environments-3",
"Environments-4"
],
"Role": [
"Demo-1"
],
"Machines": [
"Machines-61",
"Machines-41"
]
},
"IsEditable": true,
"Prompt": null,
"Type": "String",
"IsSensitive": false
},
{
"Id": "",
"Name": "LastName",
"Value": "B",
"Description": null,
"Scope": {
"Environment": [
"Environments-1"
],
"Role": [
"Demo-2"
]
},
"IsEditable": true,
"Prompt": null,
"Type": "String",
"IsSensitive": false
}
],
In-Correct Json
"Variables": [
{
"Id": "",
"Name": "FullName",
"Value": "",
"Description": null,
"Scope": [
{
"Environment": [
"Environments-1",
"Environments-2",
"Environments-3",
"Environments-4"
],
"Role": [
"Demo-2"
]
},
{
"Machines": [
"Machines-61"
]
},
{
"Machines": [
"Machines-41"
]
}
],
"IsEditable": true,
"Prompt": null,
"Type": "String",
"IsSensitive": false
},
{
"Id": "",
"Name": "LastName",
"Value": "",
"Description": null,
"Scope": {
"Environment": [
"Environments-1"
],
"Role": [
"Demo-1"
]
},
"IsEditable": true,
"Prompt": null,
"Type": "String",
"IsSensitive": false
}
],
PowerShell Code
# Global Variable
$VariableJsonFile = "D:\Scripts\variables.json"
$VariableName = "FullName"
$EnvironmentList = ("Environments-3", "Environments-4")
$MachinesList = ("Machines-61", "Machines-41")
# Get Content Of File & Construct Array As An List Type
$VariableSet = Get-Content -Path $($VariableJsonFile) | ConvertFrom-Json
foreach ($Variable in $($VariableSet.Variables)) {
########################################## WORKING ####################################################
# Adding New Environment In Scope
if ($Variable.Name -eq $($VariableName)) {
[System.Collections.Generic.List[System.Object]]$AddEnvironmentObject = $Variable.Scope.Environment
if ($($EnvironmentList) -is [array]) {
foreach ($Environment in $($EnvironmentList)) {
if ($($AddEnvironmentObject) -contains $($Environment) ) {
# Wanted to remove variable first becasue of there is chance off that variable is already holding multiple value's.
[void]$AddEnvironmentObject.Remove($($Environment))
# Add the variable post removal.
[void]$AddEnvironmentObject.Add($($Environment))
}
else {
# Add the new variable
[void]$AddEnvironmentObject.Add($($Environment))
}
}
}
else {
if ($($AddEnvironmentObject) -contains $($EnvironmentList) ) {
# Wanted to remove variable first becasue of there is chance off that variable is already holding multiple value's.
[void]$AddEnvironmentObject.Remove($($EnvironmentList))
# Add the variable post removal.
[void]$AddEnvironmentObject.Add($($EnvironmentList))
}
else {
# Add the new variable
[void]$AddEnvironmentObject.Add($($EnvironmentList))
}
}
$Variable.Scope.Environment = $AddEnvironmentObject
##################################### NOT WORKING ##############################################
# Add Target Machines In Scope
[System.Collections.Generic.List[System.Object]]$AddScopeObject = $Variable.Scope
if ($($MachinesList) -is [array]) {
foreach ($Machine in $($MachinesList)) {
# Construct PSCustomObject
$MachineObject = [PsCustomObject]@{
Machines = @($Machine)
}
$AddScopeObject.Add($MachineObject)
$Variable.Scope = $AddScopeObject
}
}
else {
# Construct PSCustomObject
$MachineObject = [PsCustomObject]@{
Machines = @($MachinesList)
}
$AddScopeObject.Add($MachineObject)
$Variable.Scope = $AddScopeObject
}
}
}
$VariableSet | ConvertTo-Json -Depth 10 | Set-Content -Path $VariableJsonFile
@jeremy.miller or @paul.calvert : Tagging You Guys For Quick Response.
Thanks
Vivek Singh