1
votes

Trying to read a file that having comma delimited rows. I want to read each row and split into array and make a global variables with the array values.

File content:

server1,component1,service1
server2,BatchProcessor2,service2
server3,BatchProcessor3,service3

I want to create a global variable as a directory path like below while reading each row.

$deployment.path = D:\server1\component1\service1
$deployment.path = D:\server2\component2\service2
$deployment.path = D:\server3\component3\service3

But I get null values.

Code:

$fileIn  = 'D:\Test\build.properties.dev'
(Import-Csv -Path $filein -Delimiter ',' -Header 'ServerName','ComponentName','ServiceName' | ForEach-Object {
    $deployment.path = D:\$_.ServerName\$_.ComponentName\$_.ServiceName
    if (!(Test-Path $deployment.path)) {
        New-Item -ItemType Directory -Force -Path $deployment.path
    }
})
1
where does $Deployment come from? you are assigning a value t. a prop of that $Var, but i don't see where it comes from ...Lee_Dailey
also, why is your pipeline enclosed in parens? that seems unneeded ...Lee_Dailey
also also ... where you do get null values?Lee_Dailey
Did this work for you?m0lochwalker
@Avinash Hi there, will you kindly accept my answer below as it seemed to solve your problem? Thank you!m0lochwalker

1 Answers

2
votes

Do it like this:

$csv = Import-Csv -Path 'D:\Test\build.properties.dev'

$csv | ForEach-Object {
    $path = "D:\$($_.servername)\$($_.componentname)\$($_.servicename)"
    if (!(Test-Path $path)) {
        New-Item -Itemtype Directory -Path $path -Confirm:$false -Force
    }
}

If your .csv does not have headers, add them. If you can’t, do it programmatically. You could use Tee-Object, for example. Same principle above applies.