0

I'm using StringBuilder.Replace in a PowerShell script to strip out line breaks in text fields before outputting to a log file. Below is an example of what I'm using... and it works perfectly on our development environment. However, on the live environment, no line breaks are stripped out at all. Does anyone know what could be causing it to differ from environment to environment? There is a lot more content on the live server, but since the actual system is identical to the dev, all the text fields themselves are the same.

$log = "C:\mylogfile.csv"   
$newline = [System.Environment]::NewLine
$sb2 = New-Object System.Text.StringBuilder 
$sb2.Append("Text fields")
$sb2.Replace($newline,".")
$sb2.ToString() | Out-File $log -Append

1 Answer 1

1

Ok, sod's law that I find a solution shortly after posting on here..!

The following works for me. I'd experimented with 'r and 'n with no luck, but by doing both of them together with NewLine, all line breaks are now being stripped out:

$log = "C:\mylogfile.csv"       
$newline = [System.Environment]::NewLine
$charsToStrip = "`r","`n", $newline
$sb2 = New-Object System.Text.StringBuilder 
$sb2.Append("Text fields")
foreach ($char in $charsToStrip)
{
  $sb2.Replace($char,".")
}
$sb2.ToString() | Out-File $log -Append
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.