-
Notifications
You must be signed in to change notification settings - Fork 869
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 cf37f00
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 completionSource = 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: | ||
completionSource.SetCanceled(cancellationToken); | ||
break; | ||
case ManagementStatus.NoError: | ||
completionSource.SetResult(output); | ||
break; | ||
default: | ||
completionSource.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 completionSource.Task; | ||
} | ||
} |