none
Import Events in Database RRS feed

  • Frage

  • Hallo,

    ich würde gerne meine Events gerne in eine MSSQL Datenbank importieren. Dazu hätte ich dieses Script.

    Alles Server laufen auf windows server 2008 R2

    SQL: SQL 2012 Std.

    $events = Get-WinEvent ForwardedEvents |  Select-Object ID, LevelDisplayName, LogName, MachineName, Message, ProviderName, RecordID, TaskDisplayName, TimeCreated  
    
    $connectionString = "Data Source=labdb001.lab.local;Integrated Security=true;Initial Catalog=EventCollections;"
    $bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $connectionString
    $bulkCopy.DestinationTableName = "GeneralEvents"
    $dt = New-Object "System.Data.DataTable"
    
    # build the datatable
    $cols = $events | select -first 1 | get-member -MemberType NoteProperty | select -Expand Name
    foreach ($col in $cols)  {$null = $dt.Columns.Add($col)}
      
    foreach ($event in $events)
      {
         $row = $dt.NewRow()
         foreach ($col in $cols) { $row.Item($col) = $event.$col }
         $dt.Rows.Add($row)
      }
      
    # Write to the database!
    $bulkCopy.WriteToServer($dt)

    leider bekomme ich beim ausführen diesen Fehler.

    Exception calling "WriteToServer" with "1" argument(s): "Received an invalid column length from the bcp client for coli
    d 3."
    At C:\Importevents.ps1:20 char:24
    + $bulkCopy.WriteToServer <<<< ($dt)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

    Kann mir vielleicht jemand helfen?

    LG

    Dienstag, 8. April 2014 08:18

Antworten

Alle Antworten