Writing Table to SQL Database

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 15 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
aludden
Posts: 3
Last visit: Thu Aug 28, 2008 4:54 am

Writing Table to SQL Database

Post by aludden »

I am trying to write the results of the following powershell script to a SGL database. The $DBROW has the correct values in it. However, I cannot figure out how to get the data into SQL??

Code: Select all

foreach ($server in $servers)
  {
    foreach ($log in $logs)
      {
        $SysEvent = New-Object system.diagnostics.eventLog($log, $server)
        $SysByDate = $SysEvent.entries[($SysEvent.Entries.count -1)..($SysEvent.Entries.count -2000)] | Where-Object { ($_.entryType -eq "Error" -or $_.entryType -eq "Warning") -and $_.timewritten -ge $last_ran_date }
        $DBRow = $SysByDate | sort timewritten -descending | Format-Table EventID, Source, EntryType, TimeWritten, UserName, MachineName, Message -HideTableHeaders -auto
        $DBRow
        $conn = New-Object system.data.sqlclient.sqlconnection
        $conn.connectionstring = "Server=ogh-teledb;Database=Servers;Trusted_Connection=yes;"
        $conn.open()

        $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
        $SqlCmd.CommandText = "INSERT INTO Events (EventID,Source,EntryType,TimeWritten,UserName,MachineName,Message) VALUES $DBRow"
        $SqlCmd.Connection = $conn
        $SqlCmd.executenonquery()
        $conn.Close()
        

      }
  } 
User avatar
aludden
Posts: 3
Last visit: Thu Aug 28, 2008 4:54 am

Writing Table to SQL Database

Post by aludden »

I am trying to write the results of the following powershell script to a SGL database. The $DBROW has the correct values in it. However, I cannot figure out how to get the data into SQL??

Code: Select all

foreach ($server in $servers)
  {
    foreach ($log in $logs)
      {
        $SysEvent = New-Object system.diagnostics.eventLog($log, $server)
        $SysByDate = $SysEvent.entries[($SysEvent.Entries.count -1)..($SysEvent.Entries.count -2000)] | Where-Object { ($_.entryType -eq "Error" -or $_.entryType -eq "Warning") -and $_.timewritten -ge $last_ran_date }
        $DBRow = $SysByDate | sort timewritten -descending | Format-Table EventID, Source, EntryType, TimeWritten, UserName, MachineName, Message -HideTableHeaders -auto
        $DBRow
        $conn = New-Object system.data.sqlclient.sqlconnection
        $conn.connectionstring = "Server=ogh-teledb;Database=Servers;Trusted_Connection=yes;"
        $conn.open()

        $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
        $SqlCmd.CommandText = "INSERT INTO Events (EventID,Source,EntryType,TimeWritten,UserName,MachineName,Message) VALUES $DBRow"
        $SqlCmd.Connection = $conn
        $SqlCmd.executenonquery()
        $conn.Close()
        

      }
  } 
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

Writing Table to SQL Database

Post by jhicks »

I think the Format-Table cmdlet is the culprit. This cmdlet tells PowerShell how to display information to the screen. When you are using it you are losing the original objects. In this case all you want are the string values. use Select-Object instaled of format-table. Drop the -auto parameter as well because you aren't formatting data.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Writing Table to SQL Database

Post by jvierra »

You will also not be able to "auto" enumerate the values as it is not a pure uni-dimensional array. You will need to do something like this:

Code: Select all

	
        $DBRow | % {    
            $cmd = "INSERT INTO dbo.ApplicationLog (EventID) VALUES(" + $_.EventID + ")"
            $cmd
        $SqlCmd.CommandText = $cmd
         $SqlCmd.executenonquery()
           }
This topic is 15 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked