rosho
rosho

Reputation: 41

Foreach loop with import-csv

Having some issue using basic import-csv in a loop function.

  1. I have basic csv file with hostname, node headers

  2. I am assigning the file using import-csv

  3. Then I am then simply starting foreach loop when I am trying to test-path using the hostname within unc path \\$r.hostname\c$\users\something\desktop

  4. Expected result is true

  5. But the actual result is false

when I am trying the actual hostname in the test-path instead of the csv param its working just fine.

$records = Import-Csv C:\Users\someuser\Desktop\partition-mapping.csv

foreach ($r in $records)
{

    Test-Path -LiteralPath \\$r.hostname\c$\users   <<====not working 

    Test-Path -LiteralPath \\hostname01.lab.local\C$\Users\ <<====== workign fine



}

as always you guys are awesome !!

Upvotes: 2

Views: 4675

Answers (1)

JPBlanc
JPBlanc

Reputation: 72610

Just test :

 Test-Path -LiteralPath "\\$($r.hostname)\c$\users"   <<====now working ;o) 

The trouble comes from the fact that $r is directly interpreted as a var, so the final string is "\\$r collection as a string.hostname\c$\users"

Upvotes: 2

Related Questions