Jacob connects to a remote computer to support WMI

I am trying to connect to a remote computer using java and Jacob to get the WMI information about the remote computer.

For localhost, I use the code below and it works fine.

String host = "localhost"; String connectStr = String.format("winmgmts:\\\\%s\\root\\CIMV2", host); ActiveXComponent axWMI = new ActiveXComponent(connectStr); // other code to get system information 

But if I change localhost to another ip / hostname, I got the following error:

 Exception in thread "main" com.jacob.com.ComFailException: Can't find moniker at com.jacob.com.Dispatch.createInstanceNative(Native Method) at com.jacob.com.Dispatch.<init>(Dispatch.java:99) at com.jacob.activeX.ActiveXComponent.<init>(ActiveXComponent.java:58) at easyticket.classes.WmiExtended.main(WmiExtended.java:28) 

and the line that throws the exception:

 ActiveXComponent axWMI = new ActiveXComponent(connectStr); 

EDIT

I tried passing username / password using WbemScripting

 String host = "192.168.7.106"; ActiveXComponent axWMI = new ActiveXComponent("WbemScripting.SWbemLocator"); axWMI.invoke("ConnectServer", new Variant(host+",\"root\\cimv2\",\"username\",\"password\"")); 

but I got this error:

 Exception in thread "main" com.jacob.com.ComFailException: Invoke of: ConnectServer Source: SWbemLocator Description: The RPC server is unavailable. 

How can i solve this? How can I pass a username / password, and if I need a domain ???

I am using Windows 8 and I am trying to connect to win8 / win7 / winxp / win2003 computers.

+4
source share
1 answer

After some searching, I managed to solve my problem ...

Here's the code, if anyone needs it.

 ActiveXComponent wmi = new ActiveXComponent("WbemScripting.SWbemLocator"); Variant variantParameters[] = new Variant[4]; variantParameters[0] = new Variant(_IPADDRESS); variantParameters[1] = new Variant("root\\cimv2"); variantParameters[2] = new Variant("username"); variantParameters[3] = new Variant("password"); ActiveXComponent axWMI; try { Variant conRet = wmi.invoke("ConnectServer", variantParameters); axWMI = new ActiveXComponent(conRet.toDispatch()); }catch(ComFailException e) { axWMI = null; } if (axWMI == null) return false; 
+4
source

Source: https://habr.com/ru/post/1490732/


All Articles