我想比較兩個 csv 檔案中的值并回傳 source2 中與 source1 中的條目不匹配的任何條目,同時忽略任何重復的條目。以下是我的嘗試,但它不會回傳所有條目。讓這個腳本做我需要的最好的方法是什么?
$AD = Import-CSV -Path "source1.csv"
$Student = Import-CSV -Path "source2.csv"
$AD |OuterJoin-Object $Student -on UserPrincipalName | Export-CSV -Path "Path.csv"
Source1 和 Source2 csv 具有“名稱”、“用戶主體名稱”和“團隊描述”列。我想用這些來匹配條目。理想的輸入/輸出應該是這樣的:
Source1.csv
| TeamDesc | UserPrincipalName | Name |
|:---------|:--------------------|:------------|
| Team 1 | student1@domain.com | john smith |
| Team 1 | student2@domain.com | nancy drew |
| Team 2 | student3@domain.com | harvey dent |
Source2.csv
| TeamDesc | UserPrincipalName | Name |
|:---------|:--------------------|:------------|
| Team 1 | student1@domain.com | john smith |
| Team 2 | student3@domain.com | harvey dent |
Export.csv
| TeamDesc | UserPrincipalName | Name |
|:---------|:--------------------|:-----------|
| Team 1 | student2@domain.com | nancy drew |
uj5u.com熱心網友回復:
不確定它是如何完成的OuterJoin-Object。我假設你想這樣做:
$AD = Import-Csv source1.csv | Group-Object UserPrincipalName -AsHashTable -AsString
$Student = Import-CSV -Path source2.csv
@(
$AD.Values.ForEach{ $_ }
$Student.Where{ -not $AD.ContainsKey($_.UserPrincipalName) }
) | Export-CSV -Path Path.csv -NoTypeInformation
如果要排除來自 的可能重復項source2.csv,可以使用:
@(
$AD.Values.ForEach{ $_ }
$Student.Where{ -not $AD.ContainsKey($_.UserPrincipalName) }.
ForEach{ $AD[$_.UserPrincipalName] = $null }
) | Export-CSV -Path Path.csv -NoTypeInformation
現在查看您現在已編輯的答案,該答案提供了預期的輸出,看來您真正想要的是:
$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
uj5u.com熱心網友回復:
$Source1 = ConvertFrom-Csv @'
TeamDesc, UserPrincipalName, Name
"Team 1", [email protected], "john smith"
"Team 1", [email protected], "nancy drew"
"Team 2", [email protected], "harvey dent"
'@
$Source1 = ConvertFrom-Csv @'
TeamDesc, UserPrincipalName, Name
"Team 1", [email protected], "john smith"
"Team 2", [email protected], "harvey dent"
'@
$Source1 |OuterJoin $Source2 -On Name,TeamDesc
TeamDesc UserPrincipalName Name
-------- ----------------- ----
Team 1 {[email protected], $null} nancy drew
請注意,UserPrincipalName 有兩個值,一個來自左表,一個(空)來自右表。這將向您顯示資訊丟失的一面。您還可以使用-Name引數進一步區分屬性,例如:
$Source1 |OuterJoin $Source2 -On Name,TeamDesc -Name Source1,Source2
TeamDesc Source1UserPrincipalName Source2UserPrincipalName Name
-------- ------------------------ ------------------------ ----
Team 1 [email protected] nancy drew
或者只是左邊的屬性:
$Source1 |OuterJoin $Source2 -On Name,TeamDesc -Property 'Left.*'
TeamDesc UserPrincipalName Name
-------- ----------------- ----
Team 1 [email protected] nancy drew
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/515557.html
標籤:电源外壳CSV
