1

I build a C# project which called an API and received a response as shown below and I looped through to determine how many times the System.State "New Value" was Resolved. We want to move this to run on a schedule via a pipeline so I am attempting to translate this into PowerShell.

I'm unfamiliar with how to create objects and loop through them in PowerShell as I'v shown in my C# project. Any guidance would be appreciated.

I've tried Invoke-RestMethod with | ConvertFrom-JSON:

$HistoryQueryResult = Invoke-RestMethod -Method Get -ContentType application/json -Uri $QueryUri -Headers $header | ConvertFrom-Json 

..but, I'm unsure after this point.

   var RootObjects = JsonConvert.DeserializeObject<WorkItemHistoryJson.Root>(result2);
            foreach (var item in RootObjects.value)
            {
                if (item.fields != null)
                {
                    if (item.fields.SystemState != null)
                    {                                          
                        if (item.fields.SystemState.newValue == StateToCheck)
                        {
                            TES_WorkItem.CountOfTestingRounds += 1;
                        }
                    }
                }                
            }

{
    "count": 6,
    "value": [
        {
            "id": 1,
            "workItemId": 226,
            "rev": 1,
            "revisedBy": {
                "id": "0e7735b9-cf6a-6468-82c1-81e6b092addd",
                "descriptor": "aad.MGU3NzM1YjktY2Y2YS03NDY4LTgyYzEtODFlNmIwOTJhZGRk"
            },
            "revisedDate": "2020-05-22T09:49:00.81Z",
            "fields": {
                "System.Id": {
                    "newValue": 226
                },
                "System.Reason": {
                    "newValue": "New"
                },
                "System.CreatedDate": {
                    "newValue": "2020-05-22T07:59:22.64Z"
                },
                "System.ChangedDate": {
                    "newValue": "2020-05-22T07:59:22.64Z"
                }
            }
        },
        {
            "id": 2,
            "workItemId": 226,
            "rev": 2,
            "revisedDate": "2020-05-22T09:49:04.45Z",
            "fields": {
                "System.Rev": {
                    "oldValue": 1,
                    "newValue": 2
                },        
                "System.State":{
                    "oldValue":"New",
                    "newValue":"Resolved"
                 }                     
            }
        }
    ]
}
2
  • 2
    Invoke-RestMethod should be already returning an object for you, there shouldn't be a need for ConvertFrom-Json. Isn't that the case? Commented Feb 24, 2022 at 16:07
  • @SantiagoSquarzon Yup I think you are correct. It's how I can iterate through each element and check the value of the system state "New Value". I understand I can do a foreach but I can't seem to get it to go throguh each element of the JSON. Commented Feb 24, 2022 at 16:13

1 Answer 1

2

I believe, assuming the JSON follows this exact structure, you could do it just like this, which returns 1 when testing with the one posted in question:

$json = Invoke-RestMethod -Method Get -ContentType application/json -Uri $QueryUri -Headers $header
(@($json.value.fields.'system.state'.newValue) -eq 'Resolved').Count

-eq can be used for filtering if the LHS of the comparison is an array, hence the use of the array sub-expression @(..).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.