2009-09-27 65 views
2

我有一個腳本可以找到特定的已安裝軟件,但我也無法獲取軟件的版本。例如,假設我收到了所有安裝的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?如果我將DisplayName替換爲DisplayVersion,則甚至找不到任何內容。或者,我還有另外一個途徑嗎?

回答

12

的,因爲這條線的工作方式在一個空的輸出更換DisplayNameDisplayVersion結果:

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

什麼這行做的是找到在temp2.txt文件中同時包含微軟所有行DisplayName子字符串(也就是說,它查找名稱包含Microsoft)的產品。 DisplayVersion這些行依次包含產品版本號,並且不包含單詞Microsoft,這就是爲什麼您會得到空輸出。

我可以建議一對夫婦使用WMI替代解決方案:

  1. 使用腳本(VBScript中,PowerShell的等),而不是一個批處理文件解析HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall子項,因爲腳本語言提供更好的支持文本操作。下面是輸出安裝的Microsoft產品的名稱和版本VBScript示例(產品名稱中包含微軟,更精確地說):

    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 Win32_Product類獲得有關該軟件的信息(如名稱,供應商,版本等)。 wmic實用程序可讓您直接從命令行和批處理文件執行此操作。我這裏還有一些例子:

    • 打印的姓名和所安裝的軟件版本:

      wmic product get Name, Version 
      
    • 列出所有安裝的Microsoft產品:

      wmic product where "Vendor like '%Microsoft%'" get Name, Version 
      
    • 列出已安裝的產品,有辦公室名稱:

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

+0

有沒有辦法讓Window Installer安裝的程序的安裝路徑?我的程序是一個Interop Excel插件,只包含.dlls並且沒有可執行文件.exe。看到我的添加/刪除程序和_wmic產品獲取名稱,版本_列表,但不是通過命令_where_ – Kenny

+0

@Kenny:嘗試'InstallLocation'屬性,例如'wmic獲取名稱,InstallLocation'。 – Helen

+0

不幸的是InstallLocation是空的。由於類似的屬性列表,我感覺_wmic_從_HKLM \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall_獲取信息。你也可以從這個註冊表項看到許多有_InstallLocation_missing。 – Kenny

2

[可恥的複製/粘貼@海倫的回答從這裏開始]

如果你感興趣的是Windows安裝程序安裝的軟件,就可以得到這些軟件的信息(如姓名,供應商,版本等)通過查詢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輸出保存到一個文件,你可以使用/輸出和/或/格式參數,例如:

WMIC /output:software.txt產品獲得名稱,版本 wmic /output:software.htm產品名稱,版本/格式:htable

有關wmic語法的更多信息,請參見wmic /?

[從@Helen恬不知恥地複製/粘貼答案的結束在這裏結束。]

如果Windows安裝程序安裝的軟件wasnt」,而不是在註冊表中查找,你可以看看在自己的前男友。你需要的東西超出一個純粹的.bat文件。你需要一些可以打開exes並提取版本信息的東西。

我會看看PowerShell,它是.bat文件的Windows後繼。使用System.Diagnostics.FileVersionInfo.GetVersionInfo獲取版本。

0

超越PowerShell的另一種可能性(這是一個很好的做法)是使用帶有JScript或VBScript的WMI來訪問軟件存儲。

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 
相關問題