WMI 인스턴스의 일부 검색
부분 인스턴스 검색은 WMI가 인스턴스 속성의 하위 집합만 검색하는 경우입니다. 예를 들어 WMI는 이름 및 키 속성만 검색할 수 있습니다. 부분 인스턴스 검색의 가장 일반적인 사용은 여러 속성이 있는 큰 열거형에 있습니다.
PowerShell을 사용하여 WMI 인스턴스의 일부 검색
get-WmiObject 사용하여 인스턴스의 개별 속성을 검색할 수. 속성 자체를 검색하고 여러 가지 방법으로 표시할 수 있습니다. 인스턴스 검색과 마찬가지로 PowerShell은 기본적으로 지정된 클래스의 모든 인스턴스를 반환합니다. 단일 인스턴스만 검색하려면 특정 값을 지정해야 합니다.
다음 코드 예제에서는 Win32_LogicalDisk 클래스의 인스턴스에 대 한 볼륨 일련 번호를 표시 합니다.
(Get-WmiObject -class Win32_logicalDisk).DeviceID
#or
Get-WmiObject -class Win32_LogicalDisk | format-list DeviceID
#or
$myDisk = Get-WmiObject -class Win32_LogicalDisk
$myDisk.DeviceID
C#을 사용하여 WMI 인스턴스의 일부 검색(System.Management)
특정 인스턴스의 세부 정보를 사용하여 새 ManagementObject 만들어 인스턴스의 개별 속성을 검색할 수 있습니다. 그런 다음 GetPropertyValue 메서드를 사용하여 인스턴스의 속성을 하나 이상 암시적으로 검색할 수 있습니다.
메모
System.Management은 WMI에 액세스하는 데 사용된 원래의 .NET 네임스페이스였습니다. 그러나 이 네임스페이스의 API는 일반적으로 더 느리고, 현대적인 Microsoft.Management.Infrastructure 관련 API에 비해 확장성이 떨어집니다.
다음 코드 예제에서는 Win32_LogicalDisk 클래스의 인스턴스에 대 한 볼륨 일련 번호를 표시 합니다.
using System.Management;
...
ManagementObject myDisk = new ManagementObject("Win32_LogicalDisk.DeviceID='C:'");
string myProperty = myDisk.GetPropertyValue("VolumeSerialNumber").ToString();
Console.WriteLine(myProperty);
VBScript를 사용하여 WMI 인스턴스의 일부 검색
GetObject사용하여 인스턴스의 개별 속성을 검색할 수 있습니다.
다음 코드 예제에서는 Win32_LogicalDisk 클래스의 인스턴스에 대 한 볼륨 일련 번호를 표시 합니다.
MsgBox (GetObject("WinMgmts:Win32_LogicalDisk='C:'").VolumeSerialNumber)
C++를 사용하여 WMI 인스턴스의 일부 검색
다음 절차는 C++를 사용하여 부분 인스턴스 검색을 요청하는 데 사용됩니다.
C++ 사용하여 부분 인스턴스 검색을 요청하려면
CoCreateInstance호출하여 IWbemContext 개체를 만듭니다.
컨텍스트 개체는 WMI가 WMI 공급자에게 추가 정보를 전달하는 데 사용하는 개체입니다. 이 경우 IWbemContext 개체를 사용하여 공급자에게 부분 인스턴스 검색을 처리하도록 지시합니다.
IWbemContext 개체에 검색하려는 속성을 설명하는 __GET_EXTENSIONS, __GET_EXT_CLIENT_REQUEST 및 기타 명명된 값을 추가합니다.
다음 표에서는 검색 호출에 사용하는 다양한 명명된 값을 나열합니다.
IWbemContext 컨텍스트 객체를 IWbemServices::GetObject, IWbemServices::GetObjectAsync, IWbemServices::CreateInstanceEnum또는 IWbemServices::CreateInstanceEnumAsync 호출에 pCtx 매개 변수로 전달합니다.
IWbemContext 개체를 전달하면 공급자가 부분 인스턴스 검색을 허용하도록 지시합니다. 전체 인스턴스 검색에서 pCtx를 null 값으로 설정합니다. 공급자가 부분 인스턴스 검색을 지원하지 않는 경우 오류 메시지가 표시됩니다.
공급자가 부분 인스턴스 작업을 준수할 수 없는 경우, 공급자는 컨텍스트 개체를 입력하지 않은 것처럼 진행하거나 WBEM_E_UNSUPPORTED_PARAMETER를 반환합니다.
다음 예제에서는 Win32_LogicalDisk전체 인스턴스 검색을 수행한 다음 Win32_LogicalDisk.Caption 속성의 부분 인스턴스 검색을 수행하는 방법을 설명합니다.
#include <stdio.h>
#define _WIN32_DCOM
#include <wbemidl.h>
#pragma comment(lib, "wbemuuid.lib")
#include <iostream>
using namespace std;
#include <comdef.h>
void main(void)
{
HRESULT hr;
_bstr_t bstrNamespace;
IWbemLocator *pWbemLocator = NULL;
IWbemServices *pServices = NULL;
IWbemClassObject *pDrive = NULL;
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_CONNECT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL, EOAC_NONE, 0);
if (FAILED(hr))
{
CoUninitialize();
cout << "Failed to initialize security. Error code = 0x"
<< hex << hr << endl;
return;
}
hr = CoCreateInstance(CLSID_WbemLocator, NULL,
CLSCTX_INPROC_SERVER, IID_IWbemLocator,
(void**) &pWbemLocator);
if( FAILED(hr) )
{
CoUninitialize();
printf("failed CoCreateInstance\n");
return;
}
bstrNamespace = L"root\\cimv2";
hr = pWbemLocator->ConnectServer(bstrNamespace,
NULL, NULL, NULL, 0, NULL, NULL, &pServices);
if( FAILED(hr) )
{
pWbemLocator->Release();
CoUninitialize();
printf("failed ConnectServer\n");
return;
}
pWbemLocator->Release();
printf("Successfully connected to namespace.\n");
BSTR bstrPath =
SysAllocString(L"Win32_LogicalDisk.DeviceID=\"C:\"");
// *******************************************************//
// Perform a full-instance retrieval.
// *******************************************************//
hr = pServices->GetObject(bstrPath,
0,0, &pDrive, 0);
if( FAILED(hr) )
{
pServices->Release();
CoUninitialize();
printf("failed GetObject\n");
return;
}
// Display the object
BSTR bstrDriveObj;
hr = pDrive->GetObjectText(0, &bstrDriveObj);
printf("%S\n\n", bstrDriveObj);
pDrive->Release();
pDrive = NULL;
// *****************************************************//
// Perform a partial-instance retrieval.
// *****************************************************//
IWbemContext *pctxDrive; // Create context object
hr = CoCreateInstance(CLSID_WbemContext, NULL,
CLSCTX_INPROC_SERVER, IID_IWbemContext,
(void**) &pctxDrive);
if (FAILED(hr))
{
pServices->Release();
pDrive->Release();
CoUninitialize();
printf("create instance failed for context object.\n");
return;
}
VARIANT vExtensions;
VARIANT vClient;
VARIANT vPropertyList;
VARIANT vProperty;
SAFEARRAY *psaProperties;
SAFEARRAYBOUND saBounds;
LONG lArrayIndex = 0;
// Add named values to the context object.
VariantInit(&vExtensions);
V_VT(&vExtensions) = VT_BOOL;
V_BOOL(&vExtensions) = VARIANT_TRUE;
hr = pctxDrive->SetValue(_bstr_t(L"__GET_EXTENSIONS"),
0, &vExtensions);
VariantClear(&vExtensions);
VariantInit(&vClient);
V_VT(&vClient) = VT_BOOL;
V_BOOL(&vClient) = VARIANT_TRUE;
hr = pctxDrive->SetValue(_bstr_t(L"__GET_EXT_CLIENT_REQUEST"),
0, &vClient);
VariantClear(&vClient);
// Create an array of properties to return.
saBounds.cElements = 1;
saBounds.lLbound = 0;
psaProperties = SafeArrayCreate(VT_BSTR, 1, &saBounds);
// Add the Caption property to the array.
VariantInit(&vProperty);
V_VT(&vProperty) = VT_BSTR;
V_BSTR(&vProperty) = _bstr_t(L"Caption");
SafeArrayPutElement(psaProperties, &lArrayIndex,
V_BSTR(&vProperty));
VariantClear(&vProperty);
VariantInit(&vPropertyList);
V_VT(&vPropertyList) = VT_ARRAY | VT_BSTR;
V_ARRAY(&vPropertyList) = psaProperties;
// Put the array in the named value __GET_EXT_PROPERTIES.
hr = pctxDrive->SetValue(_bstr_t(L"__GET_EXT_PROPERTIES"),
0, &vPropertyList);
VariantClear(&vPropertyList);
SafeArrayDestroy(psaProperties);
// Pass the context object as the pCtx parameter of GetObject.
hr = pServices->GetObject(bstrPath, 0, pctxDrive, &pDrive, 0);
if( FAILED(hr) )
{
pServices->Release();
CoUninitialize();
printf("failed property GetObject\n");
return;
}
// Display the object
hr = pDrive->GetObjectText(0, &bstrDriveObj);
printf("%S\n\n", bstrDriveObj);
SysFreeString(bstrPath);
VARIANT vFileSystem;
// Attempt to get a property that was not requested.
// The following should return a NULL property if
// the partial-instance retrieval succeeded.
hr = pDrive->Get(_bstr_t(L"FileSystem"), 0,
&vFileSystem, NULL, NULL);
if( FAILED(hr) )
{
pServices->Release();
pDrive->Release();
CoUninitialize();
printf("failed get for file system\n");
return;
}
if (V_VT(&vFileSystem) == VT_NULL)
printf("file system variable is null - expected\n");
else
printf("FileSystem = %S\n", V_BSTR(&vFileSystem));
VariantClear(&vFileSystem);
pDrive->Release();
pctxDrive->Release();
pServices->Release();
pServices = NULL; // MUST be set to NULL
CoUninitialize();
return; // -- program successfully completed
};
실행될 때 이전 코드 예제에서는 다음 정보를 씁니다. 첫 번째 개체 설명은 전체 인스턴스 검색에서 가져온 것입니다. 두 번째 개체 설명은 부분 인스턴스 검색에서 가져온 것입니다. 마지막 섹션에서는 원래 GetObject 호출에서 요청되지 않은 속성을 요청하면 null 값을 수신한다는 것을 보여 있습니다.
Successfully connected to namespace
instance of Win32_LogicalDisk
{
Caption = "C:";
Compressed = FALSE;
CreationClassName = "Win32_LogicalDisk";
Description = "Local Fixed Disk";
DeviceID = "C:";
DriveType = 3;
FileSystem = "NTFS";
FreeSpace = "3085668352";
MaximumComponentLength = 255;
MediaType = 12;
Name = "C:";
Size = "4581445632";
SupportsFileBasedCompression = TRUE;
SystemCreationClassName = "Win32_ComputerSystem";
SystemName = "TITUS";
VolumeName = "titus-c";
VolumeSerialNumber = "7CB4B90E";
};
instance of Win32_LogicalDisk
{
Caption = "C:";
CreationClassName = "Win32_LogicalDisk";
Description = "Local Fixed Disk";
DeviceID = "C:";
DriveType = 3;
MediaType = 12;
Name = "C:";
SystemCreationClassName = "Win32_ComputerSystem";
SystemName = "MySystem";
};
다음 출력은 이전 예제에서 생성됩니다.
file system variable is null - expected
Press any key to continue