output - WMIC 输出到文本文件

标签 output wmic

我有一个在我镜像的所有电脑上运行的脚本,它基本上看起来并告诉我是否安装了程序,如果安装了,是什么版本。 当我在 CMD 窗口中运行命令时,它可以工作。 但作为我的脚本的一部分,它删除了 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
::----------------------------------------------------------------------------------------

关于output - WMIC 输出到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69662228/

相关文章:

r - 如何将控制台输出存储到 R 中的变量

java - 在 Ubuntu 上为 VS Code 设置 java

python - 使用Python在远程主机中创建目录

python - WMIC + Python 子进程/(Windows 10)

go - 通过 GUID 卸载应用程序

c++ - 对齐列输出 C++

转到目标语言

java - 根据条件要求,用户输入的错误输出

wmi - WMIC磁盘驱动器获取序列号->无效的XML输出

windows - 确定用于映射网络驱动器的域和用户名