How to update values in a nested json file by Powershell - arrays

I want to update some values of below content which i believe a json file. This is the output of azure devops release definition and I need to reuse the content modifying some fields.
I can simply update values like id and name using these lines
$ReleaseDef.Name = $newReleaseName
$ReleaseDef.path = $folderQA
But I dont know how to update array fields like artifact and triggers. I can get values by calling $ReleaseDef.artifacts.sourceid
but can not set any values there , It throws errors like The property 'sourceid' cannot be found on this object. Verify that the
property exists and can be set.
Please suggest
source : userInterface
revision : 4
description :
createdBy : #{displayName=Jyotiprakash Nayak; url=https://azuredevopsdv
.ril.com/EntApps/_apis/Identities/2ca8bd5d-7797-4177-b7bb-2
6daa0d29ed9; _links=;
id=2ca8bd5d-7797-4177-b7bb-26daa0d29ed9;
uniqueName=domain\Jyotiprakash.Nayak; imageUrl=https://azuredev
opsdv.company.com/EntApps/_apis/GraphProfile/MemberAvatars/win.
Uy0xLTUtMjEtMjIwNzU5NTE2Ni03MjEyNTY2NjUtNTU2MTkwNDkyLTQ4NjY
yMw; descriptor=win.Uy0xLTUtMjEtMjIwNzU5NTE2Ni03MjEyNTY2NjU
tNTU2MTkwNDkyLTQ4NjYyMw}
createdOn : 2019-12-10T09:11:11.057Z
modifiedBy : #{displayName=Jyotiprakash Nayak; url=https://azuredevopsdv
.company.com/EntApps/_apis/Identities/2ca8bd5d-7797-4177-b7bb-2
6daa0d29ed9; _links=;
id=2ca8bd5d-7797-4177-b7bb-26daa0d29ed9;
uniqueName=Domain\Jyotiprakash.Nayak; imageUrl=https://azuredev
opsdv.comapny.com/EntApps/_apis/GraphProfile/MemberAvatars/win.
Uy0xLTUtMjEtMjIwNzU5NTE2Ni03MjEyNTY2NjUtNTU2MTkwNDkyLTQ4NjY
yMw; descriptor=win.Uy0xLTUtMjEtMjIwNzU5NTE2Ni03MjEyNTY2NjU
tNTU2MTkwNDkyLTQ4NjYyMw}
modifiedOn : 2019-12-13T09:16:13.463Z
isDeleted : False
variables :
variableGroups : {}
environments : {#{id=7; name=Stage 1; rank=1; owner=; variables=;
variableGroups=System.Object[]; preDeployApprovals=;
deployStep=; postDeployApprovals=;
deployPhases=System.Object[]; environmentOptions=;
demands=System.Object[]; conditions=System.Object[];
executionPolicy=; schedules=System.Object[];
currentRelease=; retentionPolicy=; processParameters=;
properties=; preDeploymentGates=; postDeploymentGates=;
environmentTriggers=System.Object[]; badgeUrl=https://azure
devopsdv.comapny.com/EntApps/_apis/public/Release/badge/e0b1a36
0-01a5-4eea-af57-b2a461559ac9/7/7}}
artifacts : {#{sourceId=e0b1a360-01a5-4eea-af57-b2a461559ac9:48;
type=Build; alias=_eCAM-Team-CI; definitionReference=;
isPrimary=True; isRetained=False}}
triggers : {#{artifactAlias=_eCAM-Team-CI;
triggerConditions=System.Object[];
triggerType=artifactSource}}
releaseNameFormat : Release-$(rev:r)
tags : {}
pipelineProcess : #{type=designer}
properties : #{DefinitionCreationSource=}
id : 7
name : Release-Template-1
path : \QA
projectReference :
url : https://azuredevopsdv.company.com/EntApps/e0b1a360-01a5-4eea-af
57-b2a461559ac9/_apis/Release/definitions/7
_links : #{self=; web=}

These attributes contain hash table values.
You should be able to update by specifying the key and its new value like so:
$ReleaseDef.artifacts["sourceid"] = "someValueHere"

Thanks all for your valuable helps, I finally made it work through some foreach loops.
```$artifact = $json.artifacts
foreach ($value in $artifact)
{
$value.alias = "_$newReleaseName"
$value.sourceId = "$($projectid):$($BuildID)"
}
foreach ($triggervalue in $snapshot.triggers)
{
$triggervalue.artifactAlias = "_$newReleaseName"
}```

Related

Replace values in powershell object

