Accessing a Remote WMI Win32_ComputerSystem Object
Accessing a remote namespace provides all of the capabilities most developers need. However, there may be times when accessing an object directly is more efficient. For example, a Directory Services query may return an enumerated list of computer objects that a developer wishes to manage. To avoid the additional step of accessing multiple namespaces, the IWMIExtension interface provides the GetWMIObject method.
Note For more information about support and installation of this component on a specific operating system, see Operating System Availability of WMI Components.
The GetWMIObject method returns an instance of a WMI Win32_ComputerSystem object that Directory Services maps to a Directory Services Computer object. Due to the structure of the Directory Services, the ADSI extension currently supports the GetWMIObject method only on Directory Services Computer objects. After GetWMIObject returns the Win32_ComputerSystem object, a developer can access the object like any other WMI object.
The following procedure describes how to access a remote Win32_ComputerSystem object.
To access a remote Win32_ComputerSystem object
- Determine the name of the computer containing the
Win32_ComputerSystem object you wish to access.
You need the name of the computer to locate the computer on the enterprise. The most common way to locate a computer is to query Active Directory. Active Directory uses the current Directory Services security settings for the user. Make sure the user has the correct level of access before attempting to use Active Directory.
- Retrieve the Active Directory Computer object that represents the computer.
Directory Services uses the current DS user security settings to access Active Directory.
- Use the
GetWMIObject method to retrieve the
Win32_ComputerSystem instance associated with the computer.
Directory Services uses the current WMI user security settings to access the Win32_ComputerSystem object. Therefore, ensure the current user has the correct security clearance.
- Access the Win32_ComputerSystem object as you would a normal WMI object.
Dim ADSObject As IADs Dim WMIObject As SWbemObject ' Query Active Directory for your computer. Set ADSObject = GetObject("LDAP://CN=<computer _ name>,CN=Computers,DC=<domain>,DC=<company>,DC=com") ' Use GetWMIObject on the Directory Services Computer object to retrieve the WMI object Set WMIObject = ADSObject.GetWMIObject ' Now use any properties ' or methods of the WMI object. Debug.Print "Status=" + WMIObject.Status
IADs *pComputer = NULL; IWMIExtension *pWMI = NULL; ISWbemObject *pObj = NULL; wchar_t Obj = L"LDAP://CN=<computer name,CN=Computers,DC=<domain>,DC=<company>,DC=com"; CoInitialize (NULL); //Query Active Directory to get your computer. HRESULT hRes = ADsGetObject( Obj, IID_IADs, (void**)&pComputer ); //Get the WMI ADSI extension interface on this object. hRes = pComputer->QueryInterface( IID_IWMIExtension, (void**)&pWMI ); //Use the extension to get the WMI object. hRes = pWMI->GetWMIObject(&pObj); //Print the object. BSTR strObjText; hRes = pObj->GetObjectText_(0, &strObjText); wprintf(L"%s\n", strObjText); SysFreeString(strObjText); //Clean up. pObj->Release(); pWMI->Release(); pComputer->Release();
The C++ code requires the following references and #include statements to compile correctly.
# pragma comment(lib, "wbemuuid.lib") #include <iostream> using namespace std; #include <Iads.h> #include <Wbemads.h> #include <Adshlp.h>