windows WMI:获取已安装软件的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25131413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WMI: Get the list of Installed Softwares
提问by Mandar Shinde
I need to get the list of installed softwares on remote Windows hosts using wmi
calls. I have tried using Win32_Product
and Win32Reg_AddRemovePrograms
Classes.
我需要使用wmi
调用获取远程 Windows 主机上已安装软件的列表。我试过使用Win32_Product
和Win32Reg_AddRemovePrograms
类。
Advantage of using Win32_Product
is that, it displays all the softwares installed on the machine, but it is very very slow and does not work on more than 90% hosts (giving errors like- NTSTATUS: NT code 0xc002001b - NT code 0xc002001b
). On the other hand, Win32Reg_AddRemovePrograms
is much quicker and works pretty well on most of the hosts, but misses plenty of softwares.
使用的好处Win32_Product
是,它显示了机器上安装的所有软件,但它非常非常慢,并且在超过 90% 的主机上不起作用(给出类似 - 的错误NTSTATUS: NT code 0xc002001b - NT code 0xc002001b
)。另一方面,Win32Reg_AddRemovePrograms
在大多数主机上运行速度更快并且运行良好,但缺少大量软件。
Is there any other Win32 Class that could do the same efficiently?
有没有其他 Win32 类可以有效地做同样的事情?
回答by Stefan Steiger
You can use wmic.
您可以使用 wmic。
Example:
例子:
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
Or you can do it like the "Add/Remove Programs", reading all uninstall registry keys:
或者您可以像“添加/删除程序”一样执行此操作,读取所有卸载注册表项:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall
On 64 bit Windows, remember to also check:
在 64 位 Windows 上,记得还要检查:
HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\
To returns a list of all software installed on a computer, whether or not by Windows-Installer:
要返回计算机上安装的所有软件的列表,无论是否由 Windows 安装程序:
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
回答by Domenico Zinzi
The encoded version in c# that is based on the readings in the system registry windows.
c# 中的编码版本基于系统注册表窗口中的读数。
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();
}
}
}