这与我问的here非常相似,只有两个文件,反向搜索。
考虑到下面显示的两个文件(test1.csv
和test2.csv
),我需要将LoginNumber
中的test1.csv
与来自UserIDNumber
的{{1}}进行匹配}。
找到匹配项后,请抓取test2.csv
中ReportExpirationDate
栏中的值。
如果test2.csv
的值中没有任何内容,则该值应从ReportExpirationDate
更改为$null
。
最后,在匹配User Not Found
和test1.csv
时,我们需要在ExpirationDateFromReport
列附加LoginNumber
。
注意:我在这个问题的列之间添加了空格,因此更容易查看和查看我在说什么。这些文件没有空格......
UserIDNumber
:
test1.csv
"SamAccountName","LoginNumber","Type,"ContractorDomain","ExpirationDateFromAd"
"jd12395" ,"0031482" ,"H" ,"P12345" ,"2016/06/26 13:59"
"jwet" ,"" ,"P" ,"A52671" ,""
"x023js" ,"0418230" ,"P" ,"" ,""
"1b3gas5" ,"2167312" ,"H" ,"425126" ,"2015/11/17 12:00"
"632g1fsa" ,"0052142" ,"L" ,"" ,""
"x023js" ,"0720182" ,"P" ,"" ,"2016/06/26 13:59"
"4126hs" ,"" ,"H" ,"" ,"2016/08/12 4:32"
"axv" ,"2058194" ,"A" ,"L21514" ,"2016/03/11 7:31"
:
test2.csv
test1.csv的最终结果应如下所示:
UserIDNumber,AccountDescriptionDetails,ReportExpirationDate
0052142 ,Company CEO ,
0418230 ,Software Developer ,20161124
0031482 ,Software Developer ,
2167312 ,Project Manager ,20151117
0720182 ,QA Tester ,20160128
1840237 ,Accountant Administrator ,
0000418 ,Program Manager ,20160309
"SamAccountName","LoginNumber","Type,"ContractorDomain","ExpirationDateFromAd","ExpirationDateFromReport"
"jd12395" ,"0031482" ,"H" ,"P12345" ,"2016/06/26 13:59" ,""
"jwet" ,"" ,"P" ,"A52671" ,"" ,""
"x023js" ,"0418230" ,"P" ,"" ,"" ,"20161124"
"1b3gas5" ,"2167312" ,"H" ,"425126" ,"2015/11/17 12:00" ,"20151117"
"632g1fsa" ,"0052142" ,"L" ,"" ,"" ,""
"x023js" ,"0720182" ,"P" ,"" ,"2016/06/26 13:59" ,"20160128"
"4126hs" ,"" ,"H" ,"" ,"2016/08/12 4:32" ,""
"axv" ,"2058194" ,"A" ,"L21514" ,"2016/03/11 7:31" ,""
由于某种原因,附加列的所有值都是空白的。
答案 0 :(得分:1)
帐户说明不是唯一的,也不会存在于test1.csv
中,因此它们不会作为哈希表的关键字工作。但是,用户ID可以正常工作。
$accountStatus = @{}
Import-Csv 'C:\Temp\test2.csv' | ForEach-Object {
$accountStatus[$_.UserIDNumber] = $_.ReportExpirationDate
}
使用if
语句处理空的报告到期日期:
Import-Csv 'C:\Temp\test2.csv' | ForEach-Object {
$accountStatus[$_.UserIDNumber] = if ($_.ReportExpirationDate) {
$_.ReportExpirationDate
} else {
'No Date Found'
}
}
然后将新列添加到test1.csv
:
Import-Csv 'C:\Temp\test1.csv' |
Select-Object -Property *, @{n='ExpirationDateFromReport';e={
if ($accountStatus.ContainsKey($_.LoginNumber)) {
$accountStatus[$_.LoginNumber]
} else {
'User Match Not Found'
}
}} | Export-Csv 'C:\Temp\final.csv' -NoType