query_wmi/lib.rs
1//! # query-wmi
2//!
3//! [](https://github.com/SubconsciousCompute/query-wmi/actions/workflows/rust.yml)
4//! [](https://crates.io/crates/query-wmi)
5//! [](https://docs.rs/query-wmi/latest/query_wmi/)
6//!
7//! A crate to query `WMI` classes in windows
8//!
9//! <https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-start-page>
10//!
11//! > Windows Management Instrumentation (WMI) is the infrastructure for management data and
12//! > operations on Windows-based operating systems. You can write WMI scripts or applications to
13//! > automate administrative tasks on remote computers, but WMI also supplies management data to
14//! > other parts of the operating system and products—for example, System Center Operations Manager
15//! > (formerly Microsoft Operations Manager (MOM)), or Windows Remote Management (`WinRM`).
16//!
17//! Usage:
18//!
19//! ```ignore
20//! use query_wmi::{COMLibrary, Variant, WMIConnection};
21//! use query_wmi::computer_hardware::{
22//! get_Win32_CDROMDrive, get_Win32_ComputerSystem,
23//! get_Win32_PCMCIAController, get_Win32_PnPEntity, get_Win32_Processor,
24//! get_Win32_SystemEnclosure, get_Win32_TapeDrive, get_Win32_USBHub,
25//! };
26//! use query_wmi::operating_systems::get_Win32_OperatingSystem;
27//!
28//! fn main() -> Result<(), Box<dyn std::error::Error>> {
29//! let com_con = COMLibrary::new()?;
30//! dbg!(get_Win32_OperatingSystem(com_con)?);
31//! dbg!(get_Win32_CDROMDrive(com_con)?);
32//! dbg!(get_Win32_ComputerSystem(com_con)?);
33//! dbg!(get_Win32_PCMCIAController(com_con)?);
34//! dbg!(get_Win32_PnPEntity(com_con)?);
35//! dbg!(get_Win32_Processor(com_con)?);
36//! dbg!(get_Win32_SystemEnclosure(com_con)?);
37//! dbg!(get_Win32_USBHub(com_con)?);
38//! dbg!(get_Win32_TapeDrive(com_con)?);
39//! Ok(())
40//! }
41//! ```
42//!
43//! ## Return type
44//!
45//! `type Query = Vec<HashMap<String, Variant>>`.
46//!
47//! `String` is the name of the returned struct field with [`Variant`](Variant) being an enum type.
48//!
49//! ## Currently included queries:
50//!
51//! The subsections were defined according
52//! to [WMI Tasks for Scripts and Applications](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks-for-scripts-and-applications),
53//! you can find more classes [here](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/operating-system-classes).
54//!
55//! [Accounts and Domains](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks--accounts-and-domains)
56//!
57//! - [`Win32_ComputerSystem`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-computersystem)
58//! - [`Win32_Group`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-group)
59//!
60//! [Computer Hardware](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks--computer-hardware)
61//!
62//! - [`Win32_CDROMDrive`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-cdromdrive)
63//! - [`Win32_ComputerSystem`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-computersystem)
64//! - [`Win32_PCMCIAController`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-pcmciacontroller)
65//! - [`Win32_PnPEntity`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-pnpentity)
66//! - [`Win32_PointingDevice`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-pointingdevice)
67//! - [`Win32_Processor`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-processor)
68//! - [`Win32_SystemEnclosure`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-systemenclosure)
69//! - [`Win32_USBHub`](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/cimwin32a/win32-usbhub)
70//! - [`Win32_TapeDrive`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-tapedrive)
71//!
72//! [Computer Software](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks--computer-software)
73//!
74//! - [`Win32_Product`](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/aa394378(v=vs.85))
75//!
76//! [Dates and Times](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks--dates-and-times)
77//!
78//! - [`Win32_LocalTime`](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/wmitimepprov/win32-localtime)
79//! - [`Win32_TimeZone`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-timezone)
80//!
81//! [Desktop Management](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks--desktop-management)
82//!
83//! - [`Win32_Desktop`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-desktop)
84//! - [`Win32_DesktopMonitor`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-desktopmonitor)
85//! - [`Win32_StartupCommand`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-startupcommand)
86//!
87//! [Disks and File Systems](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks--disks-and-file-systems)
88//!
89//! - [`Win32_DiskQuota`](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/wmipdskq/win32-diskquota)
90//! - [`Win32_VolumeChangeEvent`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-volumechangeevent)
91//! - [`Win32_LogicalDisk`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-logicaldisk)
92//! - [`Win32_MappedLogicalDisk`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-mappedlogicaldisk)
93//! - [`Win32_Volume`](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/aa394515(v=vs.85))
94//! - [`Win32_DiskDrive`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-diskdrive)
95//! - [`Win32_DiskPartition`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-diskpartition)
96//!
97//! [Event Logs](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks--event-logs)
98//!
99//! - [`Win32_NTLogEvent`](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/eventlogprov/win32-ntlogevent)
100//! - [`Win32_NTEventlogFile`](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/aa394225(v=vs.85))
101//!
102//! [Files and Folders](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks--files-and-folders)
103//!
104//! - [`CIM_DataFile`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/cim-datafile)
105//! - [`Win32_Share`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-share)
106//! - [`Win32_Directory`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-directory)
107//!
108//! [Networking](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks--networking)
109//!
110//! - [`Win32_NetworkAdapterConfiguration`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-networkadapterconfiguration)
111//! - [`Win32_NetworkAdapter`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-networkadapter)
112//!
113//! [Operating Systems](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks--operating-systems)
114//!
115//! - [`Win32_OperatingSystem`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-operatingsystem)
116//! - [`Win32_QuickFixEngineering`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-quickfixengineering)
117//! - [`Win32_WindowsProductActivation`](https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/aa394520(v=vs.85))
118//!
119//! [Performance Monitoring](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks--performance-monitoring)
120//!
121//! - [`Win32_Perf`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-perf)
122//! - [`Win32_PerfFormattedData`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-perfformatteddata)
123//! - [`Win32_PerfRawData`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-perfrawdata)
124//!
125//! [Processes](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks--processes)
126//!
127//! - [`Win32_Process`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-process)
128//! - [`Win32_ProcessStartup`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-processstartup)
129//!
130//! [Printers and Printing](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks--printers-and-printing)
131//!
132//! - [`Win32_Printer`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-printer)
133//!
134//! [Registry](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks--registry)
135//!
136//! - [`Win32_Registry`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-registry)
137//!
138//! [Scheduled Tasks](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks--scheduled-tasks)
139//!
140//! - [`Win32_ScheduledJob`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-scheduledjob)
141//!
142//! [Services](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks--services)
143//!
144//! - [`Win32_Service`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-service)
145//! - [`Win32_DependentService`](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-dependentservice)
146//!
147//! ## Building your own class queries
148//!
149//! You can use the provided [`wmi`](crate::wmi) macro to make your own queries:
150//!
151//! ```ignore
152//! #![allow(non_snake_case)]
153//!
154//! use query_wmi::wmi;
155//! use query_wmi::Query;
156//! use paste::paste;
157//! use std::collections::HashMap;
158//! use query_wmi::COMLibrary;
159//! use query_wmi::{Variant, WMIConnection};
160//!
161//! // this creates the function `get_CLASS_NAME()`
162//! wmi!{
163//! /// documentation
164//! CLASS_NAME, r"path_to_namespace"
165//! }
166//!
167//! // calling it
168//! let com_con = COMLibrary::new()?;
169//! dbg!(get_CLASS_NAME(com_con)?);
170//! ```
171//!
172//! ### Building your own queries
173//!
174//! You can also replace `CLASS_NAME` with a query like `CLASS_NAME where SOME_CONDITION=VALUE`
175//!
176//! See [WQL Operators](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wql-operators)
177
178pub mod accounts_and_domains;
179pub mod computer_hardware;
180pub mod computer_software;
181pub mod date_and_times;
182pub mod desktop_management;
183pub mod disks_and_file_systems;
184pub mod event_logs;
185pub mod files_and_folders;
186pub mod networking;
187pub mod operating_systems;
188pub mod performance_monitoring;
189pub mod printers_and_printing;
190pub mod processes;
191pub mod registry;
192pub mod scheduled_tasks;
193pub mod services;
194
195use std::collections::HashMap;
196
197pub use wmi::COMLibrary;
198pub use wmi::{Variant, WMIConnection};
199
200// https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmi-tasks-for-scripts-and-applications
201
202/// `type Query = Vec<HashMap<String, Variant>>`.
203///
204/// `String` is the name of the returned struct field with [`Variant`](Variant) being an enum type.
205pub type Query = Vec<HashMap<String, Variant>>;
206
207/// Our main macro to build queries.
208///
209/// The first argument takes documentation followed by WMI `class` name and it is followed by
210/// `Namespace` or where it is located.
211/// ## Building your own class queries
212///
213/// You can use the provided [`wmi`](crate::wmi) macro to make your own queries:
214///
215/// ```ignore
216/// #![allow(non_snake_case)]
217///
218/// use query_wmi::wmi;
219/// use query_wmi::Query;
220/// use paste::paste;
221/// use std::collections::HashMap;
222/// use query_wmi::COMLibrary;
223/// use query_wmi::{Variant, WMIConnection};
224///
225/// // this creates the function `get_CLASS_NAME()`
226/// wmi!{
227/// /// documentation
228/// CLASS_NAME, r"path_to_namespace"
229/// }
230///
231/// // calling it
232/// let com_con = COMLibrary::new()?;
233/// dbg!(get_CLASS_NAME(com_con)?);
234/// ```
235///
236/// ### Building your own queries
237///
238/// You can also replace `CLASS_NAME` with a query like `CLASS_NAME where SOME_CONDITION=VALUE`
239///
240/// See [WQL Operators](https://learn.microsoft.com/en-us/windows/win32/wmisdk/wql-operators)
241#[macro_export]
242macro_rules! wmi {
243 (
244 $(#[$attr:meta])*
245 $query: ident, $path: expr
246 ) => {
247 paste!{
248 $(#[$attr])*
249 pub fn [<get_ $query>](com_con: COMLibrary) -> Result<Query, Box<dyn std::error::Error>> {
250 let wmi_con = WMIConnection::with_namespace_path($path, com_con)?;
251
252 let results: Vec<HashMap<String, Variant>> =
253 wmi_con.raw_query(format!("SELECT * FROM {}", stringify!($query)))?;
254
255 Ok(results)
256 }
257 }
258 };
259}