G Ram
asked on
Log file showing up garbage when Write-Output is used,
Hello,
Using Write-Output cmdlet shows up garbage in log file.
And I am using PS v2 . Apparently I had been using this without any issue in machines having PS 3.0+
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
And I am using PS v2 . Apparently I had been using this without any issue in machines having PS 3.0+
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)
@Girish,
Please let me know how that works out for you.
Please let me know how that works out for you.
ASKER
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 ..
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 ..
ASKER
@ITGuy565,
I will try out..
I will try out..
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:
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
ASKER
oBdA,
I ensured -append i sthere in Out-File while specifying Unicode . I tries with many combinations. Strange thing is..
Even these lines
Thanks,
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
produce just the first line in log file..Thanks,
@Girish
Did you try my solution? if so what were the results?
Did you try my solution? if so what were the results?
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:\>
Have you tried the transcript yet? It's way easier to handle.
ASKER
oBdA,
YEs, I check after deleting log file. For PS <4 ,this gives "This host does not support transcription.",so that's ruled out
YEs, I check after deleting log file. For PS <4 ,this gives "This host does not support transcription.",so that's ruled out
ASKER
ITguy565,
No it does not make a difference ..just the same state.
No it does not make a difference ..just the same state.
When testing the transcript, don't run the script in the ISE, run it from a regular PS console.
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?
ASKER
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..
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
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.
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.
ASKER
oBdA,
For some reason this fails to write "Starting.." text , rest is done.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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...
ASKER
Thanks..
Drop the "-Encoding ASCII" completely from Out-File, or use "-Encoding Unicode" instead.