【问题标题】:Powershell script is not appending .CSVPowershell 脚本未附加 .CSV
【发布时间】:2018-11-07 16:36:36
【问题描述】:

我正在尝试自动生成我们每天必须在数据库上执行的报告,但我首先需要生成器相应地自行工作,以便我可以为其创建计划任务。

所以我编写了一个脚本,它应该从数据库中获取数据并将其导出到 CSV,但问题是它不会附加并在第一个 sql 时停止导出。

$serverName = $Global:setupConfiguration["GENERAL SETTINGS"]["ODBC_DATASOURCE_NAME"]
$databaseName = $Global:setupConfiguration["CASINO CONFIGURATION"]["DATABASENAME"]
$recordSetToFileName = @{0='1.csv';}
$currentRecordSetIndex = -1
$progAccount = $null
$iniBaseSection = $false
#Execute the SQL on the local database and dumps a csv in C:/Automation/

#fetches the data to temp table
$sqlFile = "$($Global:globalVariables.executionPath)\sql\0.sql"
$data = Invoke-SqlQuery -File $sqlFile -Server $serverName -Database $databaseName
$fileName = "$($Global:globalVariables.localDirectory)\$($recordSetToFileName[0])"
$data | export-csv $fileName -notypeinformation -force -Encoding utf8

#selects from temp table and exports.

$sqlFile = "$($Global:globalVariables.executionPath)\sql\1.sql"
$data = Invoke-SqlQuery -File $sqlFile -Server $serverName -Database $databaseName
$fileName = "$($Global:globalVariables.localDirectory)\$($recordSetToFileName[0])"
$data | export-csv  $fileName -notypeinformation  -Encoding utf8 -Force


$sqlFile = "$($Global:globalVariables.executionPath)\sql\2.sql"
$data = Invoke-SqlQuery -File $sqlFile -Server $serverName -Database $databaseName
$fileName = "$($Global:globalVariables.localDirectory)\$($recordSetToFileName[0])"
$data | export-csv -Append -Path $fileName -notypeinformation  -Encoding utf8 -Force

$sqlFile = "$($Global:globalVariables.executionPath)\sql\3.sql"
$data = Invoke-SqlQuery -File $sqlFile -Server $serverName -Database $databaseName
$fileName = "$($Global:globalVariables.localDirectory)\$($recordSetToFileName[0])"
$data | export-csv  -Append -Path $fileName -notypeinformation  -Encoding utf8 -Force

$sqlFile = "$($Global:globalVariables.executionPath)\sql\4.sql"
$data = Invoke-SqlQuery -File $sqlFile -Server $serverName -Database $databaseName
$fileName = "$($Global:globalVariables.localDirectory)\$($recordSetToFileName[0])"
$data | export-csv -Append -Path $fileName -notypeinformation  -Encoding utf8 -Force

$sqlFile = "$($Global:globalVariables.executionPath)\sql\5.sql"
$data = Invoke-SqlQuery -File $sqlFile -Server $serverName -Database $databaseName
$fileName = "$($Global:globalVariables.localDirectory)\$($recordSetToFileName[0])"
$data | export-csv -Append -Path $fileName -notypeinformation -Encoding utf8 -Force

$sqlFile = "$($Global:globalVariables.executionPath)\sql\6.sql"
$data = Invoke-SqlQuery -File $sqlFile -Server $serverName -Database $databaseName
$fileName = "$($Global:globalVariables.localDirectory)\$($recordSetToFileName[0])"
$data | export-csv -Append -Path $fileName -notypeinformation -Encoding utf8 -Force

$sqlFile = "$($Global:globalVariables.executionPath)\sql\7.sql"
$data = Invoke-SqlQuery -File $sqlFile -Server $serverName -Database $databaseName
$fileName = "$($Global:globalVariables.localDirectory)\$($recordSetToFileName[0])"
$data | export-csv -Append -Path $fileName -notypeinformation -Encoding utf8 -Force

$sqlFile = "$($Global:globalVariables.executionPath)\sql\8.sql"
$data = Invoke-SqlQuery -File $sqlFile -Server $serverName -Database $databaseName
$fileName = "$($Global:globalVariables.localDirectory)\$($recordSetToFileName[0])"
$data | export-csv -Append -Path $fileName -notypeinformation -Encoding utf8 -Force

$sqlFile = "$($Global:globalVariables.executionPath)\sql\9.sql"
$data = Invoke-SqlQuery -File $sqlFile -Server $serverName -Database $databaseName
$fileName = "$($Global:globalVariables.localDirectory)\$($recordSetToFileName[0])"
$data | export-csv -Append -Path $fileName -notypeinformation -Encoding utf8 -Force

$sqlFile = "$($Global:globalVariables.executionPath)\sql\10.sql"
$data = Invoke-SqlQuery -File $sqlFile -Server $serverName -Database $databaseName
$fileName = "$($Global:globalVariables.localDirectory)\$($recordSetToFileName[0])"
$data | export-csv -Append -Path $fileName -notypeinformation -Encoding utf8 -Force

$sqlFile = "$($Global:globalVariables.executionPath)\sql\11.sql"
$data = Invoke-SqlQuery -File $sqlFile -Server $serverName -Database $databaseName
$fileName = "$($Global:globalVariables.localDirectory)\$($recordSetToFileName[0])"
$data | export-csv -Append -Path $fileName -notypeinformation -Encoding utf8 -Force

【问题讨论】:

  • 您指的是带有$($recordSetToFileName[0] 的保存的CSV 文件,并且始终是1.csv,因为您永远不会更改它。你确定其他部分没有运行......因为看起来你每次都只是覆盖输出文件。
  • @Lee_Dailey ,它不应该覆盖,因为我正在使用 -append 并且我正在使用 $($recordSetToFileName[0] 进行命名,因为某些报告需要不同的报告 .csv 文件,但这个不需要它。
  • 不确定它为什么“停止”,因为您没有向我们提供太多信息。听起来其他语句甚至都没有运行? $data 是否在后续脚本中返回了任何内容?是默默的失败吗?请为此创建一个函数。您一遍又一遍地重复代码。现在更改为$filename 很痛苦
  • 您的第一次和第二次导出不要使用-Append。我设法错过了 others 确实使用该参数。您可能会将其添加到第二个。 [咧嘴]
  • 第一个甚至应该有输出,因为它是在 DB 中创建临时表的原因,而第一个作为第二个是创建 1.csv 文件的第一个,其他任何内容都应该附加到它上面,这就是它失败。至于 $filename,它应该很重要,因为我只是附加到以前的。

标签: powershell append export-csv


【解决方案1】:

所以我找到了解决方案,我只是合并了 .CSV

#Merges ALL .CSV Files to final.CSV

$getFirstLine = $true

get-childItem "$($Global:globalVariables.localDirectory)\CSVTemp\*.csv" | foreach {
    $filePath = $_

    $lines =  $lines = Get-Content $filePath  
    $linesToWrite = switch($getFirstLine) {
           $true  {$lines}
           $false {$lines | Select -Skip 1}

    }

    $getFirstLine = $true
    Add-Content "$($Global:globalVariables.localDirectory)\CSVTemp\$nameFinal" $linesToWrite
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 2021-10-23
    • 2022-01-13
    • 1970-01-01
    • 2011-10-27
    相关资源
    最近更新 更多