This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
get_hotfix.cs
170 lines (145 loc) · 5.35 KB
/
get_hotfix.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// To Compile:
// C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe /t:exe /out:get_hotfix.exe get_hotfix.cs
using System;
using System.Management;
class GetHotFix
{
public static int PrintUsage()
{
Console.WriteLine(@"List all patches on a system
USAGE:
get-hotfix [/S system] [/U [domain\]username /P password] [/V] [/?]
/V Print additional information (Description, InstalledBy, InstalledOn)
");
return 0;
}
public static int Main(string[] args)
{
string system = ".";
string username = "";
string password = "";
bool verbose_set = false;
try
{
// Parse arguments
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
switch (arg.ToUpper())
{
case "-S":
case "/S":
i++;
try
{
system = args[i].Trim(new Char[] { '\\', ' ' });
}
catch (IndexOutOfRangeException)
{
throw new ArgumentException("No system specified");
}
break;
case "-U":
case "/U":
i++;
try
{
username = args[i];
}
catch (IndexOutOfRangeException)
{
throw new ArgumentException("No username specified");
}
break;
case "-P":
case "/P":
i++;
try
{
password = args[i];
}
catch (IndexOutOfRangeException)
{
throw new ArgumentException("No password specified");
}
break;
case "-V":
case "/V":
verbose_set = true;
break;
case "/?":
default:
return PrintUsage();
}
}
ConnectionOptions conn_opts = new ConnectionOptions();
// Apply username and password if specified
if (username.Length > 0 && password.Length > 0)
{
conn_opts.Username = username;
conn_opts.Password = password;
}
else if (username.Length > 0 || password.Length > 0)
{
// Error out if username or password were specified, but not both
throw new ArgumentException("Please specify username and password");
}
ManagementScope scope = new ManagementScope("\\\\" + system + "\\root\\cimv2", conn_opts);
string[] selectedProperties;
if (verbose_set)
{
selectedProperties = new string[] { "HotFixID", "Description", "InstalledBy", "InstalledOn" };
}
else
{
selectedProperties = new string[] { "HotFixID" };
}
SelectQuery query = new SelectQuery("Win32_QuickFixEngineering", null, selectedProperties);
// Execute query within scope and iterate through results
using (var searcher = new ManagementObjectSearcher(scope, query))
{
string line = "\n";
// Print a column header for each property
foreach (string prop in selectedProperties)
{
line += prop.PadRight(22, ' ');
}
Console.WriteLine(line);
line = "";
// Dynamically generate underlines based on the list of properties
foreach (string prop in selectedProperties)
{
line += new String('-', prop.Length).PadRight(22, ' ');
}
Console.WriteLine(line);
// Print the hotfix info
foreach (ManagementObject proc in searcher.Get())
{
if (proc != null)
{
line = "";
foreach (string prop in selectedProperties)
{
string val = proc.GetPropertyValue(prop).ToString();
if (val != null)
{
line += val.PadRight(22, ' ');
}
}
Console.WriteLine(line);
}
}
}
return 0;
}
catch (Exception e)
{
Console.Error.WriteLine("ERROR: {0}", e.Message.Trim());
return 1;
}
finally
{
Console.WriteLine("\nDONE");
}
}
}