如何将PowerShell输出插入SQL数据库?

时间:2017-04-28 07:51:38

标签: sql sql-server function powershell

我有一个名为Get-LogData的PowerShell函数,当我这样调用这个函数时:

Get-LogData | Format-Table -AutoSize

它以表格的形式输出信息,其中包含以下列:

Author, Revision, Msg, Path

我希望将 插入 此信息放入新的SQL数据库表中,如果该表存在,则必须在插入之前先将其删除。

这是我的功能:

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
}

0 个答案:

没有答案