I have looked through the past questions regarding updating a JSON file and can't seem to find exactly what i am looking for.
Lets start with the script itself.
foreach ($Ip in $Ips)
{
Code here that runs against IPs.
foreach ($Cluster in $getCluster) {
Get-Cluster -name $Cluster |
select @{Name='attName';Expression {$IP}},
Name,
att1,
att2,
att3,
att4,
att5,
att6,
att7,
att8 |
Convertto-Json | out-file $ConfigFile.Results.ClusterOutput
}
}
Now the first time this script runs the first time it hits the cluster section. It creates a .txt file where it houses the Json Data. The file looks like this:
First Run of ForEach $IP
[
{
"attName": "1.1.1.1",
"Name": "testData",
"att1": value,
"att2": value,
"att3": value,
"att4": value,
"att5": value,
"att6": null,
"att7": null,
"att8": null
},
{
"attName": "2.2.2.2",
"Name": "testData",
"att1": value,
"att2": value,
"att3": value,
"att4": value,
"att5": value,
"att6": null,
"att7": null,
"att8": null
}
]
Now the when the first foreach gets done with the second foreach i need it to add those attributes for the next IP address that comes up. I would like it to look like the following.
Second Run of ForEach $IP
[
{
"attName": "1.1.1.1",
"Name": "testData",
"att1": value,
"att2": value,
"att3": value,
"att4": value,
"att5": value,
"att6": null,
"att7": null,
"att8": null
},
{
"attName": "2.2.2.2",
"Name": "testData",
"att1": value,
"att2": value,
"att3": value,
"att4": value,
"att5": value,
"att6": null,
"att7": null,
"att8": null
},
{
This is the second runs data.
"attName": "3.3.3.3",
"Name": "testData",
"att1": value,
"att2": value,
"att3": value,
"att4": value,
"att5": value,
"att6": null,
"att7": null,
"att8": null
}
]
I feel like i need an if statement checking to see if the file is created - if it isn't then run it the first time - if it hasn't been written yet then add the "attributes to the existing file".
Here is what I have found and looking at right now. So far I haven't had success in making this work.
EDIT: I should have clarified a little bit better that was my bad. The information below were things i have been testing and trying to get to work. Thats not to say they work - its just things I have found as reference.
<# if (Test-Path $ConfigFile.Results.ClusterOutput){
(New-Object PSObject |
Add-Member -PassThru NoteProperty Name $getClusterConfig.Name |
Add-Member -PassThru NoteProperty Age 10 |
Add-Member -PassThru NoteProperty Amount 10.1
) | Convertto-Json
#pathToJson = "F:\Path\To\JSON\file.json"
# $a = Get-Content $ConfigFile.Results.ClusterOutput | ConvertFrom-Json
#$a.policies.'Framework.DataContext'.connectionString = "Server=ServerName;Database=DateBaseName;Integrated Security=sspi2;"
#$a | ConvertTo-Json | set-content $pathToJson
}
else{
Get-Cluster -name $Cluster |
select @{Name='attName';Expression {$IP}},
Name,
att1,
att2,
att3,
att4,
att5,
att6,
att7,
att8 |
Convertto-Json | out-file $ConfigFile.Results.ClusterOutput
}
My question is how can i get the if than else statement to add the next json child and keep it formatted like it should be. Any help would be greatly appreciated.