批处理文件以获取特定安装的软件以及版本

Joh*_*Zaj 3 batch-file

我有一个脚本找到特定安装的软件,但我也无法获得该软件的版本.例如,假设我收到了所有安装的Microsoft软件的列表.这是我到目前为止:

echo software installed > software_list.txt
echo ================= >>software_list.txt
reg export HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall temp1.txt
find "Microsoft" temp1.txt| find "DisplayName" > temp2.txt
for /f "tokens=2,3 delims==" %%a in (temp2.txt) do (echo %%a >> software_list.txt)

start notepad "software_list.txt"

del temp1.txt temp2.txt
Run Code Online (Sandbox Code Playgroud)

如何从reg导出中获取DisplayVersion?如果我用DisplayVersion替换DisplayName,甚至找不到任何东西.或者,我应该在这里采取另一种途径吗?

Hel*_*len 13

更换DisplayNameDisplayVersion在,因为这条线的工作方式一个空的输出结果:

find "Microsoft" temp1.txt| find "DisplayName" > temp2.txt
Run Code Online (Sandbox Code Playgroud)

这一行的作用是找到temp2.txt文件中包含MicrosoftDisplayName子字符串的所有行(即,它找到名称中包含Microsoft的产品).具有DisplayVersion的行,包含产品版本号,不包含Microsoft,这就是为什么你得到空输出.

我可以建议一些使用WMI的替代解决方案:

  1. HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall使用脚本(VBScript,PowerShell等)而不是批处理文件解析子键,因为脚本语言为文本操作提供了更好的支持.这是一个VBScript示例,它输出已安装的Microsoft产品的名称和版本(名称包含Microsoft的产品,更准确):

    On Error Resume Next
    
    Const strComputer = "."
    Const HKLM        = &H80000002
    Const strKeyPath  = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    
    Dim oReg, arrSubKeys, strProduct, strDisplayName, strVersion
    
    Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
         strComputer & "\root\default:StdRegProv")
    
    ' Enumerate the subkeys of the Uninstall key
    oReg.EnumKey HKLM, strKeyPath, arrSubKeys
    For Each strProduct In arrSubKeys
      ' Get the product's display name
      oReg.GetStringValue HKLM, strKeyPath & "\" & strProduct, "DisplayName", strDisplayName
      ' Process only products whose name contain 'Microsoft'
      If InStr(1, strDisplayName, "Microsoft", vbTextCompare) > 0 Then
        ' Get the product's display version
        oReg.GetStringValue HKLM, strKeyPath & "\" & strProduct, "DisplayVersion", strVersion
        WScript.Echo strDisplayName & vbTab & strVersion
      End If
    Next
    
    Run Code Online (Sandbox Code Playgroud)

    用法:

    cscript //nologo productlist.vbs
    cscript //nologo productlist.vbs > productlist.txt
  2. 如果您感兴趣的软件是由Windows Installer安装的,则可以通过查询WMI Win32_Product类获取有关该软件的信息(例如名称,供应商,版本等).该wmic实用程序允许您直接从命令行和批处理文件执行此操作.以下是一些例子:

    要将wmic输出保存到文件,可以使用/output和(可选)/format参数,例如:

    wmic /output:software.txt product get Name, Version
    wmic /output:software.htm product get Name, Version /format:htable
    
    Run Code Online (Sandbox Code Playgroud)

    有关wmic语法的详细信息,请参阅wmic /?