0

How to write a loop for the below code to retrieve all values at a time present in the file and not using it for retrieving values for each {0} {1}.. present in the file.

$data = Get-Content -Path 'C:\Users\Username\Downloads\ScheduledJobs1.json' | ConvertFrom-Json

$Status1 = $("{0:N1}" -f $data.value.CompletedJobs.State.Name)
$Asset1 = $("{0:N1}" -f $data.value.Application.Asset.Name)
$Application1 = $("{0:N1}" -f $data.value.Application.Name)

$Status2 = $("{1:N1}" -f $data.value.CompletedJobs.State.Name)
$Asset2 = $("{1:N1}" -f $data.value.Application.Asset.Name)
$Application2 = $("{1:N1}" -f $data.value.Application.Name)

$Status3 = $("{2:N1}" -f $data.value.CompletedJobs.State.Name)
$Asset3 = $("{2:N1}" -f $data.value.Application.Asset.Name)
$Application3 = $("{2:N1}" -f $data.value.Application.Name)

$Status4 = $("{3:N1}" -f $data.value.CompletedJobs.State.Name)
$Asset4 = $("{3:N1}" -f $data.value.Application.Asset.Name)
$Application4 = $("{3:N1}" -f $data.value.Application.Name)

$Status5 = $("{4:N1}" -f $data.value.CompletedJobs.State.Name)
$Asset5 = $("{4:N1}" -f $data.value.Application.Asset.Name)
$Application5 = $("{4:N1}" -f $data.value.Application.Name)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Possible duplicate of [Convert JSON to CSV using PowerShell](https://stackoverflow.com/questions/43594860/convert-json-to-csv-using-powershell) –  Jun 30 '18 at 10:40
  • What does `$data.results` return? –  Jun 30 '18 at 10:42
  • 1
    Can you give an example of what you have in `ScheduledJobs1.json`. So far it looks like only this changes from `{0:N1} to {4:N1}`. So you can for loop through 0 to 4 or the max count if you can get it. – Sid Jun 30 '18 at 10:55
  • 1
    Your question still isn't very clear. Please describe the actual result you want to achieve. What do you mean by "retrieve all values in the file"? What values? What do you intend to do with them once you have them? – Ansgar Wiechers Jun 30 '18 at 10:57

1 Answers1

0

When you ConvertFrom-Json, you get $data as a structured powershell object. The I see you are trying to refine the values of that object to your requirement. Ideally, you would want that refined values in another object to be used elsewhere.

    $data = Get-Content -Path 'C:\Users\Username\Downloads\ScheduledJobs1.json' | ConvertFrom-Json

    $Result = for ($i = 0; $i -lt $maxCount; $i++)
    {
        $Obj = [PScustomObject]@{
            Status = $("{$i`:N1}" -f $data.value.CompletedJobs.State.Name)
            Asset = $("{$i`:N1}" -f $data.value.Application.Asset.Name)
            Application = $("{$i`:N1}" -f $data.value.Application.Name)
        }
        $Obj
    }

    $Result | ft

Where $maxcount is in your case 4 or if there is more data in your Json, it is probably $data.Count. That is your responsibility to figure out.

This will create an object $obj with 3 properties every iteration and add it to $result.

So $Result will have your final consolidated object. Do this: $result | ft and see if that is how you wanted it.

Sid
  • 2,586
  • 1
  • 11
  • 22
  • Hi Robin, thanks for your response and in explaining the code but where do i see the output, for the code you mentioned i tried to run it and it gives me no errors but with no output displayed in the editor, could you please suggest how to get the output and export it to .htm file or excel file – G.vinay Kumar Jun 30 '18 at 11:32
  • I did tell you to run `$result | ft`. That is your output. – Sid Jun 30 '18 at 13:02