Avoid PowerShell creating line break in long strings

Andrés Solano 11 Reputation points
2021-03-08T17:45:02.527+00:00

Hello Community,

I´m pretty new working with PowerShell and I have some doubts about some behavior related with PowerShell. Basically, I´m trying to create a .json file with the output of a select statement, for this I created a process that generates the .json file in SQL Server and then it prints it and lot it into a file using PowerShell

Invoke-Sqlcmd -inputfile $script -QueryTimeout 30 -ServerInstance $instance -Database $database -Verbose 4> "$file"

This process create a .json with the structure that I desired, but I saw some problems related with some strings that has a huge length were in this cases PowerShell added a line break when the string is too long. For example:

75572-file.txt

This file shows in the column4 where the string is getting cut like

"COLUMN4":"AAAAAAAAAAAAAAAAAAAAAA,.......,OOOOOOOO,PPPPPPPPPPP,QQQQQQQQQQQQQQQ
QQQQQQQQ,RRRRRRRRRR,.......,BBBBBBBBBBBBB
BBB,CCCCCCCC,DDDDDDDDDD,FFFFFFFFFF",

Where the lines with Q and B are having a line break.

Is there a way to avoid PowerShell adding this extra line while the file creation process?

Or is any other way I can remove this line break without removing all the break lines?

I will appreciate any help provided.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,355 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. MotoX80 31,561 Reputation points
    2021-03-08T18:18:53.417+00:00

    Instead of redirecting stdout (which might get console settings involved) , try it this way.

    Invoke-Sqlcmd -inputfile $script -QueryTimeout 30 -ServerInstance $instance -Database $database -Verbose 4  | out-file "$file"
    
    0 comments No comments

  2. Andrés Solano 11 Reputation points
    2021-03-08T20:08:21.02+00:00

    Hello,

    I tried the code above, but I'm getting the following error message:

    Invoke-Sqlcmd : The 'Query' and the 'InputFile' options are mutually exclusive.

    Thanks in advance.


  3. Rich Matheisen 44,696 Reputation points
    2021-03-08T20:25:33.423+00:00

    What sort of data are returned from the Invoke-SQL cmdlet? If you assigned the output from the Invoke-SQL cmdlet to a variable, is that variable an array of PSCustomObjects? If it is you could just pipe the results of the Invoke-SQL command to a ConvertTo-JSON cmdlet.

    The Out-File usually assumes the width of the PowerShell console. You can add the "-Width" parameter, but if the length of the output exceeds that length the output is truncated. Why not pipe the results to a Set-Content cmdlet instead?

    0 comments No comments

  4. Andrés Solano 11 Reputation points
    2021-03-10T17:19:58.63+00:00

    I just fixed the file with the -NoNewLine command, this made only one string of the json file then I replace some characters as needed to create the line breaks in order to provide the necessary format.

    #Replace double quotes and comma with double quotes, comma and a line break
    (Get-Content $file) -replace '",', """,`r`n" | Set-Content $file -Force
    
    #Replace brakets with line breaks and a braket
    (Get-Content $file) -replace "{","`r`n{`r`n" | Set-Content $file -Force
    
    #Replace brakets with line breaks and a braket
    (Get-Content $file) -replace "}","`r`n}`r`n" | Set-Content $file -Force
    
    0 comments No comments

  5. Suresh Sekar 1 Reputation point
    2021-07-20T13:27:02.673+00:00

    You should use Out-File to write the results to the file and use -Width parameter to specify the number of characters in a line upto which line break should not occur. The maximum number you can specify for the width is 2147483647.

    Try the below command and it should work as you expect.

    Invoke-Sqlcmd -inputfile $script -QueryTimeout 30 -ServerInstance $instance -Database $database -Verbose 4>&1 | Out-File $file -Width 2147483647

    0 comments No comments