Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2314

Import Events in Database

$
0
0

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


Viewing all articles
Browse latest Browse all 2314

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>