question

GOVINDUSREEHARSHA-5153 avatar image
0 Votes"
GOVINDUSREEHARSHA-5153 asked RichMatheisen-8856 answered

Powershell vlookup

File_1.csv

Number LUNID
1 111
2 104
3 67
4 156

File_2.csv

Number UniqueID
1 50003AC0100000000000001700003B64
2 50003AC0100000000000001800003B64
3 50003AC0100000000000001B00003B64
4 50003AC0100000000000001C00003B64


Expected Result

Number LUNID UniqueID
1 111 50003AC0100000000000001700003B64
2 104 50003AC0100000000000001800003B64
3 67 50003AC0100000000000001B00003B64
4 156 50003AC0100000000000001C00003B64




windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

Try this:

 $hash1 = @{}
 Import-Csv c:\junk\file_1.csv -Delimiter " " |
     ForEach-Object{
         $hash1[$_.Number] = @($_.LUNID,0)
     }
 Import-Csv c:\junk\file_2.csv  -Delimiter " " |
     ForEach-Object{
         if ($hash1.ContainsKey($_.Number)){
             ($hash1.($_.Number)[1])++
             [PSCustomObject]@{
                 Number = $_.Number
                 LUNID = $hash1.($_.Number)[0]
                 UniqueId = $_.UniqueId
             }
         }
         else{
             [PSCustomObject]@{
                 Number = $_.Number
                 LUNID = "?"
                 UniqueID = $_.UniqueID
             }
         }
     }
 $hash1.GetEnumerator()|
     ForEach-Object{
         if ($_.Value[1] -eq 0){
             Write-Host "LUNID " ($_.Value[0]) " in 1st file is unmatched" -ForegroundColor Yellow
         }
     }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.