The first type of object you can retrieve is a WMI class. When retrieving a WMI class, you actually retrieve a class definition, which is a listing of the properties, qualifiers, and methods that fully describe the class. However, a class definition is basically the class itself.
PowerShell uses a standard query to retrieve class definitions, using the meta_class class.
To retrieve a class definition in PowerShell
Use the Get-WmiObject with a query to meta_class, with the WHERE clause containing the name of the class you with to retrieve.
PowerShell
Get-WmiObject -query"SELECT * FROM meta_class WHERE __class = 'Win32_LogicalDisk'"
Get-WmiObject is the standard cmdlet PowerShell uses to retrieve class and instance information from WMI. The meta_class class defines the query as a schema query. Without the meta_class class, this query would return all instances of Win32_LogicalDisk. For more information about querying WMI, see SELECT Statement for Schema Queries.
The current process for retrieving a WMI definition in C# is to use CIMInstance class.
To retrieve a class definition in C# (Microsoft.Management.Infrastructure)
Using the Microsoft.Management.Infrastructure namespace, create a CIMInstance class with the specified namespace and class name.
The created class will contain all of the class information, but no instance data.
CSharp
using Microsoft.Management.Infrastructure;
...
string Namespace = @"root\cimv2";
string className = "Win32_LogicalDisk";
CimInstance diskDrive = new CimInstance(className, Namespace);
Alternately, as with PowerShell, you could also perform a query, using the meta_class tag as part of the query.
CSharp
using Microsoft.Management.Infrastructure;
...
string Namespace = @"root\cimv2";
string diskDriveQuery = "SELECT * FROM meta_class WHERE __class = 'Win32_LogicalDisk'";
CimSession mySession = CimSession.Create("localhost");
IEnumerable<CimInstance> queryInstance = mySession.QueryInstances(Namespace, "WQL", diskDriveQuery);
As with PowerShell, C# uses a meta_class query to retrieve class definitions. Alternately, you can create a ManagementClass object to access the class definition directly.
Not
System.Management was the original .NET namespace used to access WMI; however, the APIs in this namespace generally are slower and do not scale as well relative to their more modern Microsoft.Management.Infrastructure counterparts.
To retrieve a class definition in C# (System.Management)
You can use the ManagementObjectSerarcher with a query to meta_class, with the WHERE clause containing the name of the class you with to retrieve.
CSharp
using System.Management;
...
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM meta_class WHERE __class = 'Win32_LogicalDisk'");
ManagementObjectCollection myDiskCollection = searcher.Get();
A class or instance can also be specified, in which case the returned object is a WMI object, for example, an instance of Win32_LogicalDisk, rather than a services object. Note that you cannot use the VBScript GetObject functions to create an instance of the generic object SWbemObject.
In HTML pages running in Microsoft Internet Explorer (IE), GetObject and CreateObject can fail because WMI scripting objects, like ActiveX controls, are not marked as safe for scripting. The one exception is the SWbemDateTime object. The only way that these calls can succeed is when you lower the IE security settings, which is not recommended.
One class can have multiple class definitions, which happens typically when you have more than one class provider loaded into one namespace. When a class has multiple class definitions, WMI returns the first definition discovered and the WBEM_S_DUPLICATE_OBJECTS status code.
Bu modülde sınıfları içeren ad alanlarının yapısı ve ayrıca bir sınıfın örneklerini sorgulama açıklanmaktadır. Geçici bağlantıları ve CIM oturumlarını kullanarak uzak bilgisayarları sorgulamayı kapsar.