Powershell脚本未附加.CSV

时间:2018-11-07 16:36:36

标签: powershell append export-csv

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

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

1 个答案:

答案 0 :(得分: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
    }