批量输出非UTF-8文本文件的WMIC命令 [英] WMIC command in batch outputting non UTF-8 text files
问题描述
我正在使用WMIC命令将SIDS列表以及附带的用户个人资料名称输出为文本.从文本中,我可以编辑需要添加一组注册表项的SIDS列表.但是,在SIDS的已编辑文本文件中循环的脚本会以一种无法拾取且无法运行的格式进行编码.使用notepad ++,我可以将UCS-2 LE BOM重新编码为UTF-8,然后可以轻松完成脚本.
I'm using a WMIC command to output a list of SIDS and accompanying user profile names to text. From the text, I can edit a list of SIDS I need to add a set of registry keys to. However, the script that loops through the edited text file of SIDS is encoded in a format the script doesn't pick up and just fails to run. Using notepad++ I can re-encode from UCS-2 LE BOM to UTF-8 and then I can complete the script hassle free.
如何使WMIC文本的输出默认为UTF-8
?.
How can I make the output from the WMIC text default to UTF-8
?.
我在多台PC上注意到了这一点.如上所述,要解决此问题,我可以在notepad ++中重新编码,但是如果可能的话,我确实需要避免这一步骤.尽我所能使事情自动化.唯一的问题是编码,一旦我获得UTF-8文本文件,所有其他脚本,命令,代码等都可以.我经常使用批处理文件,并且喜欢输出到文本文件,查看它们是否都按预期默认为UTF-8.似乎特定于WMIC命令.
I noticed this on more that one PC. To cure the problem, as mentioned, I can re-encode in notepad++ but its a step I really need to avoid if possible. Trying to automate things as much as I can. The sole issue is the encoding, all other scripts, commands, codes etc. are fine once I get a UTF-8 text file. I use batch file often and like to output to text files, looking at those they are all defaulting to UTF-8 as expected. Seems specific to WMIC command here.
WMIC Path Win32_UserProfile Where "Special='False' And Not LocalPath='Null'" Get LocalPath,SID>somefile.txt
提供我需要的所有信息,但输出到UCS-2 LE BOM,而不是UTF-8
Gives all the info I need but outputs to UCS-2 LE BOM, not UTF-8
任何帮助都会很棒,谢谢. (是否正在考虑使用reg查询来绕过此问题?)
Any assistance would be great, thanks. (was thinking maybe a reg query would bypass the issue?)
推荐答案
wmic
输出的编码取决于发送输出的位置
The encoding of wmic
's output depends on where the output is being sent
- 如果使用重定向操作符或
/output
开关将输出发送到磁盘文件,则wmic
将使用UCS-2 LE BOM - 如果将输出发送到控制台或管道,则
wmic
将使用OEM代码页
- If you send the output to a disk file by using a redirection operator or the
/output
switch,wmic
will use UCS-2 LE BOM - If you send the output to the console or to a pipe,
wmic
will use OEM codepage
如果您的脚本无法处理UCS-2输出,则一个简单的选择(没有第三方工具)是使用管道来更改wmic
的写入位置.
If your scripts can not process the UCS-2 output one simple option (without third party tools) is to change where wmic
writes by using a pipe.
wmic os get localdatetime | find /v "" > someFile.txt
此处wmic
的输出通过管道传输到find /v ""
(查找任何非空行),然后使用OEM代码页写入磁盘文件.
Here wmic
's output is piped to find /v ""
(find any non empty line) and then writen to a disk file using your OEM codepage.
[W:\]:# wmic os get localdatetime > file.txt
[W:\]:# hex file.txt
HEX: +00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 0123456789abcdef
0000000000: FF FE 4C 00 6F 00 63 00 61 00 6C 00 44 00 61 00 .■L.o.c.a.l.D.a.
0000000010: 74 00 65 00 54 00 69 00 6D 00 65 00 20 00 20 00 t.e.T.i.m.e. . .
0000000020: 20 00 20 00 20 00 20 00 20 00 20 00 20 00 20 00 . . . . . . . .
0000000030: 20 00 20 00 20 00 20 00 0D 00 0A 00 32 00 30 00 . . . .....2.0.
0000000040: 31 00 39 00 30 00 33 00 32 00 33 00 31 00 30 00 1.9.0.3.2.3.1.0.
0000000050: 31 00 34 00 34 00 30 00 2E 00 30 00 39 00 34 00 1.4.4.0...0.9.4.
0000000060: 30 00 30 00 30 00 2B 00 30 00 36 00 30 00 20 00 0.0.0.+.0.6.0. .
0000000070: 20 00 0D 00 0A 00 .....
[W:\]:#
[W:\]:# wmic os get localdatetime | find /v "" > file.txt
[W:\]:# hex file.txt
HEX: +00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 0123456789abcdef
0000000000: 4C 6F 63 61 6C 44 61 74 65 54 69 6D 65 20 20 20 LocalDateTime
0000000010: 20 20 20 20 20 20 20 20 20 20 20 0D 0D 0A 32 30 ...20
0000000020: 31 39 30 33 32 33 31 30 31 35 30 34 2E 31 35 38 190323101504.158
0000000030: 30 30 30 2B 30 36 30 20 20 0D 0D 0A 0D 0D 0A 000+060 ......
[W:\]:#
如果使用这种方法,则应注意一个奇怪的副作用:输出中的行不是以CRLF
序列结尾,而是以CRCRLF
序列结尾.
If you use this approach then you should note a curious side effect: the lines in the output don't end in a CRLF
sequence, but a CRCRLF
sequence.
如果这也是脚本问题,则可以使用type
命令读取输出文件,并重定向其输出以生成另一个带有ANSI编码的文件
If this is also a problem to your scripts then you can use the type
command to read the output file and redirect its output to generate another one with ANSI enconding
[W:\]:# wmic os get localdatetime > file.txt
[W:\]:# hex file.txt
HEX: +00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 0123456789abcdef
0000000000: FF FE 4C 00 6F 00 63 00 61 00 6C 00 44 00 61 00 .■L.o.c.a.l.D.a.
0000000010: 74 00 65 00 54 00 69 00 6D 00 65 00 20 00 20 00 t.e.T.i.m.e. . .
0000000020: 20 00 20 00 20 00 20 00 20 00 20 00 20 00 20 00 . . . . . . . .
0000000030: 20 00 20 00 20 00 20 00 0D 00 0A 00 32 00 30 00 . . . .....2.0.
0000000040: 31 00 39 00 30 00 33 00 32 00 33 00 31 00 30 00 1.9.0.3.2.3.1.0.
0000000050: 32 00 33 00 31 00 31 00 2E 00 39 00 36 00 31 00 2.3.1.1...9.6.1.
0000000060: 30 00 30 00 30 00 2B 00 30 00 36 00 30 00 20 00 0.0.0.+.0.6.0. .
0000000070: 20 00 0D 00 0A 00 .....
[W:\]:#
[W:\]:# type file.txt > file2.txt
[W:\]:# hex file2.txt
HEX: +00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 0123456789abcdef
0000000000: 4C 6F 63 61 6C 44 61 74 65 54 69 6D 65 20 20 20 LocalDateTime
0000000010: 20 20 20 20 20 20 20 20 20 20 20 0D 0A 32 30 31 ..201
0000000020: 39 30 33 32 33 31 30 32 33 31 31 2E 39 36 31 30 90323102311.9610
0000000030: 30 30 2B 30 36 30 20 20 0D 0A 00+060 ..
[W:\]:#
当UCS-2文件中的字符在ANSI代码页中没有直接等价字符时,就会出现这种方法的问题.
The problem with this approach appears when the characters in the UCS-2 file don't have a direct equivalent in the ANSI codepage.
但是,如果使用第三方工具是有效的选择,则德国的CONVERTCP工具(如果您愿意编译,则包括源代码)是集成到此类脚本中的一个很好的选择.
But if using a third party tool is a valid option, then aGerman's CONVERTCP tool (including source code if you prefer to compile it) is a good alternative to integrate in this kind of scripts.
[W:\]:# tasklist /fi "pid eq 6232"
Nombre de imagen PID Nombre de sesión Núm. de ses Uso de memor
========================= ======== ================ =========== ============
Proceso↔Amañado↔.exe 6232 Console 1 2.596 KB
[W:\]:# wmic process where "processID=6232" get name > file.txt
[W:\]:# hex file.txt
HEX: +00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 0123456789abcdef
0000000000: FF FE 4E 00 61 00 6D 00 65 00 20 00 20 00 20 00 .■N.a.m.e. . . .
0000000010: 20 00 20 00 20 00 20 00 20 00 20 00 20 00 20 00 . . . . . . . .
0000000020: 20 00 20 00 20 00 20 00 20 00 20 00 20 00 20 00 . . . . . . . .
0000000030: 20 00 0D 00 0A 00 50 00 72 00 6F 00 63 00 65 00 .....P.r.o.c.e.
0000000040: 73 00 6F 00 94 21 41 00 6D 00 61 00 F1 00 61 00 s.o.ö!A.m.a.±.a.
0000000050: 64 00 6F 00 94 21 2E 00 65 00 78 00 65 00 20 00 d.o.ö!..e.x.e. .
0000000060: 20 00 0D 00 0A 00 .....
[W:\]:#
[W:\]:# type file.txt > file2.txt
[W:\]:# hex file2.txt
HEX: +00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 0123456789abcdef
0000000000: 4E 61 6D 65 20 20 20 20 20 20 20 20 20 20 20 20 Name
0000000010: 20 20 20 20 20 20 20 20 0D 0A 50 72 6F 63 65 73 ..Proces
0000000020: 6F 1D 41 6D 61 A4 61 64 6F 1D 2E 65 78 65 20 20 oAmañado.exe
0000000030: 0D 0A ..
[W:\]:#
[W:\]:# convertcp 1200 65001 /i file.txt /o file2.txt
[W:\]:# hex file2.txt
HEX: +00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 0123456789abcdef
0000000000: 4E 61 6D 65 20 20 20 20 20 20 20 20 20 20 20 20 Name
0000000010: 20 20 20 20 20 20 20 20 0D 0A 50 72 6F 63 65 73 ..Proces
0000000020: 6F E2 86 94 41 6D 61 C3 B1 61 64 6F E2 86 94 2E oÔåöAma├▒adoÔåö.
0000000030: 65 78 65 20 20 0D 0A exe ..
[W:\]:#
这篇关于批量输出非UTF-8文本文件的WMIC命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!