这看起来很基本,但我无法在任何地方找到或弄清楚 我有2个CSV文件我想创建一个匹配列的新文件。
Huge.csv
"Share","Group","Username","Name","LogonScript"
"\\SHARE\TEST","Group Test","administrator","Administrator name","(no-script)"
"\\SHARE\TEST","Group Test","user1","user name1","logon.bat"
"\\SHARE\TEST","Group Test","user2","user name2","logon.bat"
Little.csv
"Username","Computer","NetworkDrives"
administrator,PC100,M:\\share\it#N:\\share\test
user2,PC102,M:\\share\it#N:\\share\test
期望的输出:
output.csv
"Share","Group","Username","Name","LogonScript","Computer","NetworkDrives"
"\\SHARE\TEST","Group Test","administrator","Administrator name","(no-script)",PC100,M:\\share\it#N:\\share\test
"\\SHARE\TEST","Group Test","user1","user name1","logon.bat",,
"\\SHARE\TEST","Group Test","user2","user name2","logon.bat",PC102,M:\\share\it#N:\\share\test
以下是与我合作的代码:
$HugeFile = Import-Csv -Path .\Huge.csv
$LittleFile = Import-Csv -Path .\Little.csv
ForEach ($entryh in $HugeFile) {
$o | add-member NoteProperty -Name "Share" -Value ($entryh.Share)
$o | add-member NoteProperty -Name "Group" -Value ($entryh.Group)
$o | add-member NoteProperty -Name "Username" -Value ($entryh.Username)
$o | add-member NoteProperty -Name "Name" -Value ($entryh.Name)
$o | add-member NoteProperty -Name "LogonScript" -Value
($entryhu.LogonScript)
ForEach ($entryl in $LittleFile) {
If ($($entryh.Username) -eq $($entryl.Username)) {
$o | add-member NoteProperty -Name "Computer" -Value ""
($entryl.Computer)
$o | add-member NoteProperty -Name "NetworkDrives" -Value ""
($entryl.NetworkDrives)
} Else {
$o | add-member NoteProperty -Name "Computer" -Value "," -Force
$o | add-member NoteProperty -Name "NetworkDrives" -Value "," -Force
}
$o | Export-Csv -NoTypeInformation .\output.csv
}
}
}
我的代码无效。 :/
我的第二个问题是我在想是否有更好的选择,因为每个"用户名"在Huge.csv
我必须与"用户名"进行比较在Little.csv
。
也许创建哈希表可能更合理。 Concat计算机和网络创造了一个价值? 像:
key Computer+NetworkDrive
----------- ---------------------
administrator PC100,M:\\share\it#N:\\share\test
user2 PC102,M:\\share\it#N:\\share\test
非常感谢!
编辑 谢谢@ Ansgar-Wiechers
答案 0 :(得分:1)
是的,散列表可能是最好的解决方法。我这样做:
$additionalData = @{}
Import-Csv .\Little.csv | % {
$additionalData[$_.Username] = $_.Computer, $_.NetworkDrives
}
Import-Csv .\Huge.csv `
| select Share, Group, Username, Name, LogonScript, @{n='Computer';e={}},
@{n='NetworkDrives';e={}} `
| % {
if ( $additionalData.ContainsKey($_.Username) ) {
$_.Computer = $additionalData[$_.Username][0]
$_.NetworkDrives = $additionalData[$_.Username][1]
}
$_
} | Export-Csv -NoTypeInformation .\output.csv
或者,根据@ mjolinor的建议,为计算机和网络驱动器使用单独的哈希表:
$computers = @{}
$netDrives = @{}
Import-Csv .\Little.csv | % {
$computers[$_.Username] = $_.Computer
$netDrives[$_.Username] = $_.NetworkDrives
}
Import-Csv .\Huge.csv `
| select Share, Group, Username, Name, LogonScript, @{n='Computer';e={}},
@{n='NetworkDrives';e={}} `
| % {
if ( $computers.ContainsKey($_.Username) ) {
$_.Computer = $computers[$_.Username]
}
if ( $netDrives.ContainsKey($_.Username) ) {
$_.NetworkDrives = $netDrives[$_.Username]
}
$_
} | Export-Csv -NoTypeInformation .\output.csv