Created
July 6, 2025 06:10
-
-
Save opentechnologist/da9bab0a22e5426d593af54f02d16f58 to your computer and use it in GitHub Desktop.
a sample C# project that retrieves workstation information via WMI.
This file contains hidden or 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
| using System; | |
| using System.Collections.Generic; | |
| using WMI; | |
| namespace MainPackage | |
| { | |
| public class MainClass | |
| { | |
| public static void Main(string[] args) | |
| { | |
| RunQuery( | |
| "SELECT Vendor, Name, Version, IdentifyingNumber, UUID FROM Win32_ComputerSystemProduct", | |
| new List<string> (new[] {"Vendor", "Name", "Version", "IdentifyingNumber", "UUID"}) | |
| ); | |
| RunQuery( | |
| "SELECT Model, SerialNumber FROM Win32_DiskDrive WHERE MediaLoaded = True", | |
| new List<string> (new[] {"Model", "SerialNumber"}) | |
| ); | |
| RunQuery( | |
| "SELECT Name, NetConnectionID, MACAddress FROM Win32_NetworkAdapter WHERE PhysicalAdapter = True AND MACAddress != Null", | |
| new List<string> (new[] {"Name", "NetConnectionID", "MACAddress"}) | |
| ); | |
| } | |
| static void RunQuery(string wqlQuery, List<string> propertyFilters = null) | |
| { | |
| try | |
| { | |
| Console.WriteLine(wqlQuery); | |
| WmiQueryService wmiService = new WmiQueryService(); | |
| List<Dictionary<string, object>> queryResult = wmiService.ExecuteQuery(wqlQuery); | |
| if (queryResult.Count == 0) | |
| { | |
| Console.WriteLine("\tNo Results."); | |
| return; | |
| } | |
| foreach (Dictionary<string, object> wmiObject in queryResult) | |
| { | |
| FilterProperties(wmiObject, propertyFilters); | |
| Console.WriteLine(); | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| if (ex.InnerException != null) | |
| { | |
| Console.WriteLine(ex.InnerException.Message); | |
| } | |
| else | |
| { | |
| Console.WriteLine(ex.Message); | |
| } | |
| } | |
| } | |
| static void FilterProperties(Dictionary<string, object> wmiObject, List<string> propertyFilters) | |
| { | |
| if (propertyFilters == null || propertyFilters.Count == 0) | |
| { | |
| foreach (KeyValuePair<string, object> keyValue in wmiObject) | |
| { | |
| Console.WriteLine("\t{0}: {1}", keyValue.Key.Trim(), keyValue.Value); | |
| } | |
| return; | |
| } | |
| foreach (string property in propertyFilters) | |
| { | |
| object propertyValue; | |
| if (wmiObject.TryGetValue(property.Trim(), out propertyValue)) | |
| { | |
| Console.WriteLine("\t{0}: {1}", property.Trim(), propertyValue); | |
| } | |
| else | |
| { | |
| Console.WriteLine("\t{0}: ** MISSING **", property.Trim()); | |
| } | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or 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
| <?xml version="1.0" encoding="utf-8"?> | |
| <Project | |
| xmlns="http://schemas.microsoft.com/developer/msbuild/2003" | |
| Sdk="Microsoft.NET.Sdk" | |
| DefaultTargets="Build" | |
| > | |
| <PropertyGroup> | |
| <AssemblyName>main</AssemblyName> | |
| <OutputPath>bin\</OutputPath> | |
| </PropertyGroup> | |
| <ItemGroup> | |
| <Compile Include="main.cs" /> | |
| <Compile Include="WMI.cs" /> | |
| </ItemGroup> | |
| <Target Name="Build" Inputs="@(Compile)" Outputs="$(OutputPath)$(AssemblyName).exe"> | |
| <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" /> | |
| <Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe"> | |
| <Output TaskParameter="OutputAssembly" ItemName="OutputFile" /> | |
| </Csc> | |
| <Message Text="Generated file: @(OutputFile)" Condition="Exists('@(OutputFile)')" /> | |
| </Target> | |
| <Target Name="Clean" > | |
| <Delete Files="$(OutputPath)$(AssemblyName).exe" /> | |
| </Target> | |
| <Target Name="Rebuild" DependsOnTargets="Clean;Build" /> | |
| </Project> |
This file contains hidden or 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
| using System; | |
| using System.Collections.Generic; | |
| using System.Management; | |
| /* | |
| https://learn.microsoft.com/windows/win32/wmisdk/wmi-glossary/ | |
| https://learn.microsoft.com/windows/win32/wmisdk/wmi-reference/ | |
| */ | |
| namespace WMI | |
| { | |
| public class WmiQueryService | |
| { | |
| public List<Dictionary<string, object>> ExecuteQuery(string wqlQuery) | |
| { | |
| List<Dictionary<string, object>> queryResults = new List<Dictionary<string, object>>(); | |
| try | |
| { | |
| ManagementObjectSearcher searcher = new ManagementObjectSearcher(wqlQuery); | |
| foreach (ManagementObject wmiObject in searcher.Get()) | |
| { | |
| Dictionary<string, object> propertyDictionary = new Dictionary<string, object>(); | |
| foreach (PropertyData property in wmiObject.Properties) | |
| { | |
| propertyDictionary[property.Name] = property.Value; | |
| } | |
| queryResults.Add(propertyDictionary); | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| throw new Exception("An error occurred while executing the query.", ex); | |
| } | |
| return queryResults; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment