Using -verbose, only gives linefeed

by dbishop211 at 2012-09-24 15:31:25

When I use the -verbose parameter with invoke-sqlcmd and log to a transcript file, the output only contains a linefeed character and no carriage return. When I view the output in Notepad, the output is run into one very, cery long line (it wraps at some point but not sure where). Is it possible to have -verbose output to the transcript file with a [CR][LF] instead of only a [LF].
by DonJ at 2012-09-24 16:00:19
That’s entirely up to the cmdlet author and/or PowerShell (which is more likely) - there’s no way for you to modify that behavior.
by dbishop211 at 2012-09-25 07:08:27
If that is so, once I’ve stopped the trascription, is there some way I can, within my Powershell script process the transcript file and find all occurrances of [LF] that are not preceeded by a [CR] and replace [LF] with [CR][LF]?
At the same time, I’ve noticed that the output created by the -verbose parameter of invoke-sqlcmd preceeds the actual text created with "VERBOSE: " (quotes added; e.g. “VERBOSE: Changed database context to ‘NC_Extract’.”). At the same time I am ‘processing’ the transcript file, I would like to delete all occurrances of "VERBOSE: “.

Can this be done easily?
by DonJ at 2012-09-25 07:27:45
Sure. n is the CRLF character; read about_escape* in the shell's help to find just LF or CR. Use -replace. <br><br>And to get rid of VERBOSE, you'd do the same thing. Use the -replace operator.</blockquote>by poshoholic at 2012-09-25 08:15:21<blockquote>Dealing with different line termination character sequences is a pain. I routinely use something like this (with shorter variable names) in many PowerShell scripts I write:<br><code>$stringWithConsistentAndCorrectLineTerminationSequences = $stringWithMixedOrIncorrectLineTerminationSequences -replace &quot;rn|r|n&quot;,[System.Environment]]</code></blockquote>by dbishop211 at 2012-09-26 06:21:51<blockquote>Sorry to keep asking, but how would I go about 'cleaning up' a file? I suppose I would have to read it in a line at a time, and write it back out to a new file, but this is quite a bit above anything I've done so far with Powershell.<br>I am relatively new to Powershell, and although I love what I can do with it, learning it so far has been finding articles on the Internet and piecing them together. I am basically a database developer and using Powershell to control execution of a large number of scripts.</blockquote>by DonJ at 2012-09-26 08:10:16<blockquote>Something kind of like this:<br><br><code><br>ForEach ($line in (Get-Content inputfile&#46;txt) {<br> $line -replace &quot;this&quot;,&quot;that&quot; | Out-File outputfile&#46;txt -append<br>}<br></code><br><br>You can see how Kirk used the -replace function in his post; I've just provided a simpler example here (replacing &quot;this&quot; with &quot;that&quot;) for illustration.</blockquote>by dbishop211 at 2012-09-26 09:45:17<blockquote>Okay, starting to get there. What I did not mention, and figured I could handle similarly, is that when -verbose is used with invoke-sqlcmd, it prefixes each line of output with &quot;VERBOSE: &quot;, which I also wanted to remove. This is the code I have (actually killing two birds with one stone):<br><br><code>ForEach ( $line in (Get-Content &#46;\Patient&#46;log) )<br>{<br> $line -replace &quot;nVERBOSE:”, [Environment]::NewLine | Out-File .\outputfile.txt -append

When I run this, the output file does have CRLF, but "VERBOSE: " is still in the file (which doesn’t make sense to me. What I do know is that the source (and subsquently the output) file is in unicode. Would this make a difference?
by DonJ at 2012-09-26 09:53:44
That’s because it isn’t matching “`nVERBOSE”. You’re probably going to have to have it just match, “VERBOSE:” to get rid of that.
by poshoholic at 2012-09-26 19:30:51
When you call Get-Content, it returns an array of strings, with each string representing a line in the file without any newline characters. That last part is key. I think you’re not even getting newline characters in the results from Get-Content. In which case you just want to remove any line that contains nothing but “VERBOSE:” and possibly leading or trailing whitespace. Here’s a command that should do that for you.
Get-Content C:\patient.log | Where-Object {$_ -notmatch '^\s+?VERBOSE]