I need to send a file that has a header row and the data starts on the next line. It is not working. The data is either right next to the header or I can add an extra blank line (which I don’t want).
The problem code is in the last 10 lines.
This is a re-usable function that makes a connection to the database and creates a variable you can fetch the result set into
Function Execute-SqlSelect($command,$serverName,$dbName)
{
$resultDataSet = New-Object System.Data.DataSet$sqlConn = New-Object System.Data.SqlClient.SqlConnection $sqlConn.ConnectionString = "Server=$serverName;Database=$dbName;Integrated Security=SSPI" $sqlConn.Open() $sqlDataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $sqlCommand = New-Object System.Data.SqlClient.SqlCommand if($sqlConn.State -eq 1) { $sqlCommand.CommandText = $command $SqlCommand.Connection = $sqlConn $sqlCommand.CommandTimeout = 0 $sqlDataAdapter.SelectCommand = $sqlCommand $sqlDataAdapter.Fill($resultDataSet,"result") | Out-Null $sqlConn.Close() } return $resultDataSet}
#this is the sql run to get the data
$file = “P:\ReportScheduler\SCRIPTS\RelayBillPlacement.sql”$query = get-content $file
echo $query
$results = Execute-SqlSelect $query “my server” “my database”
$pathname = "c:\downloads"
$filename = [System.DateTime]::Today.AddDays(-1).ToString(“MMddyyyy”) + “_LPH_fauquier_patinfo.dat”
#this is the header row
$z = “16960|Fauquier Hospital|Fauquier Hospital|”+(Get-Date).ToString(‘MM/dd/yyyy’)+“|06:00:00”#create the file with ONLY the header line
new-item -path $pathname -name $filename -type “file” -value $z#this is the file name with the full path
$x = $pathname + $filename#drop the data into a 2nd file
$results.Tables[0] | export-csv -path “c:\downloads\aNewFile.csv” -notypeinformation -Delimiter ‘|’ -encoding ASCII$a = “c:\downloads\aNewFile.csv”
#remove the double quotes
(Get-Content $a) | Foreach-Object {$_ -replace ‘"’, ‘’}|Out-File $a#!!! this ends up adding a blank line instead of a carriage return
Add-Content -Path $x -Value “`r”#add the data to the file which only contains the header row
Add-Content -Path $x -Value (Get-Content -Path “c:\downloads\aNewFile.csv”)DONE ###