我一直在使用来自 this answer 的代码从 MS Teams 检查类(class)花名册的添加/更改:
$set = [System.Collections.Generic.HashSet[string]]::new(
[string[]] (Import-CSV -Path stundent.csv).UserPrincipalName,
[System.StringComparer]::InvariantCultureIgnoreCase
)
Import-Csv ad.csv | Where-Object { $set.Add($_.UserPrincipalName) } |
Export-Csv path\to\output.csv -NoTypeInformation
理想情况下,我希望能够检查与新文件相比是否有删除,交换导入文件位置,并检查是否有添加。如果我的文件看起来像 Source1 和 Source2(如下),删除检查将返回 Export1,添加检查将返回 Export2。 由于多个类(class)会有多个学生实例,我想在过滤器查询中包含 TeamDesc 以确保只返回该类(class)学生的特定实例。
Source1.csv
Source2.csv
Export1.csv
导出2.csv
最佳答案
尝试以下,它使用 Compare-Object
通过两个列值比较 CSV 文件,只需将感兴趣的属性(列)名称传递给 -Property
;使用 intrinsic .Where()
method 将结果输出分成两个集合,基于输入端不同的属性组合是唯一的。 :
$removed, $added = (
Compare-Object (Import-Csv Source1.csv) (Import-Csv Source2.csv) -PassThru `
-Property TeamDesc, UserPrincipalName
).Where({ $_.SideIndicator -eq '=>' }, 'Split')
$removed |
Select-Object -ExcludeProperty SideIndicator |
Export-Csv -NoTypeInformation Export1.csv
$added |
Select-Object -ExcludeProperty SideIndicator |
Export-Csv -NoTypeInformation Export2.csv
关于powershell - 我怎样才能更正 .csv 文件的协调以删除重复/空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74452725/