Log file showing up garbage when Write-Output is used,

Hello,
 Using Write-Output cmdlet shows up garbage in log file.
$Logfile="\\server2\svrback\DGS_Backup\log.txt"
$copyitems=Copy-Item -Path $SourcePath -Destination "\\server2\svrback\DGS_Backup\" -Recurse -force  -Passthru  -ea Stop |Select Name,LastWriteTime,Mode 
$copyitems| tee-object -filepath $Logfile 
$logstring ="Copied.."  # This shows as garbage in log file
Write-Output  $logstring | Out-File $Logfile -Encoding ascii   -append

Open in new window


And I am using PS v2 . Apparently I had been using  this without any issue in machines having PS 3.0+
G RamAsked:
Who is Participating?
 
oBdACommented:
That's the Tee-Object which overwrites the file created in line 5. -Append is only available since PS 3.0.
But nobody forces you to use it, especially since you have the results in a variable anyway.
$LogFile = "\\server2\svrback\DGS_Backup\log.txt"
$SourcePath= "C:\DGS\Folders\Svr2"
$logstring = "Copying done.."
Remove-Item $LogFile -ErrorAction SilentlyContinue
Write-Output  "Starting to copy.. " | Out-File $Logfile   -append
$copyitems=Copy-Item -Path $SourcePath -Destination "\\server2\svrback\DGS_Backup\" -Recurse -force  -Passthru  -ea Stop |Select Name,LastWriteTime,Mode 
$copyitems | Out-File $Logfile  -append
$copyitems
Write-Output  $logstring | Out-File $Logfile  -append

Open in new window

0
 
oBdACommented:
That happens because you're creating the log file with Tee-Object, which creates a Unicode file, and then force-append ASCII characters with Out-File - Unicode and ASCII just don't mix.
Drop the "-Encoding ASCII" completely from Out-File, or use "-Encoding Unicode" instead.
0
 
ITguy565Commented:
This has not been tested but it should work.. Please let me know if you have any issues.

$Logfile="\\server2\svrback\DGS_Backup\log.txt"
$copyitems=Copy-Item -Path $SourcePath -Destination "\\server2\svrback\DGS_Backup\" -Recurse -force  -Passthru  -ea Stop |Select Name,LastWriteTime,Mode 
$copyitems| tee-object -filepath $Logfile 
$logstring ="Copied.."  # This shows as garbage in log file
Write-Output  $logstring | Out-File $Logfile -Encoding ascii   -append

#Processes File for last time and Removes any non-UTF 8 BOM
            $logimport = @(get-content "\\server2\svrback\DGS_Backup\log.txt")
            $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
            [System.IO.File]::WriteAllLines("\\server2\svrback\DGS_Backup\log.txt", $logimport, $Utf8NoBomEncoding)  

Open in new window

0
WEBINAR: 10 Easy Ways to Lose a Password

Join us on June 27th at 8 am PDT to learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees. We'll cover the importance of multi-factor authentication and how these solutions can better protect your business!

 
ITguy565Commented:
@Girish,

Please let me know how that works out for you.
0
 
G RamAuthor Commented:
oBdA,
     If I  drop the "-Encoding ASCII" completely from Out-File,only the Write-Output  string gets into log file, File names to be copied do not come and if I add this.,its just blank log file. I had already tried out the first option ..
0
 
G RamAuthor Commented:
@ITGuy565,
 I will try out..
0
 
oBdACommented:
This works just fine here both in 2.0 and 3.0.
Could it be that you accidentally removed the "-Append" from Out-File as well?
And have you considered using a transcript instead of doing everything manually? A transcript will just log everything ending up in the console:
$Logfile = "\\server2\svrback\DGS_Backup\log.txt"
Start-Transcript -Path $LogFile
$copiedItems = Copy-Item -Path $SourcePath -Destination "\\server2\svrback\DGS_Backup\" -Recurse -Force -Passthru -ea Stop | Select-Object Name, LastWriteTime, Mode
$copiedItems
"Copy done at $((Get-Date).ToLongDateString())"
Stop-Transcript

Open in new window

0
 
G RamAuthor Commented:
oBdA,

