0

I have an imported CSV ($csv) with multiple headers, one of which is "Target Server". In the CSV this column has values device1, device2 etc.

I also have a hashtable ($hash) with name/value pairs of name(device1) - value(fqdn1) etc.

So I would like to replace the "device1" in CSV with the correct value from the hashtable, like:

foreach($row in $csv)
  if($hash[$_.Name]){
    $row."Target Server"= $hash[$_.Value]
  }

Am I getting warm?

1 Answer 1

2

Use the ContainsKey() method to see if the hashtable contains an entry with a specific name/key:

foreach($row in $csv) {
    if($hash.ContainsKey($row.'Target Server')) {
        $row.'Target Server' = $hash[$row.'Target Server']
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Mathias, I get error: Exception calling "ContainsKey" with "1" argument(s): "Key cannot be null. Parameter name: key" How do I reference the specific csv column by name?
Actually it worked, I was passing $csv from memory during runtime from a previous operation. When I exported the previous result to a .csv and imported it assigning it to $csv it worked.

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.