5

I'm new to powershell. But learning.... I'm quite far with my script to achieve what i want. I just miss the last portion. I hope someone can The issue i have is don't get the correct result when creating an CSV file.

Ok, i have an CSV file something like this:

A           B
AAAA        111111
CCCC        222222
EEEE        333333
HHHH        444444

Now i have an array with this info:

AAAA
BBBB
CCCC
DDDD
HHHH

I compare the values in the array with the CSV file. And I want to receive the results of column B.

This is working fine. I receive this result:

111111
222222
444444

So basically this is correct. BUT.... I need to receive this:

111111

222222

444444

So when the value is not found, I want an empty cell

I hope my question is clear enough ;)

The code i have is this:

$CSV = import-csv C:\tmp\mac-vendors-export.csv -Delimiter ';'
$vendormacarray = $Vendormac
$vendorname = foreach ($UniqueVendorMac in $Vendormacarray) {
    $csv | where-object {$_.A -match $UniqueVendorMac} | select-object  -Expand B
}

I think i have to add something like -or $_.A -eq 'null'.... but cannot figure this last part out.

Hopefully someone can help me out with this question.

Thank you

1 Answer 1

3

Use a hash table to store your items in A as its keys and the items in B as the values, that way you can perform fast lookups when comparing with $vendormacarray.

An example:

$map = @{}
Import-Csv C:\tmp\mac-vendors-export.csv -Delimiter ';' | ForEach-Object {
    $map[$_.A] = $_.B
}

$vendorMac = @(
    'AAAA'
    'BBBB'
    'CCCC'
    'DDDD'
    'HHHH'
)

$vendorMac | ForEach-Object {
    [pscustomobject]@{
        MAC      = $_
        ThingInB = $map[$_]
    }
}

Then the output using the data from your question would be something like:

MAC  ThingInB
---  --------
AAAA 111111
BBBB
CCCC 222222
DDDD
HHHH 444444
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.