1

In my script I use an expandproperty 3 times, this to get to the correct data. In the first expand property I also have to use data. But I cannot add this to the select line.

In -expandproperty value I have to use computerbalance and in -expandproperty -secureemployer I have to use employeenumber.

This is my script:

Invoke-RestMethod -Uri $Url_budgetid -Method Get -Headers $header_process | Select-Object -ExpandProperty value | Select employee | Select-Object -ExpandProperty Employee | Select-Object -ExpandProperty SecureEmployee 

I hope you can help me.

Arnab
  • 4,216
  • 2
  • 28
  • 50
Martijnsp1991
  • 15
  • 1
  • 5

1 Answers1

1

You can use a .dotProperty way to use properties of objects. You can accumulate only properties you're interested in into one object using [PSCustomObject]@{Key1=Value1;Key2=Value2;...} way.

Here's example data and example of it's simplification to a list of objects.

$data = @'
[    {
        computer: {balance : 180, name: "Computer1"},
        employee: {displayName: "Juan Carlos Rodrigez", employeeNumber: "Emp123", telephone: "+1234567890"}
    },
    {
        computer: {balance : 220, name: "Computer2"},
        employee: {displayName: "Natalia Marisa Oreiro", employeeNumber: "Emp456", telephone: "+987654321" }
    },
    {
        computer: {balance : 90, name: "Computer3"},
        employee: {displayName: "Don Pedro", employeeNumber: "Emp000", telephone: "+77777777777" }
    }
]
'@ | ConvertFrom-Json
    
$warningLine = 200
$dataSimplified = $data | 
    ForEach-Object { return [PSCustomObject]@{ 
        ComputerBalance = [Int]::Parse($_.computer.balance);  # we can use values 'As Is'
        EmployeeNumber = $_.employee.employeeNumber.ToUpper(); # We can translate or modify falues
        FirstName = $_.employee.displayName.Split(' ')[0]; # We can use them partially
        ShouldWarnLowBalance = $_.computer.balance -le $warningLine } # We can create new properties based on what we whant
        } |
    Where-Object { $_.ShouldWarnLowBalance -eq $true } # We can right then filter output depending on our generated values
        
#> $dataSimplified
#
#ComputerBalance EmployeeNumber FirstName ShouldWarnLowBalance
#--------------- -------------- --------- --------------------
#            180 EMP123         Juan                      True
#             90 EMP000         Don                       True

I'm using powershell for over 5 years and never used -ExpandProperty syntax.

filimonic
  • 3,988
  • 2
  • 19
  • 26
  • filimonic, thanx for your answer, iam almost there but the results i get are not the same as yours. My results are like this: EmployeeNumber computerBalance -------------- --------------- {A20200626-00, A20200626-01, 100003, 100015...} {$null, $null, 1695,4000, 323,0400...} The data is next to eachother instead below :( – Martijnsp1991 Aug 11 '20 at 07:10
  • It's better is you can export data BEFORE you work with it? and code and upload somewhere. To export data, use Export-CliXML – filimonic Aug 11 '20 at 07:20
  • filimonic , i can export it, but i want the file to be in a csv format like: ComputerBalance; EmployeeNumber 180; EMP123 90; EMP000 is that posible? – Martijnsp1991 Aug 11 '20 at 08:13
  • Of course it's possible, but I need to see source data to help you making data in fformat you want – filimonic Aug 11 '20 at 09:00
  • This is the whole input: – Martijnsp1991 Aug 11 '20 at 12:30
  • is this a general approach towards parsing `JSON`? – Nicholas Saunders Nov 18 '20 at 05:03
  • `ConvertFrom-Json` is general approach for quite small JSON data. – filimonic Nov 23 '20 at 09:10