Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Added WMI util #4998

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/Agent.Sdk/Util/WmiUtil.cs
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;
}
}
Loading