Ola Pessoal do GUJ ,
Boa tarde !
Estou com uma duvida GRANDE , estou tentando usar a API JACOB para pegar informações por WMI de outra maquina windows na rede , mas não estou conseguindo , só consigo pegar da maquina local.
Segue o fonte de exemplo para mostrar informações das contas de usuario na maquina local:
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;
/**
* Quick little example of using JACOB (an open source Java-COM bridge) to access
* the windows WMI interface.
* <br>
* To use this snippet you will need to download and install JACOB. This is relatively easy:
* I just uncompressed the zip and put the jacob.jar and the dll into my classpath.
*
* @author NickDMax (at) DreamInCode
*/
public class ListUsers {
/**
* List the services currently running on the host computer.
*
* @param args
*/
public static void main(String[] args) {
String host = "localhost"; //Technically you should be able to connect to other hosts, but it takes setup
String connectStr = String.format("winmgmts:\\\\%s\\root\\CIMV2", host);
String query = "SELECT * FROM Win32_UserAccount"; //Started = 1 means the service is running.
//String query = "SELECT * FROM Win32_Account WHERE Name=\"Rafael\""; //Started = 1 means the service is running.
ActiveXComponent axWMI = new ActiveXComponent(connectStr);
//Execute the query
Variant vCollection = axWMI.invoke("ExecQuery", new Variant(query));
//Our result is a collection, so we need to work though the.
EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
Dispatch item = null;
while (enumVariant.hasMoreElements()) {
System.out.println("*****Testando*****");
item = enumVariant.nextElement().toDispatch();
//Dispatch.call returns a Variant which we can convert to a java form.
String sid = Dispatch.call(item,"SIDType").toString();
System.out.println(sid);
if( sid.equals("1") ) {
try {
String Nome = Dispatch.call(item,"Name").toString();
String FullNome = Dispatch.call(item,"FullName").toString();
String Descricao = Dispatch.call(item,"Description").toString();
Boolean Habilitado = Dispatch.call( item,"Disabled").getBoolean();
//int servicePID = Dispatch.call(item,"ProcessId").getInt();
System.out.println("Login:"+Nome);
System.out.println("Nome:"+FullNome);
System.out.println("Descricao:"+Descricao);
System.out.println("Desabilitado:"+Habilitado);
} catch( Exception e ) {
e.printStackTrace();
}
//System.exit(0);
} // fim do if
}
System.exit(0);
}
}
Segue abaixo o fonte de uma das tentativas de tentar pegar de uma maquina remota:
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;
/**
* Quick little example of using JACOB (an open source Java-COM bridge) to access
* the windows WMI interface.
* <br>
* To use this snippet you will need to download and install JACOB. This is relatively easy:
* I just uncompressed the zip and put the jacob.jar and the dll into my classpath.
*
* @author NickDMax (at) DreamInCode
*/
public class ListUsers {
/**
* List the services currently running on the host computer.
*
* @param args
*/
public static void main(String[] args) {
String host = "192.168.1.3"; //Technically you should be able to connect to other hosts, but it takes setup
String connectStr = String.format("WbemScripting.SWbemLocator");
// String connectStr = String.format("winmgmts:\\\\%s\\root\\CIMV2", host);
// String connectStr = String.format("winmgmts:root\\CIMV2:Win32_AccountUser");
String query = "SELECT * FROM Win32_UserAccount"; //Started = 1 means the service is running.
//String query = "SELECT * FROM Win32_Account WHERE Name=\"Rafael\""; //Started = 1 means the service is running.
ActiveXComponent axWMI = new ActiveXComponent(connectStr);
//Execute the query
// Variant vCollection = axWMI.invoke("ConnectServer", new Variant(host),new Variant("root\\cimv2"), new Variant("usuario"),new Variant("senha"));
Variant vCollection = axWMI.invoke("ConnectServer",new Variant("\"192.168.1.3\",\"usuario\",\"senha\""));
//vCollection.toDispatch();
vCollection = axWMI.invoke("ExecQuery", new Variant(query));
//Our result is a collection, so we need to work though the.
EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
Dispatch item = null;
while (enumVariant.hasMoreElements()) {
System.out.println("*****Testando*****");
item = enumVariant.nextElement().toDispatch();
//Dispatch.call returns a Variant which we can convert to a java form.
String sid = Dispatch.call(item,"SIDType").toString();
System.out.println(sid);
if( sid.equals("1") ) {
try {
String Nome = Dispatch.call(item,"Name").toString();
String FullNome = Dispatch.call(item,"FullName").toString();
String Descricao = Dispatch.call(item,"Description").toString();
Boolean Habilitado = Dispatch.call( item,"Disabled").getBoolean();
//int servicePID = Dispatch.call(item,"ProcessId").getInt();
System.out.println("Login:"+Nome);
System.out.println("Nome:"+FullNome);
System.out.println("Descricao:"+Descricao);
System.out.println("Desabilitado:"+Habilitado);
} catch( Exception e ) {
e.printStackTrace();
}
//System.exit(0);
} // fim do if
}
System.exit(0);
}
}
Mensagem de erro (e trava o prompt de comando):
E:\Testes_java>java -classpath .;jacob-1.15-M4\jacob.jar ListUsers
Exception in thread "main" com.jacob.com.ComFailException: Invoke of: ConnectSer
ver
Source: SWbemLocator
Description: O servidor RPC nÒo estß disponÝvel.
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invokev(Dispatch.java:625)
at com.jacob.com.Dispatch.callN(Dispatch.java:453)
at com.jacob.activeX.ActiveXComponent.invoke(ActiveXComponent.java:476)
at ListUsers.main(ListUsers.java:32)
Ja pesquisei no Google e em outros lugares mas não consegui fazer funcionar e com um outro programa eu consigo acessar as informações de uma maquina remota
Por favor se alguem puder me ajudar com um exemplo agradeço.
Desde já agradeço a todos !!!