Class WmiWbemServices

java.lang.Object
org.metricshub.wmi.wbem.WmiWbemServices
All Implemented Interfaces:
AutoCloseable, WindowsRemoteExecutor

public class WmiWbemServices extends Object implements WindowsRemoteExecutor
Class for WBEM Services handling.
  • Method Details

    • getInstance

      public static WmiWbemServices getInstance(String networkResource, String username, char[] password) throws WmiComException
      Get a WmiWbemServices instance for the specified networkResource.
      Parameters:
      networkResource - \\hostname\namespace or just namespace for localhost (mandatory)
      username - The username name (may be null).
      password - The password (may be null).
      Returns:
      WmiWbemServices instance
      Throws:
      WmiComException - For any problem encountered with JNA
    • close

      public void close()
      Release the WBEM services, disconnect and remove from the cache
      Specified by:
      close in interface AutoCloseable
    • executeWql

      public List<Map<String,Object>> executeWql(String wql, long timeout) throws TimeoutException, WqlQuerySyntaxException, WmiComException

      Execute a WQL query and process its result.

      Use the IWbemServices pointer to make requests of WMI.

      Step 6 from: Example: Getting WMI Data from a Remote Computer

      Specified by:
      executeWql in interface WindowsRemoteExecutor
      Parameters:
      wql - the WQL query (required)
      timeout - Timeout in milliseconds (throws an IllegalArgumentException if negative or zero)
      Returns:
      a list of result rows. A result row is a Map(LinkedHashMap to preserve the query order) of properties/values.
      Throws:
      TimeoutException - to notify userName of timeout.
      WqlQuerySyntaxException - if WQL query syntax is invalid
      WmiComException - on any COM problem
    • executeWql

      public List<Map<String,Object>> executeWql(WqlQuery wqlQuery, long timeout) throws TimeoutException, WmiComException, WqlQuerySyntaxException

      Execute a WQL query and process its result.

      Use the IWbemServices pointer to make requests of WMI.

      Step 6 from: Example: Getting WMI Data from a Remote Computer

      Parameters:
      wqlQuery - the WQL query (as a WqlQuery instance)
      timeout - Timeout in milliseconds (throws an IllegalArgumentException if negative or zero)
      Returns:
      a list of result rows. A result row is a Map(LinkedHashMap to preserve the query order) of properties/values.
      Throws:
      TimeoutException - to notify userName of timeout.
      WqlQuerySyntaxException - if WQL query syntax is invalid
      WmiComException - on any COM problem
    • executeCommand

      public WindowsRemoteCommandResult executeCommand(String command, String workingDirectory, Charset charset, long timeout) throws WmiComException, TimeoutException
      Description copied from interface: WindowsRemoteExecutor
      Execute the command on the remote
      Specified by:
      executeCommand in interface WindowsRemoteExecutor
      Parameters:
      command - The command to execute
      workingDirectory - Path of the directory for the spawned process on the remote system (can be null)
      charset - The charset
      timeout - Timeout in milliseconds
      Returns:
      The command result
      Throws:
      TimeoutException - To notify userName of timeout.
      WmiComException
    • getObject

      public com.sun.jna.Pointer getObject(String objectPath) throws WmiComException

      Invoke the IWbemServices::GetObject method.

      The IWbemServices::GetObject method retrieves a class or instance.

      Parameters:
      objectPath - Path of the object to retrieve. (mandatory)
      Returns:
      A pointer on IWbemClassObject.
      Throws:
      WmiComException - For any problem encountered with JNA.
      See Also:
    • executeMethod

      public Map<String,Object> executeMethod(String objectPath, String className, String methodName, Map<String,Object> inputMap) throws WmiComException
      Executes the specified method of the specified class on the specified object. Method inputs are specified as a Map<String, Object> mapping input names with their values.
      Parameters:
      objectPath - WBEM path to the object on which the method is to be executed
      className - Name of the class where the method is defined
      methodName - Name of the method to invoke
      inputMap - Map of input parameters for the method
      Returns:
      a Map<String, Object> with the output parameters of the method
      Throws:
      WmiComException - if anything goes wrong
    • objectPut

      public void objectPut(com.sun.jna.Pointer pWbemClassObject, String propertyName, com.sun.jna.platform.win32.Variant.VARIANT pVal) throws WmiComException

      Invoke the IWbemClassObject::Put method.

      The IWbemClassObject::Put method sets a named property to a new value.

      Parameters:
      pWbemClassObject - A pointer on IWbemClassObject. (mandatory)
      propertyName - A parameter that must point to a valid property name. (mandatory)
      pVal - A parameter that must point to a valid VARIANT, which becomes the new property value. If pVal is NULL or points to a VARIANT of type VT_NULL, the property is set to NULL, that is, no value.
      Throws:
      WmiComException - For any problem encountered with JNA.
      See Also:
    • spawnInstance

      public com.sun.jna.Pointer spawnInstance(com.sun.jna.Pointer pWbemClassObject) throws WmiComException

      Invoke the IWbemClassObject::SpawnInstance method.

      Use the IWbemClassObject::SpawnInstance method to create a new instance of a class.

      Parameters:
      pWbemClassObject - A pointer on IWbemClassObject. (mandatory)
      Returns:
      A pointer on instantiated WBEM Class object.
      Throws:
      WmiComException - For any problem encountered with JNA.
      See Also:
    • getNetworkResource

      public String getNetworkResource()
    • getHostname

      public String getHostname()
      Description copied from interface: WindowsRemoteExecutor
      Get the hostname.
      Specified by:
      getHostname in interface WindowsRemoteExecutor
      Returns:
    • getUsername

      public String getUsername()
      Description copied from interface: WindowsRemoteExecutor
      Get the username.
      Specified by:
      getUsername in interface WindowsRemoteExecutor
      Returns:
    • getPassword

      public char[] getPassword()
      Description copied from interface: WindowsRemoteExecutor
      Get the password.
      Specified by:
      getPassword in interface WindowsRemoteExecutor
      Returns:
    • getWbemServices

      public com.sun.jna.platform.win32.COM.Wbemcli.IWbemServices getWbemServices()
    • getWbemLocator

      public com.sun.jna.platform.win32.COM.Wbemcli.IWbemLocator getWbemLocator()
    • getContext

      public com.sun.jna.platform.win32.COM.Wbemcli.IWbemContext getContext()
    • getAuthIdent

      public CoAuthIdentity getAuthIdent()
    • isClosed

      public boolean isClosed()