Skip to content

Commit 0cfc1be

Browse files
committed
Update MachineMethods.cs
1 parent c435413 commit 0cfc1be

1 file changed

Lines changed: 83 additions & 13 deletions

File tree

WMIMethods/MachineMethods.cs

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,31 @@ namespace GetWMIBasic.WMIMethods
1414
/*
1515
* Machine Method class
1616
* It is used to connect and perform WMI tasks to local and remote machines through WMI
17+
*
18+
* Security Note:
19+
* 1. When connecting to remote machines, ensure that the credentials are handled securely.
20+
* 2. Use SecureString for passwords to enhance security.
21+
* 3. The application should run with appropriate permissions to access WMI on the target machines.
22+
* For example, locally it may require administrative privileges and do not run with external credentials.
1723
*/
1824
public class MachineMethods
1925
{
26+
27+
////////////////////////////////////////////////////////////////
28+
/// Global Properties and Constructors Region
29+
/// This region contains global properties and constructors
30+
/// for the MachineMethods class.
31+
////////////////////////////////////////////////////////////////
32+
2033
/*
21-
* Class properties
34+
* Global properties
35+
* userCredential: A tuple to store computer name, username, and password
36+
* credential: A ConnectionOptions object to store WMI connection options
37+
* scope: A ManagementScope object to define the WMI scope
38+
* searcher: A ManagementObjectSearcher object to perform WMI queries
39+
* scopePath: A string to store the WMI scope path
40+
* isLocal: A boolean to indicate whether the connection is local or remote
2241
*/
23-
2442
protected (string computerName, string username, SecureString password) userCredential;
2543
protected ConnectionOptions credential;
2644
protected ManagementScope scope;
@@ -30,15 +48,19 @@ public class MachineMethods
3048
protected bool isLocal;
3149

3250
/*
33-
* Machine Method
34-
* 1. Empty - Local
35-
* 2. Or Connect through WMI
51+
* Constructor for local connection
52+
* Note: Local connections do not require username and password.
53+
* It uses current user context.
3654
*/
37-
3855
public MachineMethods()
3956
{
57+
// Set default computer name to localhost
4058
userCredential.computerName = "localhost";
59+
60+
// Set local connection flag
4161
isLocal = true;
62+
63+
// Set default connection options for local connection
4264
credential = new ConnectionOptions
4365
{
4466
Impersonation = ImpersonationLevel.Impersonate,
@@ -47,12 +69,21 @@ public MachineMethods()
4769
};
4870
}
4971

72+
/*
73+
* Constructor for remote connection
74+
* Note: Remote connections require username and password.
75+
*/
5076
public MachineMethods((string computerName, string username, SecureString password) userCredential)
5177
{
78+
// Set user credentials for remote connection
5279
this.userCredential.computerName = userCredential.computerName;
5380
this.userCredential.username = userCredential.username;
5481
this.userCredential.password = userCredential.password;
82+
83+
// Set remote connection flag
5584
isLocal = false;
85+
86+
// Set connection options for remote connection
5687
credential = new ConnectionOptions
5788
{
5889
Username = userCredential.username,
@@ -63,47 +94,86 @@ public MachineMethods((string computerName, string username, SecureString passwo
6394
};
6495
}
6596

97+
////////////////////////////////////////////////////////////////
98+
/// Connect to a name space region
99+
/// This region contains methods to connect to a WMI name space.
100+
/// For example, "root\\cimv2".
101+
////////////////////////////////////////////////////////////////
102+
103+
public void Connect(string nameSpace)
104+
{
105+
// Set the scope path based on local or remote connection
106+
scopePath = $"\\\\{userCredential.computerName}\\{nameSpace}";
107+
108+
// Create the ManagementScope object and connect
109+
scope = (isLocal) ? new ManagementScope(scopePath) : new ManagementScope(scopePath, credential);
110+
111+
// Connect to the WMI scope
112+
scope.Connect();
113+
}
114+
115+
////////////////////////////////////////////////////////////////
116+
/// Get methods region (connection setting, not the WMI data)
117+
/// Various get methods to retrieve information about the machine
118+
/// For example, computer name, current user name, etc.
119+
////////////////////////////////////////////////////////////////
120+
66121
/*
67-
* Get value
122+
* Get methods for computer name
68123
*/
69124
public string GetComputerName()
70125
{
71126
return userCredential.computerName;
72127
}
73128

74129
/*
75-
* Connection method
130+
* Get methods for current user name
76131
*/
77-
78-
public void Connect(string nameSpace)
132+
public string GetUsername()
79133
{
80-
scopePath = $"\\\\{userCredential.computerName}\\{nameSpace}";
81-
scope = (isLocal) ? new ManagementScope(scopePath) : new ManagementScope(scopePath, credential);
82-
scope.Connect();
134+
return userCredential.username;
83135
}
84136

137+
////////////////////////////////////////////////////////////////
138+
/// Get methods region (for WMI data)
139+
////////////////////////////////////////////////////////////////
140+
85141
/*
86142
* Get WMI objects
87143
*/
88144
public async Task<ManagementObjectCollection> GetObjects(string className, string fields)
89145
{
146+
// Create the WMI query
90147
ObjectQuery query = new ObjectQuery($"SELECT {fields} FROM {className}");
148+
149+
// Create the ManagementObjectSearcher object
91150
searcher = new ManagementObjectSearcher(scope, query);
151+
152+
// Execute the query and return the results asynchronously
92153
return await Task.Run(() => searcher?.Get());
93154
}
94155

156+
////////////////////////////////////////////////////////////////
157+
/// Set methods region (for WMI object)
158+
////////////////////////////////////////////////////////////////
159+
95160
/*
96161
* Call WMI Method
97162
*/
98163
public async Task CallMethod(string className, string fields, string methodName, object[] args)
99164
{
165+
// Create the WMI query
100166
ObjectQuery query = new ObjectQuery($"SELECT {fields} FROM {className}");
167+
// Execute the method asynchronously
101168
await Task.Run(() =>
102169
{
170+
// Create the ManagementObjectSearcher object
103171
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
104172
{
173+
// Invoke the method on each object returned by the query
105174
foreach (ManagementObject manageObject in searcher.Get().Cast<ManagementObject>())
106175
{
176+
// Invoke the specified method with arguments
107177
_ = manageObject.InvokeMethod(methodName, args);
108178
}
109179
}

0 commit comments

Comments
 (0)