I ensured -append  i sthere in Out-File while specifying Unicode . I tries with many combinations. Strange thing is..
Even these lines
Write-Output  "Starting to copy.. " | Out-File $Logfile -Encoding Unicode   -append 
Write-Output  $logstring | Out-File $Logfile   -append   # also iried specifying Unicode

Open in new window

produce just the first line in log file..

Thanks,
0
 
ITguy565Commented:
@Girish

Did you try my solution? if so what were the results?
0
 
oBdACommented:
Can't reproduce. Have you deleted the log file (which still contained the mixture of Unicode and ASCII) before retrying?
PS C:\> $LogFile = 'C:\Temp\OutFile.log'
PS C:\> $logstring = 'Hello World'
PS C:\> Remove-Item $LogFile -ErrorAction SilentlyContinue
PS C:\> Write-Output  "Starting to copy.. " | Out-File $Logfile -Encoding Unicode   -append
PS C:\> Write-Output  $logstring | Out-File $Logfile   -append
PS C:\> gc $LogFile
Starting to copy..
Hello World
PS C:\>

Open in new window

Have you tried the transcript yet? It's way easier to handle.
0
 
G RamAuthor Commented:
oBdA,

   YEs, I check after deleting log file. For PS <4 ,this gives "This host does not support transcription.",so that's ruled out
0
 
G RamAuthor Commented:
ITguy565,

No it does not make a  difference ..just the same state.
0
 
oBdACommented:
When testing the transcript, don't run the script in the ISE, run it from a regular PS console.
0
 
ITguy565Commented:
interesting.. I am using that same code in several of my PowerShell scripts to combat just that.. Maybe you can post a sample of what you are referring to?
0
 
G RamAuthor Commented:
oBdA,
   Ah.. yes it is OK if I run from PS console.But since it dumps everything.. that's something which end user may not want. Hence I tried using Add-Content which i has used in V3.0+ .Trying many options..
out-file "\\server2\svrback\DGS_Backup\log.txt" -Encoding Unicode
 Write-Output "Starting to copy.." `n | Add-Content "\\server2\svrback\DGS_Backup\log.txt" -PassThru -Encoding Unicode 

#Write-Output  "Starting to copy.. " | Out-File "\\server2\svrback\DGS_Backup\log.txt" -Encoding Unicode -append
$copyitems=Copy-Item -Path "C:\DGS\Folders\DGS" -Destination "\\server2\svrback\DGS_Backup\" -Recurse   -Passthru  -ea Stop |Select Name,LastWriteTime,Mode 
$copyitems| tee-object -filepath "\\server2\svrback\DGS_Backup\log.txt"    
Write-Output  "NextLine" | Add-Content "\\server2\svrback\DGS_Backup\log.txt" -PassThru -Encoding Unicode 

Open in new window

0
 
oBdACommented:
The transcript dumps what you'd see in the console. Nobody forces you to output everything. The advantage of the transcript is that it covers errors as well, which would not be logged at all the way you're handling it now.
The sample in https:#a42536251 would write exactly what you're trying to write manually (plus the transcription header/footer lines), without the giant overhead of x "Write-Output" lines.
0
 
G RamAuthor Commented:
oBdA,
 For some reason this fails to write  "Starting.." text , rest is done.
$LogFile = "\\server2\svrback\DGS_Backup\log.txt"
$SourcePath= "C:\DGS\Folders\Svr2"
$logstring = "Copying done.."
Remove-Item $LogFile -ErrorAction SilentlyContinue
Write-Output  "Starting to copy.. " | Out-File $Logfile   -append
$copyitems=Copy-Item -Path $SourcePath -Destination "\\server2\svrback\DGS_Backup\" -Recurse -force  -Passthru  -ea Stop |Select Name,LastWriteTime,Mode 
$copyitems| tee-object -filepath $Logfile 
Write-Output  $logstring | Out-File $Logfile  -append

Open in new window

0
 
G RamAuthor Commented:
Yes.. this works. Thanks for the assistance .Wonder if I can select Tee-Object into a  hash table and show up in  variable  without getting "Cannot convert ..[Object] .. to [String] " error which is obvious...
0
 
G RamAuthor Commented:
Thanks..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.