您可以使用一种.dotProperty
方式来使用对象的属性。您可以使用方式仅将您感兴趣的属性累积到一个对象中[PSCustomObject]@{Key1=Value1;Key2=Value2;...}
。
这是示例数据及其简化为对象列表的示例。
$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
我使用 powershell 超过 5 年,从未使用过-ExpandProperty
语法。