简体   繁体   English

PowerShell Out-File 在字符之间输出额外的空格

[英]PowerShell Out-File outputs extra spaces in between characters

I'm using a simple script that outputs current directory's path inside a log file:我正在使用一个简单的脚本,它在日志文件中输出当前目录的路径:

(Get-Item .).FullName | out-file "C:\windows\system32\rcwm\rc.log" -append

This works most of the time.这在大多数情况下都有效。 However, seemingly at random, appending will result in added spaces after every character in the output string.但是,看似随机,追加将导致在输出字符串中的每个字符后添加空格。 My rc.log file will then look like this:我的rc.log文件将如下所示:

C:\Users\root\Desktop\2222
C : \ U s e r s \ r o o t \ D e s k t o p \ 2 2 2 2 

Notice how I outputted the same directory name twice, and the results inside the log file were different.请注意我是如何输出相同的目录名称两次,并且日志文件中的结果不同。

Once this happens, every subsequent string appended into the log file will have spaces in between characters.一旦发生这种情况,附加到日志文件中的每个后续字符串的字符之间都会有空格。

What could be the issue?可能是什么问题?

After some time, I decided to try to change the encodings with which powershell outputs strings inside my log file.一段时间后,我决定尝试更改 powershell 在我的日志文件中输出字符串的编码。

I still don't understand why or how powershell decides which encoding to use, but it's best if you let powershell know which encoding you want to use:我还是不明白,为什么还是PowerShell如何决定哪些编码使用,但如果让你想使用的编码PowerShell的知道这是最好的:

(Get-Item .).FullName | out-file "C:\windows\system32\rcwm\rc.log" -append -Encoding UTF8

For me, UTF-8 seems to work best.对我来说,UTF-8 似乎效果最好。

C:\Users\root\šč   (UTF8)
C:\Users\root\??   (ASCII)
C : \ U s e r s \ r o o t \ a   (UNICODE)
 C   :   \   U   s   e   r   s   \   r   o   o   t   \   a   (UTF32)
C:\Users\root\šč\ő\~2\üē (UTF8)

输出文件

I'm using a simple script that outputs current directory's path inside a log file:我正在使用一个简单的脚本,该脚本在日志文件中输出当前目录的路径:

(Get-Item .).FullName | out-file "C:\windows\system32\rcwm\rc.log" -append

This works most of the time.这在大多数情况下都有效。 However, seemingly at random, appending will result in added spaces after every character in the output string.但是,看似随机,追加将在输出字符串中的每个字符之后增加空格。 My rc.log file will then look like this:我的rc.log文件将如下所示:

C:\Users\root\Desktop\2222
C : \ U s e r s \ r o o t \ D e s k t o p \ 2 2 2 2 

Notice how I outputted the same directory name twice, and the results inside the log file were different.请注意,我是如何两次输出相同的目录名,并且日志文件中的结果是不同的。

Once this happens, every subsequent string appended into the log file will have spaces in between characters.一旦发生这种情况,后面的每个附加到日志文件中的字符串都将在字符之间有空格。

What could be the issue?可能是什么问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM