title: Powershell to MySQL insert multiple returned rows into database
body: <p>I have a powershell script that I use for logon discovery purposes. The one liner is:</p>
  
  <pre><code>$events = get-eventlog -log system -computername '.' |
  where-object {(($_.EventID -like "*7001*") -or ($_.EventID -like "*7002*"))} |
  select-object replacementstrings, machinename, timegenerated  
  </code></pre>
  
  <p>This returns 'remote computer' logon details that I need to do a SID lookup in another table. The problem is I CANNOT get the MySQL insert statement to loop through results. It will insert 1 row from the above $events var. When I echo $events out in the loop It walks through the returned results just as you would assume...I looked everywhere for someone who talks about inserting multiple rows with powershell, but found (0) results in my hunt. I currently am using the .net connector for mySQL and would like to make that work if possible to avoid learning pains, but almost think it is time for something new. below is my code -minus the credential vars:</p>
  
  <pre><code># MySQL Connection string #
  function Connect-MySQL([string]$user,[string]$pass,[string]$MySQLHost, [string]$database) { 
  
      # Load MySQL .NET Connector Objects 
              [void] [system.reflection.Assembly]::LoadWithPartialName("MySql.Data") 
  
                  $connStr = "server=" + $MySQLHost + ";port=3306;uid=" + $user + ";pwd=" + $pass + ";database="+$database+";Pooling=FALSE" 
                  $conn = New-Object MySql.Data.MySqlClient.MySqlConnection($connStr) 
                  $conn.Open() 
                  return $conn 
                  } 
  
                  # Close Conn
                  function Disconnect-MySQL($conn) {
                  $conn.Close()
                  }
  
  
  # MySQL Connection Vars #
  $user = '' 
  $pass = ''
  $database = '' 
  $MySQLHost = '' 
  
  $conn = Connect-MySQL $user $pass $MySQLHost $database
  
                  function Execute-MySQLNonQuery($conn, [string]$query) { 
                  $command = $conn.CreateCommand()                  
                  $command.CommandText = $query                     
                  $RowsInserted = $command.ExecuteNonQuery()       
                  $command.Dispose()                                
  
                  if ($RowsInserted) { 
                      return $RowInserted 
                      } else { 
                      return $false 
                      } 
                      } 
  foreach($event in $events){
  $sid = $event.replacementstrings[1]
  $time = $event.timegenerated
  $id = $event.machinename
  $query = "INSERT INTO info.ewarp_testing (data1,data2,data3) VALUES ('$sid',     '$time','$id')"
  $Rows = Execute-MySQLNonQuery $conn $query
  }
  </code></pre>
  
  <p>I have asked around but have not got any answers for this issue. Certainly I cannot be the only one that needs to insert an entireoutput from powershell into a mysql db? :) Thanks!</p>