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

时间:2009-09-27 03:43:33

标签: 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

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

4 个答案:

答案 0 :(得分:13)

使用DisplayName替换DisplayVersion会导致输出为空,因为此行有效:

find "Microsoft" temp1.txt| find "DisplayName" > temp2.txt

这一行的作用是找到temp2.txt文件中包含 Microsoft DisplayName 子串的所有行(也就是说,它找到了产品其名称包含 Microsoft )。显示 DisplayVersion 的行包含产品版本号,不包含单词 Microsoft ,这就是您输出空的原因。

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

  1. 使用脚本(VBScript,PowerShell等)而不是批处理文件解析HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall个子项,因为脚本语言为文本操作提供了更好的支持。这是一个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
    

    用法:

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

    • 打印已安装软件的名称和版本:

      wmic product get Name, Version
      
    • 列出所有已安装的Microsoft产品:

      wmic product where "Vendor like '%Microsoft%'" get Name, Version
      
    • 列出已在其名称中包含 Office 的已安装产品:

      wmic product where "Name like '%Office%'" get Name, Version
      

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

    wmic /output:software.txt product get Name, Version
    wmic /output:software.htm product get Name, Version /format:htable
    

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

答案 1 :(得分:2)

[无耻地复制/粘贴@ Helen的回答从这里开始]

如果您感兴趣的软件是由Windows Installer安装的,则可以通过查询WMI Win32_Product类获取有关该软件的信息(例如名称,供应商,版本等)。在批处理文件中,可以使用WMI命令行实用程序wmic来完成此操作。以下是一些例子:

*

  Print the names and versions of installed software:

  wmic product get Name, Version

*

  List all installed Microsoft products:

  wmic product where "Vendor like '%Microsoft%'" get Name, Version

*

  List installed products that have Office in their names:

  wmic product where "Name like '%Office%'" get Name, Version

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

wmic /output:software.txt产品获取名称,版本 wmic /output:software.htm product获取名称,版本/格式:htable

有关wmic语法的更多信息,请参阅wmic /?

[来自@Helen的无耻复制/粘贴答案的结尾在这里结束。]

如果Windows安装程序没有安装该软件,而是查看注册表,您可以查看exes本身。你需要的东西不仅仅是一个.bat文件。你需要能够打开exes并提取版本信息的东西。

我会看看PowerShell,它是.bat文件的Windows后继者。使用System.Diagnostics.FileVersionInfo.GetVersionInfo获取版本。

答案 2 :(得分:1)

类似的脚本适用于各种计算机"数组"

On Error Resume Next

    Const wbemFlagReturnImmediately = &h10
    Const wbemFlagForwardOnly = &h20
    'What Programm to look for
    Const strProgram = "Microsoft"

    arrComputers = Array("NAME1","Name2")
    For Each strComputer In arrComputers
       WScript.Echo
       WScript.Echo "=========================================="
       WScript.Echo "Computer: " & strComputer
       WScript.Echo "=========================================="

       Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
       Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Product WHERE Name like '%" & strProgram & "%'")

       For Each objItem In colItems
          WScript.Echo "Name: " & objItem.Name & ";" & "Version: " & objItem.Version
    Next
Next

答案 3 :(得分:0)

PowerShell之外的另一种可能性(这是一个很好的)是使用WMI和JScript或VBScript来访问软件商店。