1

I am trying to write a PowerShell script to

  1. Read in a CSV file
  2. loop over each row in the CSV file.
  3. Within each loop I want to pass the CSV header and row values to another script, both in System.String type format.

In particular, I am struggling with the conversion to the string type format- what is the best way to do this.

Script 1:

$csvPath = 'C:\Temp\Scripts\trial_csv.csv'

# Get the header
$header = Get-Content $csvPath -TotalCount 1
$IntermediateOutput = "'C:\Temp\Scripts\output'"
$scriptPath = 'C:\Temp\Scripts\Temp.ps1'

# Get the data
Get-Content $csvPath |
Select -Skip 1 |
ForEach-Object {
    $argumentList = @()
    $argumentList += ("-Header", $header)
    $argumentList += ("-row", $row)
    $argumentList += ("-IntermediateOutput", $IntermediateOutput)
    $argumentList += ("-index", $i )

    Invoke-Expression "& `"$scriptPath`" $argumentList"
}

Script 2:

Receive inputs from script 1:

Param(
    [Parameter(Mandatory=$True)]
    [String]$header, # The header of CSV file
    [Parameter(Mandatory=$True)]
    [String]$row,  # The row of the file, like "1,0,2,.."
    [Parameter(Mandatory=$True)]
    [String]$IntermediateOutput,  # Intermediate directory
    [Parameter(Mandatory=$True)]
        [String]$index        # Index
    )

Do something later (todo)
$a = $header
$b = $row
$c = $IntermediateOutput
$d = $index
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    It's better to ask specific questions: show your code, explain what it does and where is something that you can't figure out. In it's current form it's to vague: object.ToString converts to System.String, but you probably already know that. – Andrew Savinykh Jun 02 '14 at 10:11
  • I'm getting an error and don't know why: Missing an argument for parameter 'header'. Specify a parameter of type 'System.String' and try again. – Rowan Atkinson Jun 02 '14 at 10:33

2 Answers2

1

You can obtain headers directly from a newly created $csv object. In the below example $header becomes an array of strings:

$header = ($csv[0].psobject.Properties | 
    where {$_.MemberType -eq "NoteProperty"  }).Name

An alternative is to use Get-Content cmdlet to read the first line and split it on the , character:

$header = (Get-Content $csvPath -TotalCount 1) -split ","

In your code, $header is equal to null, because $header = Write-Host $rows[1] does not assign a variable. It only performs an action.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Raf
  • 9,681
  • 1
  • 29
  • 41
1

If the data in your CSV file isn't quoted, you should be able to use it directly from the .csv file without need to do the Import-Csv:

$csvPath = 'C:\Temp\Scripts\trial_csv.csv'
$IntermediateOutput = "'C:\Temp\Scripts\output'"
$Index = 1

# Get the header
$header = Get-Content $csvPath -TotalCount 1

# Get the data
Get-Content $csvPath |
Select -Skip 1 |
    ForEach-Object {
        $argumentlist = "-Header '{0}' -row '{1}' -IntermediateOutput {2} -Index {3}" -f $header,$_,$IntermediateOutput,$Index

        # Execute script with argument list

        $Index++
    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mjolinor
  • 66,130
  • 7
  • 114
  • 135