Pass String Variable to Function Argument as Comma Separated List
<p>I've got a PowerShell function that accepts multiple values for a single argument via a comma separated list, like so:</p>

<pre><code>test-function -Targets 127.0.0.1, 8.8.8.8, fake.fqdn.com -FileName "c:\results.csv"
</code></pre>

<p>Here is the param section:</p>

<pre><code>function test-function {
    param(
        [string[]]$Targets
        [string]$FileName
    )
}
</code></pre>

<p>I'm trying to put an array in as multiple Target arguments, so I've put them into a single variable like so:</p>

<pre><code>foreach ($target in $arrayTargets) { $temp = $temp + ", $Target" }
$Targets = $temp -replace "^, ", ""
</code></pre>

<p>This would result in the required "127.0.0.1, 8.8.8.8, fake.fqdn.com" as a string in the variable <code>$Targets</code>.</p>

<p>When I try to execute the function referencing the <code>$Targets</code> variable, the function interprets <code>$Targets</code> as if it's a single entry, not multiple.  This is what I've tried with the same results:</p>

<pre><code>test-function -Targets $Targets -FileName "c:\results.csv"
test-function -Targets "$Targets" -FileName "c:\results.csv"
</code></pre>

<p>I've also tried an ArgList several different ways:</p>

<pre><code>$ArgList = "-Targets $Targets -FileName `"c:\results.csv`""
test-function $ArgList
</code></pre>

<p>Instead of taking each individual array entry as an argument, it takes the entire string as the argument.</p>

<p>How can I get this function to accept an array as multiple targets?</p>