WMIC 输出到文本文件

WMIC output to text file

我有一个脚本,我 运行 在我成像的所有电脑上,它基本上看起来并告诉我是否安装了程序,如果安装了,是什么版本。 当我 运行 我在 CMD window 中的命令有效时。 但作为我脚本的一部分,它删除了 txt 文件中的所有内容并添加了奇怪的字符。 这是边缘代码。

:Edge
If  exist "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"  (
 echo Edge is installed >> C:\Temp\Message.txt
 SET "EDGE=Y"
 wmic /OUTPUT:"C:\Temp\Message.txt" datafile where 'name="C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"' get version 
) Else (
SET "EDGE=N"&echo Edge is Not installed >> C:\Temp\Message.txt
)

这是结果 版本 94.0.992.50 ??????????4???????????????????????????? ??????????????????

我很茫然.....

您可以试试这个在一行中使用 powershell 命令的批处理脚本:

(Get-AppxPackage -Name "Microsoft.MicrosoftEdge.Stable").Version

仅在 Windows 10(32 位)

上测试
@echo off
Title Get Edge Version with Powershell and Batch
Set PassPSCMD="(Get-AppxPackage -Name "Microsoft.MicrosoftEdge.Stable").Version"
Set "LogFile=C:\Temp\Message.txt"
Call :RunPS %PassPSCMD% EdgeVersion
>"%LogFile%" (
    If defined EdgeVersion (
        echo Edge has a version : %EdgeVersion%
    ) else (
        echo Edge is not Installed !
    )
)
If Exist "%LogFile%" Start "" /MAX "%LogFile%"
Exit
REM -----------------------------------------------------------------------------
:: A function that would execute powershell script and return a value from it.
:: <RetValue> the returned value to be set like in this case we need Edge Version
:RunPS <PassPSCMD> <RetValue>
@for /F "usebackq tokens=*" %%i in (`Powershell -C %1`) do set "%2=%%i"
Goto:eof
:: End of :RunPS function
REM -----------------------------------------------------------------------------

或者在 Pure Batch 中使用 Reg Query 比以前的脚本更快

仅在 Windows 10(32 位)

上测试
@echo off
Title Get Edge Version with Reg Query in Pure Batch
Set "LogFile=C:\Temp\Message.txt"
Set "MyKey=HKEY_CURRENT_USER\SOFTWARE\Microsoft\Edge\BLBeacon"

@for /f "tokens=3 delims= " %%a in (
    'Reg Query "%MyKey%" 2^>nul ^| find /I "Version"'
) do (Set "EdgeVersion=%%a")

>"%LogFile%" (
    If defined EdgeVersion (
        echo Edge has a version : %EdgeVersion%
    ) else (
        echo Edge is not Installed !
    )
)
If Exist "%LogFile%" Start "" /MAX "%LogFile%" & Exit

如果您坚持使用 WMIC 试试这个批处理脚本:

@echo off
Title Get Edge Version with WMIC
Set "LogFile=C:\Temp\Message.txt"
SET "ARCH=x64" 
IF /I "%PROCESSOR_ARCHITECTURE%"=="x86" (IF NOT DEFINED PROCESSOR_ARCHITEW6432 SET "ARCH=x86")
If /I "%ARCH%"=="x86" ( set "PrgFiles=%ProgramFiles%" ) else ( set "PrgFiles=%ProgramFiles(x86)%" )
Set EdgePath=%PrgFiles%\Microsoft\Edge\Application\msedge.exe
echo( & echo Getting Version of "%EdgePath%"
Call :GetVersion "%EdgePath%" Version
echo EDGE Version : %Version%
echo EDGE Version : %Version%>>"%LogFile%"
Timeout /T 3 /NoBreak>nul
If Exist "%LogFile%" Start "" /MAX "%LogFile%" & Exit /B
::----------------------------------------------------------------------------------------
:GetVersion <App> <Version>
Rem The argument %~1 represent the full path of the application without the double quotes
Rem The argument %2 represent the variable to be set (in our case %2=Version)
Set "App=%~1"
Set "App=%App:\=\%"
FOR /F "tokens=2 delims==" %%I IN (
    'wmic datafile where "name='%App%'" get version /Value 2^>^nul'
) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
Exit /b
::----------------------------------------------------------------------------------------