【问题标题】:Powershell script to append the resultset from multiple source to a csv将结果集从多个源附加到 csv 的 Powershell 脚本
【发布时间】:2014-07-28 21:09:02
【问题描述】:

我需要有关此脚本的帮助。基本上我连接到多个数据库以获得我需要的数据集。在从所有不同的数据库中捕获数据集后,我需要将其输出写入 CSV 文件。下面是我基本上想出的代码。该脚本正在创建一个输出 CSV 文件,但它没有附加我捕获的所有数据。它只写入捕获的最后一个数据集。我该如何解决这个问题?

需要帮助来修复它。

$DB = Get-Content c:\ps\DBLIST.txt
foreach($Data in $DB)

{

Write-Host $Data
$strDB = $Data+".local"

$con = New-Object System.Data.SqlClient.SqlConnection
$con.ConnectionString = "Server=$strDB;Database=db;User ID=user;Password=password"
$con.open()

$qry = "select a, b, c, d from table1"

$cmd = New-Object System.Data.SqlClient.SqlCommand
$cmd.CommandText = $qry
$cmd.Connection = $con

$da = New-Object System.Data.SqlClient.SqlDataAdapter
$da.SelectCommand = $cmd
$ds = New-Object System.Data.Dataset
$da.Fill($ds)
$dr = $cmd.ExecuteReader()

Write-Host

$outFile = "C:\ps\OUTPUT.csv" 


    If ($dr.HasRows)
        {


          write-Host a     b     c     d

          While ($dr.Read())
            {

            Write-Host $dr["a"]         $dr["b"]        $dr["c"]        $dr["d"]

            }

        }
    Else
        {
          Write-Host There are no records found.  Try again.
        } 

$ds.Tables[0] | export-csv $outFile -NoTypeInfo -Force -Append
#$ds.Tables[0] | Export-Csv -Delimiter ','$outFile -Encoding "unicode"

Write-Host

$dr.Close()
$con.Close()

}

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您可以通过 Foreach-Object 使用 Powershell 管道,而不是 ForEach 循环。如果您将数据库服务器列表发送到管道中,然后简单地在循环内输出结果,Powershell 会将所有结果发送到管道中,有效地组合它们。然后,您可以获取最终结果并将其写入 CSV 文件。

    请注意,我必须将 $outfile 变量移到循环上方(无论如何它都属于该位置)。在 Powershell v2.0 中测试时,我还必须明确标记 -Path 参数:

    $outFile = "C:\TEMP\OUTPUT.csv" 
    
    $DB = Get-Content c:\ps\DBLIST.txt
    $DB | Foreach-Object {
        $Data = $_
        $strDB = $Data+".local"
    
        $con = New-Object System.Data.SqlClient.SqlConnection
        $con.ConnectionString = "Server=$strDB;Database=db;User ID=user;Password=password"
        $con.open()
    
        $qry = "select a, b, c, d from table1"
    
        $cmd = New-Object System.Data.SqlClient.SqlCommand
        $cmd.CommandText = $qry
        $cmd.Connection = $con
    
        $da = New-Object System.Data.SqlClient.SqlDataAdapter
        $da.SelectCommand = $cmd
        $ds = New-Object System.Data.Dataset
        $da.Fill($ds) | Out-Null   # prevent the number of records from going down the pipeline
        $dr = $cmd.ExecuteReader()
    
        # This is the magic right here -- it simply outputs the
        # contents of $ds.Tables[0] to the pipeline
        $ds.Tables[0] 
    
        $dr.Close()
        $con.Close()
    
    } | Select-Object a,b,c,d  | Export-Csv -Path $outFile -Delimiter ',' -Encoding "unicode"
    

    【讨论】:

      【解决方案2】:

      这应该可以工作

      $SomeObject | export-csv $outFile -NoTypeInfo -Append
      

      编辑不存在 -Append 的 PowerShell v2: 在一个对象中收集所有信息。完成后将此对象写入文件。

      # before you start the loop:
      $a = @()
      
      # in your loop:
      $a += $ds.Tables[0]
      
      # after the loop:
      $a | Export-Csv $outFile -NoTypeInformation
      

      【讨论】:

      • 它们总是具有相同的属性。不幸的是,我们使用的 PS 版本是 2.0 :( Append 不起作用。有什么聪明的方法可以在 2.0 上完成这项工作吗?
      • 派对迟到了,但这是我拼凑的一个脚本。接受多个 SQL 查询并将结果输出到 Excel 工作簿中。每个查询一个工作表,您可以指定工作表名称。作为额外的好处,列会自动调整大小以适应,使事情更容易阅读。 stackoverflow.com/questions/27917115/…
      猜你喜欢
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多