title: Powershell and SQL: data from sql query to an array
body: <p>The issue is with the SQL query that is used to find computers that the selected user has logged into. I execute the query, use an adapter to fill the results into a table, and then read the table objects into an array. I don't fully understand the syntax and functionality of an array in Powershell so I may be doing this part wrong. Inside the function where the table is read into the array, the contents of the array seem fine (just the computer name) but when I pass that array out of the function and assign it to a variable, the array has the following data: {#, compName}. It appears to be the number of computers found, then the names, if one computer is found the array is {1, ComputerName}.  However if multiple computers are found, 2 or more, the array is {2, ComputerNameComputerName}.</p>
  <p>Here is the function with the SQL query and the function where the user selects the computer.</p>
  <pre><code>Function GetComputerList {
      param ($u)
      #use the display name of the user to get their login name
      $queryUser = Get-ADUser -f{DisplayName -eq $u} #-Properties sAMAccountname | Select sAMAccountname
      $queryName = &quot;'&quot;
      $queryName += $queryUser.SamAccountName
      $queryName += &quot;'&quot;
      $query = &quot;SELECT SYS.Netbios_Name0 FROM v_R_System SYS WHERE User_Name0 = $queryName ORDER BY SYS.User_Name0, SYS.Netbios_Name0&quot;
  
      $connection = new-object system.data.sqlclient.sqlconnection( &quot;Data Source=SERVER;Initial Catalog=DATABASE;Integrated Security=SSPI;&quot;)
   
      $adapter = new-object system.data.sqlclient.sqldataadapter ($query, $connection)
  
      $table = new-object system.data.datatable
      
      $adapter.Fill($table)
  
      $i = 1
      foreach($object in $table) {
          &lt;#Write-Host &quot;$i. $($object.Netbios_Name0)&quot;
          $i++#&gt;
          $compArray += $object.Netbios_Name0
      }
      foreach($object in $compArray) {
          Write-Host &quot;$i. $($object)&quot;
      }
      
      return @($compArray)
  }
  
  
  Function SelectComputer {
      param ($a)
      $computerNum = Read-Host &quot;Please select a computer. (by number)&quot;
      $computer = ($a[$computerNum])
      return $computer
  }
  </code></pre>
  <p>And they are called like this:</p>
  <pre><code>$computerArray = GetComputerList -u $selectedUser
  $selectedComputer = SelectComputer -a $computerArray
  </code></pre>
  <p>I'm totally lost, any input is appreciated.</p>