0

I have a PowerShell function called Get-LogData and when I call this function like this:

Get-LogData | Format-Table -AutoSize

it outputs information in form of a table with the following columns:

Author, Revision, Msg, Path

I would want to insert this information into a new SQL database table and if the table exist, it has to be dropped first before the insert.

Here is my function:

Function Get-LogData() {
    $FromRev = "170000"
    $ToRev = "171000"

    $Range = $FromRev + ':' + $ToRev

    $YourMother = "SVNStaticTable"
    #Create Table object
    $table = New-Object System.Data.DataTable "$YourMother"

    #Define Columns
    $col1 = New-Object System.Data.DataColumn Author,([string])
    $col2 = New-Object System.Data.DataColumn Revision,([string])
    $col3 = New-Object System.Data.DataColumn Msg,([string])
    $col4 = New-Object System.Data.DataColumn Path,([string])

    #Add the Columns
    $table.Columns.Add($col1)
    $table.Columns.Add($col2)
    $table.Columns.Add($col3)
    $table.Columns.Add($col4)

    $LogOutput = ([xml](svn log http://Servername/svn/xyz -v -r $Range --xml)).log.logentry
    foreach ($entry in $LogOutput) {
        $paths = $entry.paths.path
        foreach ($path in $paths) {
            if ($path.InnerText -like "*/static/*") {
                #Create a row
                $row = $table.NewRow()

                $row.Author = $entry.author
                $row.Revision = $entry.revision
                #$row.Msg = $entry.msg.Replace('\D+(\d+)\D+','$1') #you call a function here
                $row.Path = $path.InnerText.Replace("/xyz/", "")
                #$row.Msg = $entry.msg.Replace("\w", '') #you call a function here
                $row.Msg = $entry.msg.Substring(0, 7) #using the substring works
                #$row.Msg = $test -replace "^.*_(\d*)$test.*$", '$1'

                $table.Rows.Add($row)
            }
        }
   }

   $table
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Immortal
  • 1,133
  • 1
  • 15
  • 35
  • Which dbms are you using? You say _new_ table, does that mean newly created, or to be created? – jarlh Apr 28 '17 at 07:54
  • I am using MS SQL server and the new table has to be created – Immortal Apr 28 '17 at 07:58
  • I answered a similiar question here, I think it will help you: http://stackoverflow.com/questions/43324189/storing-results-from-powershell-to-sql-table/43324488#43324488 – Rigerta Apr 28 '17 at 08:07
  • I tried all this but didnt work for me. Do you have any suggestions please? – Immortal May 02 '17 at 08:49

0 Answers0