0

UPDATED CODE:

I have the following code. I wish to pass the InputTable to the nested script as a 2 dimensional string array, but I can't work out how to get this from the Get-Content (which appears to be a 1D array).

First Script:

$InputTablePath=C:\Temp\test.csv
$InputTable = Get-Content $InputTablePath
$scriptPath = 'C:\Temp\nested_script.csv

$m = Get-Content $tablePath
$rows = $m.count
$cols = ($m[0].split(",")).count

$InputTable = New-Object 'string[,]' $rows,$cols
for ($i=0;$i -lt $rows; $i++){
    for ($j=0;$j -lt $cols; $j++){
        $InputTable[$i,$j]= ($m[$i].split(","))[$j]
   }
}  


$argumentlist = "-InputTable '{0}'" -f $array

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

Nested Script:

Param(
    [string[,]]$InputTable
)

$numberOfRows = $InputTable.GetLength(0)
$numberOfColumns = $InputTable.GetLength(1)

etc....

I'm currently getting an index out of bounds error on the last line.

Many Thanks.

  • Well, [`Get-Content`](http://powershell.org/wp/2013/10/21/why-get-content-aint-yer-friend/comment-page-1/) returns an array of strings, and I don't see you converting that in any way into a 2D array, so I'm not sure what how you expect that conversion to occur. It also looks like you're missing a " on line 4. – Roman Jun 02 '14 at 15:59
  • Agreed- I suppose the question is how to go about doing that. – Rowan Atkinson Jun 02 '14 at 16:00
  • Yes- I think using split might be the way forward but I'm not sure how. – Rowan Atkinson Jun 02 '14 at 16:03
  • You might also be interested in these two articles [Use the Pipeline to Create Robust PowerShell Functions](http://blogs.technet.com/b/heyscriptingguy/archive/2011/05/10/use-the-pipeline-to-create-robust-powershell-functions.aspx), [about_Splatting](http://technet.microsoft.com/en-us/library/jj672955.aspx). There's good advice there on making your functions pipeline friendly. – Roman Jun 02 '14 at 16:09
  • So I assume you are saying that the way to do this is to split each line and loop over all the lines? I.e. Something like: Get-Content $InputTablePath | Select -Skip 1 | ForEach-Object { $InputTable = $_.split(",") } – Rowan Atkinson Jun 02 '14 at 16:28
  • No- I am not in control of the nested script and so it has to be in [String[,]] format. I have tried to split the string as you suggested (see code above) but still doesn't work. – Rowan Atkinson Jun 02 '14 at 18:05
  • That's really unfortunate about the interface of the other script. [This](http://stackoverflow.com/questions/9397137/powershell-multidimensional-arrays/9397385#9397385) is how you can create a multi-dimensional array. I think your best bet is to calculate the number of rows and columns based on the result of `Get-Content` and then just use a regular for loop to fill it. You could probably try to do something with [begin/end/process](http://huddledmasses.org/using-script-functions-in-the-powershell-pipeline-take-two/), but not sure it would buy you much clarity. – Roman Jun 02 '14 at 18:32
  • OK, I have had another go. This seems closer but the nested script seems to change the dimensions of the array. What am I doing wrong? – Rowan Atkinson Jun 02 '14 at 20:33
  • What dimensions do you start/end with? – Roman Jun 02 '14 at 21:05
  • The array I create is 7,3. The array received by the second script is a single element (1 dimensional). – Rowan Atkinson Jun 02 '14 at 21:16
  • I think you're calling the nested script wrong. You are taking the array you created, and serializing it to a string with `-f`. Then that string is getting passed in as an argument to the nested script. There are some examples [here](http://stackoverflow.com/questions/12850487/powershell-how-to-invoke-a-second-script-with-arguments-from-a-script/21298905#21298905), but I'm pretty sure what you want is `& $scriptPath --InputTable $InputTable`. – Roman Jun 03 '14 at 12:49
  • The only issue with that solution is that I actually need to pass multiple arguments- and all the others need to be serialized to strings. Actual Line is something like: $argumentlist = "-Arg1 '{0}' -Arg2 '{1}' -Arg3 {2} -scenarioIndex {3} -InputTable '{4}'" -f $arg1,$_,$arg2,$arg3,$InputTable – Rowan Atkinson Jun 03 '14 at 14:40
  • Can't you just call it as `& #scriptPath -Arg1 $arg1 -Arg2 $arg2 ... -InputTable $InputTable` ? You can turn the arguments into strings as necessary without making the whole thing a string. – Roman Jun 03 '14 at 15:12

0 Answers0