0

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

所以我写了一个脚本,应该从数据库中获取数据并将其导出到 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
4

1 回答 1

0

所以我找到了解决方案,我只是合并了 .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
    }
于 2018-11-12T14:56:56.867 回答