为什么wmic的输出在powershell it';有什么不同?如何纠正?

为什么wmic的输出在powershell it';有什么不同?如何纠正?,powershell,batch-file,cmd,Powershell,Batch File,Cmd,我正在制作一个批处理dos代码来执行命令,并将代码捕获到文件中,同时将输出保持在屏幕上 EDIT2:4月21日3日15:36 我想要执行的命令既慢又快,有些命令只能执行一次。 例如,我必须执行chkdsk、dism、sfc、wmic、dos(内部、外部)、powershell等命令 我认为最快的方法是使用powershell“T” 前。 (echo foo和echo bar)| powershell“$input | tee-a out.txt” C:\WINDOWS\system32>(ech

我正在制作一个批处理dos代码来执行命令,并将代码捕获到文件中,同时将输出保持在屏幕上

EDIT2:4月21日3日15:36 我想要执行的命令既慢又快,有些命令只能执行一次。 例如,我必须执行chkdsk、dism、sfc、wmic、dos(内部、外部)、powershell等命令

我认为最快的方法是使用powershell“T”

前。 (echo foo和echo bar)| powershell“$input | tee-a out.txt”

C:\WINDOWS\system32>(echo-foo和echo-bar)| powershell“$input | tee-a out.txt”
福
酒吧
但当我将其与wmic一起使用时,我得到了不同的结果:

C:\WINDOWS\system32>wmic recoveros获取自动电子书
自动锅炉
错误的
C:\WINDOWS\system32>wmic recoveros get autoreboot | powershell“$input | tee-a out.txt”
自动锅炉
错误的
C:\WINDOWS\system32>

  • 为什么?
  • 有没有一种方法可以在不破坏代码的情况下进行更正

EDIT1:4月21日3日14:46

我添加了一些如何建议JosefZ的信息:

C:\WINDOWS\system32>del out.txt out2.txt
C:\WINDOWS\system32>wmic recoveros get autoreboot>out2.txt
C:\WINDOWS\system32>wmic recoveros get autoreboot | powershell“$input | tee-a out.txt”
自动锅炉
错误的
C:\WINDOWS\system32>键入out.txt
自动锅炉
错误的
C:\WINDOWS\system32>键入out2.txt
自动锅炉
错误的
C:\WINDOWS\system32>powershell-nopro“(cat out.txt-编码字节)-加入“”“
255 254 65 0 117 0 116 0 111 0 82 0 101 0 98 0 111 0 111 0 116 0 32 0 32 0 13 0 10 0 13 0 10 0 70 0 65 0 76 0 83 0 69 0 32 0 32 0 32 0 32 0 32 0 32 0 32 0 13 0 10 0 13 0 10 0 13 0 10 0 13 0 10 0
C:\WINDOWS\system32>powershell-nopro“(cat-out2.txt-编码字节)-加入“”“
255 254 65 0 117 0 116 0 111 0 82 0 101 0 98 0 111 0 111 0 116 0 32 0 32 0 13 0 10 0 70 0 65 0 76 0 83 0 69 0 32 0 32 0 32 0 32 0 32 0 32 0 32 0 13 0 10 0
C:\WINDOWS\system32>

EDIT3:4月21日3日16:05 这是我目前开发的代码(一些命令现在用“rem”注释):

