I've been trying to get my formatted get-eventlog query to pass each event that gets returned as a row in a table. While I am successful each time I pass an individual string to the table, I receive the following error when attempting to pass in my string variable:
Exception calling "Fill" with "1" argument(s): "Incorrect syntax near 'Index'. If this is intended as a part of a table hint, A WITH ke yword and parenthesis are now required. See SQL Server Books Online for proper syntax. Unclosed quotation mark after the character string ' ca...
Below is my script thus far (ideally I'd like to add some addtional string manipulation which will parse each returned property to its own column).
param ( [string] $filename )
# PowerShell script to list the Application eventlogs on another computer
$Log = "Application"
#$Computers = get-content $filename
#use the above get-content to read from a list of machines, use below to query a single machine
$Computers = "workstationName"
$ID = "1002"
$Type = "Error"
foreach ($Computer in $Computers)
{
write-host "Details of the Server :" $Computers
write-host "-----------------------------------"
write-host "Index, DateTime, EntryType, Source, InstanceID, Message"
$Objlog = New-Object system.diagnostics.eventLog($Log, $Computers)
$result = $Objlog.entries | select -last 5 | out-string
#The above fails, the below will successfully pass the string and add a row to the db table
#$result = "hello1"
#write-host $result
#}
## add each entry in to SQL #$testVar = "'talk"+" blabla'"
#$postToDB = $testVar
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=serverName\InstanceName;Database=EventLogDB;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "insert into Events values ($result)"
$SqlCmd.Connection = $SqlConnection $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$DataSet.Tables[0]
}