header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
insert formatted Object in to a table
Last Post 23 Dec 2009 01:10 PM by Chad Miller. 1 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Not Resolved
southwestUser is Offline
New Member
New Member
Posts:1
Avatar

--
23 Dec 2009 07:25 AM

    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]
    }
    Chad MillerUser is Offline
    Basic Member
    Basic Member
    Posts:160
    Avatar

    --
    23 Dec 2009 01:10 PM
    It appears you have the incorrect variable name in your foreach loop. Should be this:

    $Objlog = New-Object system.diagnostics.eventLog($Log, $Computer)

    Instead of this

    $Objlog = New-Object system.diagnostics.eventLog($Log, $Computers)


    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer