我正在使用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"
}
]
现在我有另一个包含一些重叠数据的哈希表数组。
[
{
"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"
}
]
我想合并它们而不会重复。
我尝试了一些cmdletys像select -Unique
和.Contains()
,但它不喜欢的工作,我希望它工作。
背景:我有一系列的AD组。在这个组中,用户只被允许成为一个组的成员 - 例如:jodoe可以是AD-Group-Contoso-1或AD-Group-Contoso-2的成员,但不适用于他们两个。我需要报告它们。 报告文件将由计划任务处理,并将其报告给管理员。第一个脚本每20分钟运行一次,处理来自第一个脚本的报告文件的报告计划任务每天运行一次 - 所以我不想在报告文件中有重复项。
这里是我的代码我试过到目前为止:
# 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
}
但它不会工作,我的感觉,我不能检查,如果哈希表的数组包含一个哈希表?
如果我将它们全部推入阵列并执行select -Unique
我只获取散列表数组的第一个条目。
哈希表是引用类型,和'载有()'将寻找完全相同的*身份证*的对象,而不是对象*类似*内容。你将不得不遍历集合并手动进行比较 –