-
Notifications
You must be signed in to change notification settings - Fork 868
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b8de4d
commit c499c39
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
| ||
using System.Management; | ||
using System.Runtime.Versioning; | ||
|
||
namespace Agent.Sdk.Util; | ||
|
||
[SupportedOSPlatform("windows")] | ||
public class WmiUtil | ||
{ | ||
public static Task<List<ManagementBaseObject>> QueryGet(string query, CancellationToken cancellationToken) | ||
{ | ||
var output = new List<ManagementBaseObject>(); | ||
var comletetionSource = new TaskCompletionSource<List<ManagementBaseObject>>(); | ||
|
||
var observer = new ManagementOperationObserver(); | ||
observer.ObjectReady += (sender, obj) => | ||
{ | ||
output.Add(obj.NewObject); | ||
}; | ||
observer.Completed += (sender, e) => | ||
{ | ||
switch (e.Status) | ||
{ | ||
case ManagementStatus.CallCanceled: | ||
comletetionSource.SetCanceled(cancellationToken); | ||
break; | ||
|
||
case ManagementStatus.NoError: | ||
comletetionSource.SetResult(output); | ||
break; | ||
|
||
default: | ||
comletetionSource.SetException(new Exception($"WMI Get Query failed with status {e.Status}")); | ||
break; | ||
} | ||
}; | ||
|
||
cancellationToken.Register(() => | ||
{ | ||
observer.Cancel(); | ||
}); | ||
|
||
using var searcher = new ManagementObjectSearcher(query); | ||
searcher.Get(observer); | ||
|
||
return comletetionSource.Task; | ||
} | ||
} |