用于%%C英寸(
“rem sfc/scannow”
“rem dism/online/cleanup image/scanhealth”
“rem dism/在线/清理图像/恢复健康”
“rem sfc/scannow”
“rem”
“rem chkdsk/扫描”
“rem”
“wmic recoveros获得自动电子书”
“wmic recoveros set autoreboot=false”
“wmic recoveros获得自动电子书”
“wmic recoveros获取调试信息类型”
“wmic recoveros set-DebugInfoType=7”
“wmic recoveros获取调试信息类型”
“rem”
“wmic页面文件列表/格式:列表”
“wmic Computersystem,其中name=“%COMPUTERNAME%”获取AutomaticManagedPagefile”
“wmic Computersystem,其中name=“%COMPUTERNAME%”设置AutomaticManagedPagefile=True”
“wmic Computersystem,其中name=“%COMPUTERNAME%”获取AutomaticManagedPagefile”
“rem”
“bcdedit/enum{badmemory}”
)do(%ComSpec%/c“echo(%CD%^>%%~c&%%~c”| powershell-command“$input | tee-Append test.txt”)

EDIT4:2021年4月10日8:21 我添加这段代码是为了创建一个参考输出,以方便各种测试之间的比较过程。这个输出在屏幕和文件上都要考虑

@echo关闭
rem警告!以管理员身份运行。
rem去一个安全的地方测试。。。
cd%温度%
对于%%C英寸(
“rem sfc/scannow”
“rem dism/online/cleanup image/scanhealth”
“rem dism/在线/清理图像/恢复健康”
“rem sfc/scannow”
“rem chkdsk/扫描”
“wmic recoveros获得自动电子书”
“wmic recoveros set autoreboot=false”
“wmic recoveros获得自动电子书”
“wmic recoveros获取调试信息类型”
“wmic recoveros set-DebugInfoType=7”
“wmic recoveros获取调试信息类型”
“wmic页面文件列表/格式:列表”
“wmic Computersystem,其中name=“%COMPUTERNAME%”获取AutomaticManagedPagefile”
“wmic Computersystem,其中name=“%COMPUTERNAME%”设置AutomaticManagedPagefile=True”
“wmic Computersystem,其中name=“%COMPUTERNAME%”获取AutomaticManagedPagefile”
“bcdedit/enum{badmemory}”
)做(
rem部分模拟命令上的“回声开启”。
回显(%CD%^>%%~C
rem执行命令。
%%~C
rem可选,但会打开另一个cmd进程。
rem%ComSpec%/c“回音(%CD%^>%%c&%%c”
)
暂停
后藤:eof


正如其他人在评论中提到的,因为它输出不一致的行尾(CR+CR+LF)。您看到的Powershell行为与它获得的输入一致,只是WMIC的行为不好。在我看来,如果要写入文件和屏幕,您最好调用它两次。它不太可能更改,也不是一个缓慢的命令:

wmic recoveros get autoreboot
wmic recoveros get autoreboot > out.txt
但是,您应该能够这样做,以去除任何空白或仅空白行:

wmic recoveros get autoreboot | powershell.exe "$input | Where-Object { -not [String]::IsNullOrWhiteSpace($_) } | Tee-Object out.txt"
这对我来说是可行的,但速度也要慢得多,因为系统必须启动Powershell才能做到这一点


否则,是否有理由不将Powershell完全用于您的脚本?如果您正在执行许多类似的操作,这可能是值得的。您可以或多或少地用
Get cimpinstance-ClassName Win32\u OSRecoveryConfiguration
替换wmic调用。如果您想知道
wmic
使用的实际类名,可以使用所有
wmic别名列表摘要

如其他人在评论中所述,因为它输出不一致的行尾(CR+CR+LF)。您看到的Powershell行为与它获得的输入一致,只是WMIC的行为不好。在我看来,如果要写入文件和屏幕,您最好调用它两次。它不太可能更改,也不是一个缓慢的命令:

wmic recoveros get autoreboot
wmic recoveros get autoreboot > out.txt
但是,您应该能够这样做,以去除任何空白或仅空白行:

wmic recoveros get autoreboot | powershell.exe "$input | Where-Object { -not [String]::IsNullOrWhiteSpace($_) } | Tee-Object out.txt"
这对我来说是可行的,但速度也要慢得多,因为系统必须启动Powershell才能做到这一点

否则,是否有理由不将Powershell完全用于您的脚本?如果您正在执行大量类似的操作,那么这可能是值得的。您可以或多或少地用<
echo "%CD%>%%~C"| powershell -noprofile -c "$($input).Trim([char] 0x22), (((Get-Content -Raw %TEMP%\tmp.txt -Encoding Oem) -replace '\r\r\n', [Environment]::NewLine).Trim[Environment]::NewLine) + [Environment]::NewLine) | tee -Append test.txt"
@echo off & setlocal

:: Delete a previous output file, if present.
del test.txt 2>NUL

for %%C in (
  "wmic recoveros get autoreboot"
  "wmic recoveros set autoreboot = true"
  "wmic recoveros get DebugInfoType"
  "wmic recoveros set DebugInfoType = 7"
  "bcdedit /enum {badmemory}"
  "wmic Computersystem where name="%COMPUTERNAME%" get AutomaticManagedPagefile"
) do ( 
  %%~C > %TEMP%\tmp.txt
  echo "%CD%>%%~C"| powershell -noprofile -c "$o = $($input).Trim([char] 0x22) + [Environment]::NewLine + (Get-Content -Raw %TEMP%\tmp.txt -Encoding Oem) -replace '\r\r\n', [Environment]::NewLine; if ([IO.File]::ReadAllBytes('%TEMP%\tmp.txt')[0..1] -join ' ' -ne '255 254') { $o = $o -replace '\r\n\z' }; $o | tee -Append test.txt"
  del %TEMP%\tmp.txt
)