-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductFromRegistry.cs
69 lines (55 loc) · 2.67 KB
/
ProductFromRegistry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
using Microsoft.Win32;
namespace ProductFromRegistry
{
[Cmdlet(VerbsCommon.Get,"SoftwareProductInstalled")]
[OutputType(typeof(SoftwareProductInstalled))]
public class GetProductRegistry : Cmdlet
{
[Parameter(Mandatory = true, Position = 0, HelpMessage = "Get Installed Software from Registry of local or remote computer")]
public string[] Computername { get; set; }
protected override void ProcessRecord()
{
var qry = GetSoft();
qry.ToList().ForEach(WriteObject);
}
public IEnumerable<SoftwareProductInstalled> GetSoft()
{
string DisplayVersion, InstallDate, ModDate;
foreach (string computer in Computername)
{
using (RegistryKey r = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, computer).OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"))
{
foreach (string subk in r.GetSubKeyNames())
{
using (RegistryKey rr = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, computer).OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall" + "\\" + subk))
{
if (rr.GetValue("DisplayName") != null)
{
DisplayVersion = rr.GetValue("DisplayVersion") == null ? "" : rr.GetValue("DisplayVersion").ToString();
InstallDate = rr.GetValue("InstallDate") == null ? "" : rr.GetValue("InstallDate").ToString();
ModDate = InstallDate.Length < 8 ? "" : (DateTime.ParseExact(InstallDate, "yyyyMMdd", null)).ToShortDateString();
yield return new SoftwareProductInstalled { DisplayName = rr.GetValue("DisplayName").ToString(), DisplayVersion = DisplayVersion, InstallDate = ModDate, RemoteServer = computer };
}
}
}
}
}
}
}
public class SoftwareProductInstalled
{
public string DisplayName { get; set; }
public string DisplayVersion { get; set; }
public string InstallDate { get; set; }
public string RemoteServer { get; set; }
/*public override string ToString()
{
return this.DisplayName + " " + this.DisplayVersion + " " ; }*/
}
}