首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WMI:获取已安装软件的列表

WMI:获取已安装软件的列表
EN

Stack Overflow用户
提问于 2014-08-05 12:51:21
回答 2查看 77.6K关注 0票数 12

我需要使用wmi调用获取远程Windows主机上已安装软件的列表。我尝试过使用Win32_ProductWin32Reg_AddRemovePrograms类。

使用Win32_Product的好处是,它可以显示机器上安装的所有软件,但它非常非常慢,并且不能在超过90%的主机上工作(给出错误,如- NTSTATUS: NT code 0xc002001b - NT code 0xc002001b)。另一方面,Win32Reg_AddRemovePrograms速度快得多,在大多数主机上都能很好地工作,但缺少很多软件。

有没有其他Win32类可以高效地做同样的事情呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-05 14:35:04

您可以使用wmic。

示例:

代码语言:javascript
复制
wmic product get name,version /format:csv

wmic /node:localhost /output:d:\programlist.htm product
get name,version /format:htable.xsl

wmic product get name,version

wmic softwareelement get name,version

wmic softwarefeature get name,version

wmic wmic:root\cli>/output:c:\ProgramList.txt product get name,version

或者你可以像“添加/删除程序”一样,读取所有卸载注册表项:

代码语言:javascript
复制
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall

在64位Windows上,请记住还要检查:

代码语言:javascript
复制
HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\

To返回计算机上安装的所有软件的列表,无论是否由Windows-Installer安装:

代码语言:javascript
复制
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE 
strComputer = "." 
strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" 
strEntry1a = "DisplayName" 
strEntry1b = "QuietDisplayName" 
strEntry2 = "InstallDate" 
strEntry3 = "VersionMajor" 
strEntry4 = "VersionMinor" 
strEntry5 = "EstimatedSize" 

Set objReg = GetObject("winmgmts://" & strComputer & _ 
 "/root/default:StdRegProv") 
objReg.EnumKey HKLM, strKey, arrSubkeys 
WScript.Echo "Installed Applications" & VbCrLf 
For Each strSubkey In arrSubkeys 
  intRet1 = objReg.GetStringValue(HKLM, strKey & strSubkey, _ 
   strEntry1a, strValue1) 
  If intRet1 <> 0 Then 
    objReg.GetStringValue HKLM, strKey & strSubkey, _ 
     strEntry1b, strValue1 
  End If 
  If strValue1 <> "" Then 
    WScript.Echo VbCrLf & "Display Name: " & strValue1 
  End If 
  objReg.GetStringValue HKLM, strKey & strSubkey, _ 
   strEntry2, strValue2 
  If strValue2 <> "" Then 
    WScript.Echo "Install Date: " & strValue2 
  End If 
  objReg.GetDWORDValue HKLM, strKey & strSubkey, _ 
   strEntry3, intValue3 
  objReg.GetDWORDValue HKLM, strKey & strSubkey, _ 
   strEntry4, intValue4 
  If intValue3 <> "" Then 
     WScript.Echo "Version: " & intValue3 & "." & intValue4 
  End If 
  objReg.GetDWORDValue HKLM, strKey & strSubkey, _ 
   strEntry5, intValue5 
  If intValue5 <> "" Then 
    WScript.Echo "Estimated Size: " & Round(intValue5/1024, 3) & " megabytes" 
  End If 
Next 
票数 17
EN

Stack Overflow用户

发布于 2017-07-12 22:47:21

c#中的编码版本,基于系统注册表窗口中的读数。

代码语言:javascript
复制
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


namespace SoftwareInventory
{
    class Program
    {
        static void Main(string[] args)
        {
            //!!!!! Must be launched with a domain administrator user!!!!!
            Console.ForegroundColor = ConsoleColor.Green;
            StringBuilder sbOutFile = new StringBuilder();
            Console.WriteLine("DisplayName;IdentifyingNumber");
            sbOutFile.AppendLine("Machine;DisplayName;Version");

            //Retrieve machine name from the file :File_In/collectionMachines.txt
            //string[] lines = new string[] { "NameMachine" };
            string[] lines = File.ReadAllLines(@"File_In/collectionMachines.txt");
            foreach (var machine in lines)
            {
                //Retrieve the list of installed programs for each extrapolated machine name
                var registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
                using (Microsoft.Win32.RegistryKey key = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machine).OpenSubKey(registry_key))
                {
                    foreach (string subkey_name in key.GetSubKeyNames())
                    {
                        using (RegistryKey subkey = key.OpenSubKey(subkey_name))
                        {
                            //Console.WriteLine(subkey.GetValue("DisplayName"));
                            //Console.WriteLine(subkey.GetValue("IdentifyingNumber"));
                            if (subkey.GetValue("DisplayName") != null)
                            {
                                Console.WriteLine(string.Format("{0};{1};{2}", machine, subkey.GetValue("DisplayName"), subkey.GetValue("Version")));
                                sbOutFile.AppendLine(string.Format("{0};{1};{2}", machine, subkey.GetValue("DisplayName"), subkey.GetValue("Version")));
                            }
                        }
                    }
                }
            }
            //CSV file creation
            var fileOutName = string.Format(@"File_Out\{0}_{1}.csv", "Software_Inventory", DateTime.Now.ToString("yyyy_MM_dd_HH_mmssfff"));
            using (var file = new System.IO.StreamWriter(fileOutName))
            {

                file.WriteLine(sbOutFile.ToString());
            }
            //Press enter to continue 
            Console.WriteLine("Press enter to continue !");
            Console.ReadLine();
        }


    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25131413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档