[英]Powershell Add-content Out-File Text is wrong
When i use this: 当我使用这个:
"$LogTime $CurrentDate Invalid Archive Grouping selected. You selected '$ArchiveGrouping'" | Out-File $LogFile -Append -Force
To write to a file i generated with this code: 要写入使用此代码生成的文件:
$Logdate = Get-Date
$Logname = $Logdate.ToString("yyyyMMdd") + ".txt"
Add-Content -Path C:\ArchiveStorage\$Logname "Log" -force
$LogFile = "C:\ArchiveStorage\$Logname"
The Text in the file looks weird it looks like this: 文件中的文本看起来很奇怪,看起来像这样:
if i change the code to just write to an existing file like: 如果我将代码更改为仅写入现有文件,例如:
$LogFile = "C:\ArchiveStorage\Log.txt"
The text file is as it should be: 文本文件应为:
why does it make spaces and all that random crap ? 为什么它会产生空间和所有这些乱七八糟的东西?
You've been hit by double byte Unicode. 您已经被双字节Unicode打了。 When, say, letter
A
is written into the file, there usually is a single byte value 0x41
. 例如,当字母
A
写入文件时,通常只有一个字节值0x41
。 In double byte Unicode, the byte value for A
is 0x0041
or 0x4100
depending on endianess. 在双字节的Unicode,对于字节值
A
是0x0041
或0x4100
取决于字节序。
When Notepad opens a file that has no Unicode byte order mark , it assumes all the extra 00
in file contents are blank spaces. 当记事本打开没有Unicode 字节顺序标记的文件时,它将假定文件内容中所有额外的
00
都为空格。 That's why you do see weridspacing
. 这就是为什么您看到
weridspacing
的原因。
For a fix, try using -Encoding ASCII
parameter with Add-Content
. 要解决此问题,请尝试将
-Encoding ASCII
参数与Add-Content
。
The "Log" entries look different because they were written with Add-Content, which uses a default encoding of ASCII, and Out-File uses a default encoding of Unicode. “日志”条目看起来有所不同,因为它们是使用Add-Content编写的,它使用ASCII的默认编码,而Out-File使用Unicode的默认编码。
Either specify the encoding type on Out-File, or switch to using Add-Content for both the "Log" heading and the detail lines. 在“文件外”上指定编码类型,或者切换到“日志”标题和明细行都使用添加内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.