Import-Csv with Powershell Using a Variable Path

Import-Csv with Powershell Using a Variable Path

I am trying to import a CSV file with powershell in a database, with the path defined in a variable. But it doesn't seem to work.

$path = "C:\Users\Admin\Desktop\TestFolder\file.txt"   
Import-CSV -Header ("header1","header2","header3") -Path $path

The above doesn't work, and I am not sure why. If I print $path I do get the correct path, and if I run Import-CSV with the path directly in it, it also work fine.

Write-Host $path
C:\Users\Admin\Desktop\TestFolder\file.txt

Import-CSV -Header ("header1","header2","header3") -Path C:\Users\Admin\Desktop\TestFolder\file.txt
header1   test123
header2   test345
header3   test567

header1   test789
header2   test901
header3   test123
etc...

Using Windows Server 2012 Standard, and PowerShell 2.

Any help is appreciated,
Thanks


EDIT:
My full code looks like this, but this doesn't work. I am trying to watch a folder with PowerShell and then import it to SQL Server 2012. The folder watch works perfectly but I can't seem to get the import to work. I figured the problem is with $DataImport and thus I went back to the basics to figure out what the problem was. I tried the very basic setup of Import-CSV as mentioned above but this (in its plain version as above) doesn't work.. so I figured I am using Import-CSV wrong.

6

2 Answers

The pastebin appears to have been removed, so I can't comment on the full script. However, you might want to try the following:

  • For any text-only strings being used as parameters in your script, use single-quotes ' '.
  • For any strings containing variables which are used as parameters in your script, use double-quotes " ".

For example, the first two command lines you give in your question should be:

$path = 'C:\Users\Admin\Desktop\TestFolder\file.txt'
Import-CSV -Header ('header1','header2','header3') -Path "$path"

Please try this way:

foreach($file in dir $CodesetBulkLoadLocation -Recurse -Include @("*.csv"));
{

#$files = @{
#'ICD10' = @{ 'EDIT_FUNCTION'='UCM-REPT-TEEN-PREG-DIAG'; 'TABLE_FUNCTION'='UCM-REPT'; 'CODE_FUNCTION' = 'I0' }
#}
#foreach($file_desc in $files.GetEnumerator())
#{
$filename = $file_desc.Key
$edit_func = $file_desc.Value.EDIT_FUNCTION
$table_func = $file_desc.Value.TABLE_FUNCTION
$code_func = $file_desc.Value.CODE_FUNCTION

$filePath = "C:\jobs\AA\DEV6\UCM\${filename}.csv"
$importFile = Import-CSV $filePath
foreach($item in $importFile)
    {
   $proc1 = $item.DIAG_NBR
   $proc2 = $item.DIAG_NBR
  # $ymdeff = $item.YMDEFF
   #$ymdend = $item.YMDEND
   #$ymdtrans = $item.YMDTRANS

   Write-Verbose "$proc1, $proc2"

   $currentDate = Get-Date -Format yyyyMMdd


# *******************************************************************************
# * Insert codes and fees into the su_edit_detail.
# *******************************************************************************

'{0,-60}{1,20}' -f "Insert TEEN PREGNANCY  ICD9 AND ICD10 CODES  into the su_edit_detail ",(Get-Date -Format yyyyMMdd:hhmmss);

$myQuery = @"
SET PAGES 600;
SET LINES 4000;
SET ECHO ON;
SET serveroutput on;

WHENEVER sqlerror exit sql.sqlcode;

insert into SU_EDIT_DETAIL(EDIT_FUNCTION, TABLE_FUNCTION, CODE_FUNCTION, CODE_TYPE,CODE_BEGIN, CODE_END, EXCLUDE, INCLUDE_X, OP_NBR, TRANSCODE, VOID, YMDEFF, YMDEND, YMDTRANS)
 select '$edit_func','$table_func','$code_func','DIAG','$proc1','$proc2',' ',' ','MIS', 'C',' ',20141001, 99991231, 20131120
from dual where not exists(select * from SU_EDIT_DETAIL where (EDIT_FUNCTION = '$edit_func' and TABLE_FUNCTION = '$table_func' and CODE_BEGIN='$proc1' and CODE_END='$proc2'));
commit;               
"@
$null = Invoke-SqlPlus -MessageOnFail 'Insert into su_edit_detail failed' -Query $myQuery;

}
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.