1

在我的脚本中,我使用了expandproperty3 次,以获取正确的数据。在第一个扩展属性中,我还必须使用数据。但我无法将其添加到选择行。

-expandproperty价值上我必须使用computerbalance,在-expandproperty -secureemployer我必须使用employeenumber.

这是我的脚本:

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

我希望你能帮助我。

4

1 回答 1

1

您可以使用一种.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语法。

于 2020-08-10T14:57:17.807 回答