The Java Web-Based Enterprise Management (JWbem) is an on-going Java implementation of Microsoft's Scripting Web-Based Enterprise Management (SWbem) API. JWbem allows Java developers to easily monitor and manage Microsoft Windows via Windows Management Instrumentation (WMI).
What can you do with JWbem? Simple. JWbem utilizes j-Interop to provide a very easy framework for Java developers to remotely manage and monitor Microsoft Windows computer systems via familiar WMI syntax.
This example shows how to connect to a remote Hyper-V server's virtualization namespace.
01.
import
com.hyper9.jwbem.SWbemLocator;
02.
import
com.hyper9.jwbem.SWbemServices;
03.
04.
...
05.
06.
// The IP address or FQDN of the Windows server to connect to.
07.
String serverName =
"hyperv.hyper9.local"
;
08.
09.
// The CIM namespace to connect to.
10.
String cimNamespace =
"root\\virtualization"
;
11.
12.
// The name of the user to connect as. The format of the user name supports
13.
// USERNAME, DOMAIN\\USERNAME, and USERNAME@DOMAIN.
14.
String userName =
"Hyper9\\akutz"
;
15.
16.
// The passprase for the given user.
17.
String passphrase =
"passphrase"
;
18.
19.
// Create a locator object.
20.
SWbemLocator loc =
new
SWbemLocator();
21.
22.
// Connect to the Windows server and return a services object.
23.
SWbemServices svc = loc.connect(serverName,
"127.0.0.1"
, cimNamespace, username, passphrase);
This example shows how to enumerate all of the virtual machines on the Hyper-V server we established a connection to in the previous example.
01.
import
com.hyper9.jwbem.SWbemObjectSet;
02.
import
com.hyper9.jwbem.msvm.MsvmComputerSystem;
03.
04.
...
05.
06.
// Define the WQL query that returns all of a Hyper-V's virtual machines.
07.
String wql =
"SELECT * FROM Msvm_ComputerSystem WHERE Caption='Virtual Machine'"
;
08.
09.
// Execute the query.
10.
SWbemObjectSet<MsvmComputerSystem> compSysSet = svc.execQuery(wql, MsvmComputerSystem.
class
);
11.
12.
// Print the names of the virtual machines.
13.
for
(MsvmComputerSystem cs : compSysSet)
14.
{
15.
System.out.println(cs.getElementName());
16.
}