【问题标题】:Is there a way to get sorted PSCustomObjects after using Compare-Object on two .JSON files?在两个 .JSON 文件上使用 Compare-Object 后,有没有办法对 PSCustomObjects 进行排序?
【发布时间】:2020-07-01 12:21:51
【问题描述】:

我正在比较 .JSON 文件,使用 Compare-Object 像这样:

# Compare two files 
$BLF = (Get-Content -Path C:\Users\...\Documents\Android1.json)
$CLF = (Get-Content -Path C:\Users\...\Documents\Android2.json)

$a = Compare-Object -ReferenceObject $BLF -DifferenceObject $CLF -IncludeEqual 

那么$a 就是System.Array。它包含 .JSON 文件中的所有行,每个都在 InputObjectSideIndicator 中以显示差异。因此对于诸如

之类的差异
# From file 1  
"workProfilePasswordMinimumLength":  false,
"workProfilePasswordMinLowerCaseCharacters":  false,
"workProfilePasswordMinUpperCaseCharacters":  null,
"deviceManagementApplicabilityRuleOsEdition":  null,

# From file 2 
"workProfilePasswordMinimumLength":  true,
"workProfilePasswordMinLowerCaseCharacters":  false,
"workProfilePasswordMinUpperCaseCharacters":  null,
"deviceManagementApplicabilityRuleOsEdition":  true,

比较函数给出使用

$a.psobject.ImmediateBaseObject

输出:

InputObject                                              SideIndicator
-----------                                              -------------
    "workProfilePasswordMinLowerCaseCharacters":  false, ==           
    "workProfilePasswordMinUpperCaseCharacters":  null,  ==           
    "workProfilePasswordMinimumLength":  true,              =>           
    "deviceManagementApplicabilityRuleOsEdition":  true, =>           
    "workProfilePasswordMinimumLength":  false,             <=           
    "deviceManagementApplicabilityRuleOsEdition":  null, <=    

我的问题是:我想对这些 InputObjects 进行排序,使它们像这样:

InputObject                                              SideIndicator
-----------                                              -------------
    "workProfilePasswordMinLowerCaseCharacters":  false, ==           
    "workProfilePasswordMinUpperCaseCharacters":  null,  ==           
    "workProfilePasswordMinimumLength":  true,              =>           
    "workProfilePasswordMinimumLength":  false,             <=     
    "deviceManagementApplicabilityRuleOsEdition":  true, =>     
    "deviceManagementApplicabilityRuleOsEdition":  null, <=     
  

这样,当变量不同时,对匹配的名称进行排序。如何对它们进行排序?

【问题讨论】:

  • 最简单的解决方案通常很有效:Sort-Object

标签: powershell sorting pscustomobject


【解决方案1】:

您可以像这样将Compare-Object cmdlet 的输出通过管道传输到Sort-Object

Compare-Object -ReferenceObject $BLF -DifferenceObject $CLF -IncludeEqual |
    Sort-Object -Property InputObject

由于它按 InputProperty 对输出进行排序,因此您得到

InputObject                                         SideIndicator   
-----------                                         -------------   
"deviceManagementApplicabilityRuleOsEdition":  null <=
"deviceManagementApplicabilityRuleOsEdition":  true =>
"workProfilePasswordMinimumLength":  false          <=
"workProfilePasswordMinimumLength":  true           =>
"workProfilePasswordMinLowerCaseCharacters":  false ==
"workProfilePasswordMinUpperCaseCharacters":  null  ==

这与您发布的输出略有不同。如果您需要与发布的完全相同的输出,则可以使用自定义排序表达式。

Sort-Object -Property @{Expression={ &lt;some expression&gt; }}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 2022-08-02
    • 2019-06-27
    • 2017-03-12
    相关资源
    最近更新 更多