Using powershell to set the HA datastores for my VMware environment. I just don't know how to replace a certain value.
$Cluster = Get-Cluster $ClusterName | Get-View
$HAInfo = $Cluster.Configuration.DasConfig
Result of $HAinfo is this:
Enabled : True
VmMonitoring : vmMonitoringDisabled
HostMonitoring : enabled
VmComponentProtecting : disabled
FailoverLevel : 1
AdmissionControlPolicy : VMware.Vim.ClusterFailoverResourcesAdmissionControlPolicy
AdmissionControlEnabled : True
DefaultVmSettings : VMware.Vim.ClusterDasVmSettings
Option : {das.ignoreRedundantNetWarning}
HeartbeatDatastore : {Datastore-datastore-2367254, Datastore-datastore-1586741}
HBDatastoreCandidatePolicy : userSelectedDs
LinkedView :
Now I'm interested in the HeartbeatDatastore, which now contains:
Type Value
---- -----
Datastore datastore-2367254
Datastore datastore-1586741
I need to replace the Value with new values. I could easily do this by just writing:
$Hainfo.HeartbeatDatastore[1].value = "newvalue"
But I can't be sure whether it contains 0, 1 or 2 values. My problem is that when for example it only contains 1 row (datastore, datastore-2367254), I don't know how I should add a new row with new values.
Not sure if this extra info is needed:
$Hainfo.HeartbeatDatastore | get-member
TypeName: VMware.Vim.ManagedObjectReference
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Type Property string Type {get;set;}
Value Property string Value {get;set;}
Please assist.
Solved it like this:
$HAInfo.HeartbeatDatastore.Clear()
$dsObj1 = New-Object VMWare.Vim.ManagedObjectReference
$dsObj1 = here I fill it with the values I converted
$HAInfo.HeartbeatDatastore += $dsObj1
$dsObj2 = New-Object VMWare.Vim.ManagedObjectReference
$dsObj2 = here I fill it with the values I converted
$HAInfo.HeartbeatDatastore += $dsObj2

How can I convert collection laravel to array?

My code like this :
$test = $this->vendorRepository->getVendor($request->get('q'));
If I dd($test), the result is collection like this :
I want to convert it to array
I try like this :
dd($test->toArray());
The result like this :
value of id changed to 0
Why did it happen? How can I solve this problem?
This might be because your ID field is a string but laravel is expecting it to be an auto-incrementing integer.
Try adding this to the top of your model:
public $incrementing = false;

Swift 3 - Get value from array of dictionaries

Trying to get the value and the key of a dictionary that's stored in an array to populate two UILabels.
My array looks as follows:
var fromLocations = [["marco" : "polo"],["jekyll" : "hide"],["freddy" : "jason"]]
I'm getting the index by a collection view's indexPath. Currently trying the following:
cell.locationName.text = fromLocations[indexPath.item].values[1]
I've tried a bunch of other ways but I can't nail it.
Is this what you are trying to do?
var fromLocations = [["marco" : "polo1"],["marco" : "polo2"],["marco" : "polo3"]]
let marco = fromLocations[0]["marco"]
print("marco = \(marco)")
This prints "polo1". Basically you are accessing the first item of the array which is a dictionary. You then access that dictionary the way you would normally access a dictionary.
So a break down would be:
let fromLocations = [["marco" : "polo"],["marco" : "polo"],["marco" : "polo"]]
let theDictionary = fromLocations[0]
let value = theDictionary["marco"]
To just get the values as an array with using the key (which is strange), turn the values into an array.
var fromLocations = [["marco" : "polo1"],["marco" : "polo2"],["marco" : "polo3"]]
let marco = Array(fromLocations[0].values)[0]
print("marco = \(marco)")
This will print "polo1"

Firebase.util.NormalizedCollection merge multiple paths

I would like to know if it is possible with Firebase.util.NormalizedCollection to merge 3 paths or more who depends on each other. On firebase doc it said that the best practice with our datas is to have flattened data as possible, so I have that :
question :
uniqueIdqestion1 :
myquestion : "ma question",
answers : {
uniqueIdanswer1 : true,
uniqueIdanswer54 : true
}
answer :
uniqueIdanswer1 :
my_answer : "my answer",
people : {
uniqueIdpeople1 : true
}
people
uniqueIdpeople1 :
name : "pedra"
With Firebase.util.NormalizedCollection, I would like to list all the answer for my question and have an array like that :
key_answer : uniqueIdanswer,
my_answer : "ma response",
name : "pedra"
I tried to have something like that with this code :
var ref = firebase.database().ref();
var nc = new firebase.util.NormalizedCollection(
ref.child('questions/' + $state.params.chatId + '/answers'),
[ref.child('answers'), 'widget2'], // B path
... ??? // C path
)
.select('widget2.my_answer', ???).ref();
$scope.reponses = $firebaseArray(nc);
But I can't still understand how to merge the B and C path. Maybe it's because C doesn't refer directly to the main path, I don't understand because something is missing for me in my mind ... Please can you help me with that ?

Sorting an arraycollection with a numeric value coming from 2 diffrent fields

Hi guys I need to sort an arraycollection. the problem is that my array is combined from two diffrent arrays, in one the age field is called "AGE", and in the other the age is called "MYAGE".
Now I need to sort the combined array according to age but I got two fields with diffrent names. I can go through the array and change all "MYAGE" to "AGE" but i was wondering if theres maybe a way to sort the array in its current status?
Thanks ahead
Say you have an ArrayCollection called myCollection. I hope the following will solve your request for that:
private function compareItems(a:Object, b:Object, fields:Array = null):int
{
var firstValue:Number = "AGE" in a ? a["AGE"] : a["MYAGE"];
var secondValue:Number = "AGE" in b ? b["AGE"] : b["MYAGE"];
if (firstValue == secondValue)
return 0;
return firstValue > secondValue ? 1 : -1;
}
…
var sort:ISort = new Sort();
sort.compareFunction = compareItems;
myCollection.sort = sort;
myCollection.refresh();

Resources