title: Powershell scope: variable array within loop not populating when adding to second array
body: <p>In the following Powershell example, how can I get <code>$server</code> to populate within line #5? I'm trying to save the results of a <code>Get-EventLog</code> query to an array for each <code>$server</code> loop iteration. </p>
  
  <p>Example:</p>
  
  <pre><code>$serversArray = @("one", "two", "three")
  ForEach ($server in $serversArray) {
    echo $server "Variable array iteration populates here correctly"
    $eventArray = @()
    $eventArray += Get-EventLog -computerName $server #But not here, where I need it
    $eventArray  #Yet it populates from calling the second array correctly
  }
  </code></pre>
  
  <ol>
  <li>I've tried assigning the scope of the $server variable to global or script. </li>
  <li>I've researched other similar issues, but none that I could find had this circumstance</li>
  <li>I've tried different combinations of piping, quotes, back ticks, etc.</li>
  </ol>
  
  <p>As always, thanks in advance.</p>
  
  <p><em>Edit</em> Ok, ISE was caching some of the variables (or something strange), as the above example began working after I restarted ISE. Anyways, the issue is with the <code>$servers</code> array input, which in the full script is from a MySQL query. If I statically assign the array with server names (like in the example above) the script works. If I use the input from the MySQL query, <code>Get-EventLog</code> fails with <code>The network path was not found</code>. So even though the server values look correct (and <em>something</em> could be expanding), it could be a text encoding issue, etc. Sorry to waste time, but discussing it has helped narrow it down. Here's the pertinent part of the actual script:</p>
  
  <pre><code>#Open SQL Connection
  [System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
  $connectionString = "server=dbserver;uid=odbc;pwd=password;database=uptime;"
  $connection = New-Object MySql.Data.MySqlClient.MySqlConnection
  $connection.ConnectionString = $connectionString
  $connection.Open()
  #Get server names from db
  $sqlGetServers = "select server_nm from servers limit 3;"
  $command = New-Object MySql.Data.MySqlClient.MySqlCommand($sqlGetServers, $connection)
  $dataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($command)
  $dataSet = New-Object System.Data.DataSet
  $recordCount = $dataAdapter.Fill($dataSet, "sample_data")
  $server_names = @()
  $server_names += $dataSet.Tables["sample_data"] 
  #$server_names = @("server-1","server-2")  &lt;-- this works
  #loop through array of server names
  foreach($server in $server_names) {
  $eventData = @()
  $eventData += Get-EventLog -computerName $server -LogName System -Newest 10
  foreach($event in $eventdata) {Do-Stuff}
  }
  </code></pre>