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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
ITguy565Commented:
@Girish,

Please let me know how that works out for you.
0
Check Out How Miercom Evaluates Wi-Fi Security!

It's not just about Wi-Fi connectivity anymore. A wireless security breach can cost your business large amounts of time, trouble, and expense. Plus, hear first-hand from Miercom on how WatchGuard's Wi-Fi security stacks up against the competition plus a LIVE demo!

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.