title: Calling PowerShell Start-Job cmdlet with multiple parameters and foreach loop is soooo close
body: <p>I have several thousand computers to backup the Security event logs to a server's share. The environment is very dynamic, hence the need to automate this.</p>
  
  <p>I've been working on a script which creates a hash wherein each key is a sequence and the value for each key is N number of computers. I'm passing the keys and values to another script which will run n number of jobs to backup the logs; n will depend on how many machines I can include in each job and still process the backups efficiently.</p>
  
  <p>Script 1 has this block:</p>
  
  <pre><code>foreach ($key in ($arrayAll.Keys | Sort-Object)) {
      Job-EvtLog.ps1 $key @($data)
  }
  </code></pre>
  
  <p>Script 2 has:</p>
  
  <pre><code>Param(
      [Parameter[Mandatory=$true, ValueFromPipeline=$true)]
      [string[]] $Key,
  
      [Parameter[Mandatory=$true, ValueFromPipeline=$true)]
      [Array[]] $Computers
  )
  
  function job_process($key) {
      #...stuff...including casting the @($Computers) to the array: $MyComputers
      $jobCommand = [ScriptBlock]::Create("foreach(`$d in $MyComputers) {Add-Content -Path $somewhere -Value `$d}")
      Start-Job -Name $key $jobCommand -Args $somewhere $MyComputers
  }
  </code></pre>
  
  <p>I'm testing this by trying to write the array of computers to a file, hence the <code>Add-Content</code>.</p>
  
  <p>I'm obviously doing something wrong creating the scriptblock. <code>Get-Job | %{$_.Command}</code> displays:</p>
  
  <pre><code>foreach ($d in my_List_of_Hostnames) {Add-Content -Path myCorrectpath -Value $d}
  </code></pre>
  
  <p>Nothing is being written to myCorrectPath.</p>
  
  <p>If I write:</p>
  
  <pre><code>... -Value `$d}")
  </code></pre>
  
  <p>toward the end of the scriptblock, the display shows the last hostname from the list of hostnames.</p>
  
  <p>How do write the scriptblock such that it will iterate thru the array of hostnames in a scriptblock to process each element in one job?  </p>