如何加入WMI查询(WQL)

时间:2010-07-08 18:39:52

标签: c++ vbscript wmi wql

我想通过WQL查询获取boot-harddisk的序列号。

可以使用以下查询检索启动分区:

SELECT * FROM Win32_DiskPartition where BootPartition=True

序列号在Win32_DiskDrive中:

SELECT DeviceID, SerialNumber FROM Win32_DiskDrive

Win32_DiskDriveToDiskPartition的映射为Win32_DiskDriveWin32_DiskPartition。 它们在Win32_DiskDrive.DeviceID

中映射Win32_DiskPartition.DeviceIDWin32_DiskDriveToDiskPartition

如何构建内部加入Win32_DiskPartitionWin32_DiskDrive的WQL查询? 我是否必须使用Associate?或者它是否适用于INNER JOIN?

2 个答案:

答案 0 :(得分:14)

WQL不支持JOIN子句。您猜测时需要使用ASSOCIATORS OF语句。这是VBScript中的一个例子:

strComputer = "." 
Set oWMI = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 

Set colPartitions = oWMI.ExecQuery( _
    "SELECT * FROM Win32_DiskPartition WHERE BootPartition=True") 

For Each oPartition in colPartitions 

    Set colDrives = oWMI.ExecQuery( _
        "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
        & oPartition.DeviceID & "'} WHERE ResultClass=Win32_DiskDrive")

    For Each oDrive in colDrives
        WScript.Echo oDrive.SerialNumber
    Next

Next

但请注意,在Windows Vista之前,Win32_DiskDrive.SerialNumber属性不可用。因此,如果您希望代码也能在早期的Windows版本上运行(例如Windows XP或Windows 2000),则应考虑使用WMI以外的API。


编辑: (回复评论)是的,您可以添加嵌套的ASSOCIATORS OF查询以获取与{对应的Win32_PhysicalMedia个实例{1}}个实例;像这样的东西:

Win32_DiskDrive

你还没有说过你正在使用的语言 - 我想在PowerShell或C#中,整个事情可以更优雅地完成,但VBScript非常冗长。

答案 1 :(得分:10)

这是与Helen发布的VBScript代码完全相同的C ++代码。

// Obtain the initial locator to WMI
// ...
// Connect to WMI through the IWbemLocator::ConnectServer method
// ...
// Set security levels on the proxy
// ...

wchar_t wmihddsn[256];
    *wmihddsn=0;

hres = pSvc->ExecQuery(
    bstr_t("WQL"),
    bstr_t("SELECT * FROM Win32_DiskPartition WHERE BootPartition=True"),
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
    NULL,
    &pEnumerator);

if(SUCCEEDED(hres) && pEnumerator) 
{
    // get the first Win32_DiskPartition
    HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);

    if(SUCCEEDED(hr) && 0 != uReturn)
    {
        VARIANT vtProp;
        wchar_t tmp[1024];
        char query[1024];

        // Get the value of the partition's DeviceID property
        hr = pclsObj->Get(L"DeviceID", 0, &vtProp, 0, 0);
        if(SUCCEEDED(hr))
        {
            if(vtProp.vt == VT_BSTR) {
                // wcout << " SerialNumber : " << vtProp.bstrVal << endl;
                wcscpy(tmp, vtProp.bstrVal);
            }
            VariantClear(&vtProp);


            // "join" Win32_DiskPartition to Win32_DiskDrive
            sprintf(query, 
                "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='%s'} WHERE ResultClass=Win32_DiskDrive",
                NarrowWcharString(tmp));

            hres = pSvc->ExecQuery(
                bstr_t("WQL"),
                bstr_t(query),
                WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
                NULL,
                &pEnumerator1);

            if(SUCCEEDED(hres) && pEnumerator1)
            {
                // get the first Win32_DiskDrive
                hr = pEnumerator1->Next(WBEM_INFINITE, 1, &pclsObj1, &uReturn);

                if(SUCCEEDED(hr) && 0 != uReturn)
                {
                    // Get the value of the disk-drive's DeviceID
                    hr = pclsObj1->Get(L"DeviceID", 0, &vtProp, 0, 0);
                    if(SUCCEEDED(hr))
                    {
                        if(vtProp.vt == VT_BSTR)
                        {
                            wcscpy(tmp, vtProp.bstrVal);
                        }
                        VariantClear(&vtProp);


                        // "join" Win32_DiskDrive to Win32_PhysicalMedia
                        sprintf(query,
                            "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='%s'} WHERE ResultClass=Win32_PhysicalMedia",
                            NarrowWcharString(tmp));

                        hres = pSvc->ExecQuery(
                            bstr_t("WQL"),
                            bstr_t(query),
                            WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
                            NULL,
                            &pEnumerator2);

                        if(SUCCEEDED(hres) && pEnumerator2)
                        {
                            // get the first Win32_PhysicalMedia
                            hr = pEnumerator2->Next(WBEM_INFINITE, 1, &pclsObj2, &uReturn);

                            if(SUCCEEDED(hr) && 0 != uReturn)
                            {
                                // get the PhysicalMedia's SerialNumber
                                hr = pclsObj2->Get(L"SerialNumber", 0, &vtProp, 0, 0);
                                if(SUCCEEDED(hr))
                                {
                                    if(vtProp.vt == VT_BSTR) 
                                    {
                                        // wcout << " SerialNumber : " << vtProp.bstrVal << endl;
                                        wcscpy(wmihddsn,vtProp.bstrVal);
                                    }
                                    VariantClear(&vtProp);
                                }

                            }

                            if(pclsObj2) pclsObj2->Release();
                        }
                        if(pEnumerator2) pEnumerator2->Release();

                    } // get disk-drive's DeviceID
                }

                if(pclsObj1) pclsObj1->Release();
            }
            if(pEnumerator1) pEnumerator1->Release();

        } // get partition's DeviceID
    }

    if(pclsObj) pclsObj->Release();
} // if succeeded first query
if(pEnumerator) pEnumerator->Release();

// ...
// cleanup