公司的IT工程师某一天找我聊天,说有没有一个程序能够统计电脑上安装的软件,公司采用的是域控,然后把这个软件放在域控的组策略里面,设置一番,只要登录域控的时候自动运行一遍,然后把采集的信息写入共享目录,这样就不用一台一台的统计了。
当时一想就直接用C#写了一个控制台程序。代码如下:
static void Main(string[] args) { string path = @"\192.168.10.251\hard_info\"; StringBuilder proinfo = new StringBuilder(); string uninstallKey = @"SOFTWAREMicrosoftWindowsCurrentVersionUninstall"; using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey)) { foreach (string skName in rk.GetSubKeyNames()) { using (RegistryKey sk = rk.OpenSubKey(skName)) { try { var displayName = sk.GetValue("DisplayName"); if (displayName != null) { proinfo.AppendLine(displayName.ToString()); } } catch { } } } } File.WriteAllText(path + Dns.GetHostName() + ".txt", proinfo.ToString()); Environment.Exit(0); }
运行以后,是能统计出来,但是少了很多软件,只有一部分,网上查询一番,发现要区分64位操作系统和32位系统,还要区分是LocalMachine还是CurrentUser
针对x86系统
HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionUninstall
HKEY_CURRENT_USERSOFTWAREMicrosoftWindowsCurrentVersionUninstall
针对x64系统
HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionUninstall
HKEY_CURRENT_USERSOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionUninstall
根据以上,改进以后代码如下:
static void Main(string[] args) { string path = @"\192.168.10.251\hard_info\"; StringBuilder proinfo = new StringBuilder(); proinfo = ShowAllSoftwaresName(); File.WriteAllText(path + Dns.GetHostName() + ".txt", proinfo.ToString()); Environment.Exit(0); } public static StringBuilder DisplayInstalledApps(RegistryKey key) { StringBuilder result = new StringBuilder(); string displayName; if (key != null) { foreach (String keyName in key.GetSubKeyNames()) { RegistryKey subkey = key.OpenSubKey(keyName); displayName = subkey.GetValue("DisplayName") as string; if (!string.IsNullOrEmpty(displayName)) { result.AppendLine(displayName); } } } return result; } public static StringBuilder ShowAllSoftwaresName() { StringBuilder proinfo = new StringBuilder(); RegistryKey key; // search in: CurrentUser key = Registry.CurrentUser.OpenSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionUninstall"); proinfo.Append(DisplayInstalledApps(key)); // search in: LocalMachine_32 key = Registry.LocalMachine.OpenSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionUninstall"); proinfo.Append(DisplayInstalledApps(key)); // search in: CurrentUser_64 key = Registry.CurrentUser.OpenSubKey(@"SOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionUninstall"); proinfo.Append(DisplayInstalledApps(key)); // search in: LocalMachine_64 key = Registry.LocalMachine.OpenSubKey(@"SOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionUninstall"); proinfo.Append(DisplayInstalledApps(key)); return proinfo; }
运行以后,全部采集了电脑安装的软件。
发给IT工程师后,反馈:有部分电脑运行不了。。。xp系统不支持。。。系统默认没安装framework
最后和IT工程师商议后,可以使用VBS,以前也写过VBS,网上找找资料就写了一个,完美解决了。
附件为vbs脚本:vbs获取软件列表.zip
参考资料: