Online-Admin.com |
 |
Menu |
 |
 |
Quick search |
 |
 |
New version notify |
 |
|
 |
TWmiQuery component |
 |
|
TWmiQuery is a part of
WmiSet Component Collection for Delphi, C++Builder.
This component executes queries against Windows Management
Instrumentation (WMI) service of Windows operating system.
|
|
SQL for WMI
|
|
WMI is a mainstream technology that currently dominates the area
of Windows OS management. WMI exposes much wider set of information
than it is possible to retrieve via Windows API.
WMI utilizes a special query language, called WQL. WQL is a SQL-like
language that allows to express queries in text form. For example, to
retrieve list of account on a computer, one could issue a query
"SELECT * FROM Win32_Account". Find the full description of
WQL language on
Microsoft Web site.
The result of WQL query is a set of WMI objects. These objects are exposed via
their COM interfaces that may be used to retrieve properties of objects.
|
|
TWmiQuery simplifies development
|
TWmiQuery VCL component executes WQL queries and exposes properties of retrieved
objects as fields. TWmiQuery is a descendant of TDataSet. If you ever developed
database applications in Delphi or C++Builder, you already know how to use
TWmiQuery. This snippet of Delphi code is familiar to many:
WmiQuery1.WQL.Text := 'SELECT * '+
'FROM Win32_LogicalDisk '+
'WHERE FreeSpace > 1000000';
WmiQuery1.Active := true;
while not WmiQuery1.EOF do
begin
//
// do processing here...
//
WmiQuery1.Next;
end;
The component can also update properties of the objects. Please note that
most of properties of WMI objects are read-only. WMI does not throw errors if
the read-only property is updated. When reloading the objects with read-only
properties, the property values will rollback to their old values. Here is the example
of changing the description of the local computer:
WmiQry1.WQL.Add('select * from Win32_OperatingSystem');
WmiQry1.ReadOnly := false;
WmiQry1.Open;
WmiQry1.Edit;
WmiQry1.FieldByName('Description').AsString :=
'Mail Server - Backup';
WmiQry1.Post;
|
|
TWmiQuery features/restrictions
|
- Execute regular WQL queries, like "SELECT * FROM Win32_DiskDrive";
Use WQL property.
- Connect to network computers via TWmiConnection component;
Use Connection property.
- Browse retrieved data set.
Use Next, Prev, First, Last methods.
- Read properties of retrieved WMI objects.
For each property in the WMI object the component will
create a field with the same name.
Use Fields property, FieldByName method.
- Retrieve WMI object as a whole. Use CurrentObject property.
- Search records in dataset. Use Locate methods.
- Work with irregular dataset, where each object in the dataset has
different set of properties.
Use IrregularView, IrregularFieldDefs, IrregularFieldValues
properties.
- Declare and jump to bookmarks.
Use GetBookmark, GotoBookmark methods.
- Change fields of objects with Edit and Post methods.
- Key fields are always included in the result set, even when not asked for;
- Filtering is not supported.
- The returned data set is not ordered. The order may change from query to query.
|
|
TWmiQuery Demo
|
WmiSet component collection comes with demo application that shows
how to use TWmiQuery with data controls. You may use this
application to test your WQL statements. The application is capable
of connecting to computers on the network. This may be useful when
testing compatibility with different operating systems.
Download WmiSet to
see the source code of the demo application. You may also
download executable here.
|
|
Useful WQL queries
|
We provide a number of common WQL queries here. You can use these
queries as is, or customize then at will. If you need to write a
new query but you are not experienced with WQL, contact technical
support. We will be happy to help.
 |
WQL |
Purpose |
 |
SELECT *
from Win32_NTLogEvent
where Logfile = "Application"
|
Returns events from event log, section "Application"
|
SELECT *
FROM Win32_Account
|
Returns all accounts known to the computer. This includes generic accounts,
like "Guest" and user accounts
|
SELECT *
FROM Win32_Group
|
Returns information about all the user groups known to the
destination computer
|
SELECT *
FROM Win32_LogicalDisk
WHERE FreeSpace > 10000000
|
Returns list of logical disks with free space greater than 10 Mb.
|
SELECT *
FROM Win32_Service
WHERE state = "Running"
|
Returns list of all running services.
|
ASSOCIATORS OF
{Win32_Group.Domain="domain", Name="user"}
WHERE ResultClass = Win32_Group
|
Lists all the groups that user domain\user is a member of.
|
ASSOCIATORS OF
{Win32_Group.Domain="domain", Name="group"}
WHERE
ResultClass = Win32_UserAccount
|
Lists all the users that are members of a group domain\group.
Note: if the group that you are interested in contains both user
and group accounts, this query becomes extremely slow. Instead
you may execute the following query
|
SELECT *
FROM Win32_GroupUser
WHERE GroupComponent = "Win32_Group.Domain='domain',
Name='group'"
|
Lists all the users that are members of a group domain\group.
Note: This query does not return the WMI objects. Instead, it returns
the string references to the destination objects that may be parsed
to retrieve the members' names.
|
SELECT *
FROM Win32_DiskPartition
WHERE DiskIndex = 0
|
Lists all the partitions of the first hard drive.
|
SELECT EstimatedRunTime
FROM Win32_Battery
|
When computer is running on batteries, returns estimated run time, in minutes.
|
SELECT * from
Win32_PhysicalMemory
|
Returns all the physical memory installed on the computer. This includes
RAM, ROM, EPROM etc.
|
SELECT * FROM
Win32_NetworkAdapterConfiguration
WHERE IPEnabled = true
|
Returns information about network adapters, just like ipconfig /all
|
|
|