0

我正在比较 .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 文件中的所有行,每个都位于一个 中InputObject,并SideIndicator显示差异。因此,对于诸如

# 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, <=     
  

以便在变量不同时对匹配的名称进行排序。我怎样才能对它们进行排序?

4

1 回答 1

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={ <some expression> }}

于 2020-07-01T12:45:07.293 回答