检查哈希表数组是否包含一个散列表。

时间:2022-06-15 00:58:59

I'm collecting data from a JSON file with the cmdlet ConvertFrom-Json. That works so far. The JSON contains an array of hashtables.

我使用cmdlet ConvertFrom-Json从JSON文件中收集数据。到目前为止。JSON包含一个散列表数组。

[
    {
        "userSamAccountName":  "jodoe",
        "QuotaGroup":  "AD-Group-Contoso-1"
    },
    {
        "userSamAccountName":  "jodoe",
        "QuotaGroup":  "AD-Group-Contoso-2"
    },
    {
        "userSamAccountName":  "frmark",
        "QuotaGroup":  "AD-Group-Contoso-1"
    },
    {
        "userSamAccountName":  "frmark",
        "QuotaGroup":  "AD-Group-Contoso-2"
    }
]

Now I have another array of hashtables containing some overlapping data.

现在我有了另一个包含一些重叠数据的哈希表数组。

[
    {
        "userSamAccountName":  "jodoe",
        "QuotaGroup":  "AD-Group-Contoso-1"
    },
    {
        "userSamAccountName":  "jodoe",
        "QuotaGroup":  "AD-Group-Contoso-2"
    },
    {
        "userSamAccountName":  "niwellenstein",
        "QuotaGroup":  "AD-Group-Contoso-1"
    },
    {
        "userSamAccountName":  "niwellenstein",
        "QuotaGroup":  "AD-Group-Contoso-2"
    }
]

I'd like to combine them without getting duplicates.

我想合并它们而不需要复制。

I tried some cmdletys like select -Unique and .Contains() but it don't work like I want it to work.

我尝试了一些cmdletys,比如select -Unique和. contains(),但是它不像我希望的那样工作。

Background: I got a range of AD groups. In this groups users are only allowed to be member of one group - e.g.: jodoe can be member of AD-Group-Contoso-1 or AD-Group-Contoso-2 but not to both of them. And I need to report them. The report-file will be processed by a scheduled task which reports them to the admins. The first script runs every 20 minutes and the report-scheduled-task which processes the report-file from the first script runs once a day - so I don't want to have duplicates in my report file.

背景:我有一系列的广告组。在这个组中,用户只能是一个组的成员——例如:jodoe可以是AD-Group-Contoso-1或AD-Group-Contoso-2的成员,但不能同时属于两个组。我需要报告他们。报告文件将由一个调度的任务处理,该任务向管理员报告。第一个脚本每20分钟运行一次,从第一个脚本处理报告文件的报告日程任务每天运行一次——所以我不希望报告文件中有重复的内容。

Here is my code what I tried so far:

这是我迄今为止尝试的代码:

# Group users in list to check if user is in 2 or more Groups #
$reportDuplicates = $adUserlist |
                    group -Property userSamAccountName |
                    ? { $_.Count -gt 1 } 
# only select the group of the duplicates #
# $reportDuplicates.Group corresponds to the Json File # 
$reportDuplicates = $reportDuplicates.Group

$reportPath = "C:\\temp\\reports\\" 
$reportDuplicatesPath = $reportPath + "reportADDuplicates.json"

# Check if file already exists #
if (Test-Path $reportDuplicatesPath) {
    # load existing reports #
    $existingDuplicatesReport = Get-Content $reportDuplicatesPath |
                                ConvertFrom-Json
    $reportDuplicates.ForEach({
        if ($existingDuplicatesReport.Contains($_)) {
            $existingDuplicatesReport.Add($_)
        }
    })
    # convert to JSON and save in file #
    $existingDuplicatesReport | ConvertTo-Json | Out-File $reportDuplicatesPath
} else {
    # convert to JSON and save in file #
    $reportDuplicates | ConvertTo-Json | Out-File $reportDuplicatesPath
}

But it won't work, I got the feeling, that I can't check if an array of hashtables contains a hashtable?

但是,我有一种感觉,我不能检查哈希表数组是否包含哈希表,这是行不通的。

If I push them all into the array and do a select -Unique I only get the first entry of the array of hashtables.

如果我将它们全部推入数组并执行select -Unique,我只能得到hashtables数组的第一个条目。

1 个解决方案

#1


4  

Tell Select-Object on which keys of the nested hashtables you want to establish uniqueness:

告诉Select-Object您希望建立惟一性的嵌套散列表的键:

$arr1 = $json1 | ConvertFrom-Json
$arr2 = $json2 | ConvertFrom-Json

$arr1 + $arr2 | Select-Object -Unique 'userSamAccountName', 'QuotaGroup'

#1


4  

Tell Select-Object on which keys of the nested hashtables you want to establish uniqueness:

告诉Select-Object您希望建立惟一性的嵌套散列表的键:

$arr1 = $json1 | ConvertFrom-Json
$arr2 = $json2 | ConvertFrom-Json

$arr1 + $arr2 | Select-Object -Unique 'userSamAccountName', 'QuotaGroup'