简体   繁体   中英

Out-File truncation of text

I have the following PowerShell script that queries a single column from a table and I want to send the results to a text file:

$SqlStatement = 
"SELECT Result 
FROM JobRun 
WHERE AccountID = 13 
    AND InterfaceID = 2
    AND Started BETWEEN '4/1/2016' AND '4/2/2016'"


$Result = Invoke-Sqlcmd -ServerInstance "xxxx" -Database "Vincent" -Query $SqlStatement -Username "rminder" -Password "xxxx"
$Result | Out-File C:\Temp\MyProject\SUP_20160401.Txt -Width 4000

Without the -Width parameter, the text is truncated at roughly 255 characters. Even with the -Width parameter, the text is truncated at roughly 2155 characters. It looks something like this:

EVV_AutoConfirmFlag|EVV_VisitRecordIndicator...                                                

Is there a way I can avoid any truncation at all? I intend to run this query many times. The Result column in the SQL Server table is a varchar(max) column so it can hold very large amounts of data. In the query above, the resulting column contains ~40K bytes of text.

By the way, I also tried Export-Csv as follows, but that truncates at around 4K of text.

$Result | Export-Csv C:\Temp\MyProject\SUP_20160401.Csv -NoTypeInformation

As @vonPryz says there are multiple issues. This should work

$Result = Invoke-Sqlcmd -ServerInstance "xxxx" -Database "Vincent" -Query $SqlStatement -Username "rminder" -Password "xxxx" -MaxCharLength ([int]::MaxValue)
$Result | Export-Csv C:\Temp\MyProject\SUP_20160401.Csv -NoTypeInformation

You will get more than 4K text with MaxCharLength and be able to output it in CSV.

Use Set-Content .

Invoke-Sqlcmd -ServerInstance "xxxx" -Database "Vincent" -Query $SqlStatement `
    -Username "rminder" -Password "xxxx" |
  Set-Content -Path C:\Temp\MyProject\SUP_20160401.Csv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM