我有2个.csv文件。
示例:
1.csv:
Users
userA
userB
2.csv:
groupid,user,random
1,userA,randomtext1
2,userA,randomtest2
3,userA,randomtext3
3,userB,randomtext1
5,userB,randomtext2
6,userB,randomtext2
我想获取1.csv中一列的值,并将其与2.csv中的列进行核对。
基本上我想要的输出是:
UserA is a member of 1
UserA is a member of 2
UserA is a member of 3
UserB is a member of 3
UserB is a member of 4
UserB is a member of 5
UserB is a member of 6
我知道我可以使用Import-Csv
,但我无法绕过如何做到这一点?
我试图将1.csv放入一个数组中,但是如何针对2.csv中的每一行检查数组以检查用户是否在给定的行上,然后使用groupip?
$user = @()
$users = Import-Csv 1.csv | Foreach {$user += $_.Users}
我尝试了很多不同的东西,但我无法让它发挥作用。
任何提示赞赏。
/拉斯穆斯
答案 0 :(得分:1)
将您感兴趣的用户名读入数组,使用Where-Object
过滤与这些用户名匹配的行:
# Read user names into string array
$TargetUsers = Import-Csv .\1.csv |Select-Object -ExpandProperty Users
# Now import and filter the CSV with the relationships
Import-Csv .\2.csv |Where-Object {$TargetUsers -contain $_.user} |ForEach-Object {
# Output the string with the information you need
"$($_.user) is a member of $($_.groupid)"
} |Out-File .\output.txt