【问题标题】:WMIC output to text fileWMIC 输出到文本文件
【发布时间】:2021-10-21 12:45:12
【问题描述】:

我有一个脚本,可以在所有我映像的 pc 上运行,它基本上会显示并告诉我是否安装了程序,如果安装了,是什么版本。 当我在 CMD 窗口中运行命令时,它可以工作。 但作为我脚本的一部分,它会删除 txt 文件中的所有内容并添加奇怪的字符。 这是 Edge 代码。

: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???????????????????????????????????? ????????????????????????

我不知所措.....

【问题讨论】:

  • 如果您想通过检查注册表项来查找 Edge 版本,请参阅 How to detect installed Edge version,然后您可以尝试在以下位置进行检查。注册表路径:Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Edge\BLBeaconKey name: version

标签: output wmic


【解决方案1】:

您可以试试这个在一行中使用 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
::----------------------------------------------------------------------------------------

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多