/WMI4Java

Java API to perform WMI queries

Primary LanguageJavaApache License 2.0Apache-2.0

WMI4Java

Java API to perform WMI queries

Installation

To install WMI4Java you can add the dependecy to your software project management tool: http://mvnrepository.com/artifact/com.profesorfalken/WMI4Java/1.6.2

For example, for Maven you have just to add to your pom.xml:

  <dependency>
        <groupId>com.profesorfalken</groupId>
        <artifactId>WMI4Java</artifactId>
        <version>1.6.2</version>
    </dependency>

Instead, you can direct download the JAR file and add it to your classpath. https://repo1.maven.org/maven2/com/profesorfalken/WMI4Java/1.6.2/WMI4Java-1.6.2.jar

Basic Usage

List all WMI classes names from root/CIMv2 (default namespace)

    List<String> wmiClassesList = WMI4Java.get().listClasses();

List all WMI classes names from root/WMI

    List<String> wmiClassesList = WMI4Java.get().namespace("root/WMI").listClasses();

Get all the properties of a WMI Object

    //Example win32_BIOS
    Map<String, String> wmiObjectProperties = WMI4Java.get().getWMIObject("Win32_BIOS");

Get a list of WMI Objects

When you need to retrieve a list of WMI objects (processors, printers, etc), the flat getWMIObject method is not adequate. In this case you should use the method getWMIObjectList instead.

    //Example Win32_PRINTER
    List<Map<String, String>> wmiObjectListProperties = WMI4Java.get().getWMIObject("Win32_PRINTER");

More Advanced Usage

Get raw output in String format

    //Example win32_BIOS
    Map<String, String> wmiObjectProperties = WMI4Java.get().getRawWMIObjectOutput("Win32_BIOS");

Filter results by property value

    //Example get processes named java.exe
    Map<String, String> wmiObjectProperties = 
        WMI4Java.get().filters(Arrays.asList("$_.Name -eq \"java.exe\"")).getWMIObject("Win32_Process");

Get only specific properties

    //Example get only specific properties from system processes
    Map<String, String> wmiObjectProperties = 
        WMI4Java.get()
            .properties(Arrays.asList("Name", "CommandLine", "ProcessId")).getWMIObject("Win32_Process");

How it works

WMI4Java uses two different mechanism in order to retrieve WMI information.

-By default, it uses jPowerShell (https://github.com/profesorfalken/jPowerShell) to open a PowerShell session and invoke the command.

-Instead, we can use an implementation based on VBScript that creates and launches an script on background.

Change the used implementation is really simple. For example, to reproduce the last example using VBScript instead of PowerShell:

    //Example win32_BIOS
    Map<String, String> wmiObjectProperties = WMI4Java.get().VBSEngine().getWMIObject("Win32_BIOS");