I am trying to automatize generating reports that we have to do daily on the DB but I first need the generator its self work accordingly so I can create a schedule task for it.
So I wrote a script that should fetch data from DB and export it to CSV but the problem is that it wont append and stops exporting at first 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
So I found the solution and I just merged the .CSV