I am trying to write a PowerShell script to
- Read in a CSV file
- loop over each row in the CSV file.
- 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