java - Jacob 连接到远程计算机以获得 WMI 支持

标签 java wmi jacob

我正在尝试使用 java 和 Jacob 连接到远程计算机,以获取有关远程计算机的一些 WMI 信息。

对于本地主机,我使用下面的代码,它工作正常。

    String host = "localhost";
    String connectStr = String.format("winmgmts:\\\\%s\\root\\CIMV2", host);

    ActiveXComponent axWMI = new ActiveXComponent(connectStr);
    // other code to get system information

但是如果我将 localhost 更改为另一个 ip/hostname 我会收到以下错误:

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)

抛出异常的行是:

ActiveXComponent axWMI = new ActiveXComponent(connectStr);

编辑

我尝试使用 WbemScripting 传递用户名/密码

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

但是我得到了这个错误:

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

我该如何解决?我如何传递用户名/密码以及是否需要域???

我使用的是 Windows 8,我正在尝试连接到 win8/win7/winxp/win2003server 计算机。

最佳答案

经过一些搜索我设法解决了我的问题...

如果有人需要的话,这是代码。

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;

关于java - Jacob 连接到远程计算机以获得 WMI 支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17573374/

相关文章:

java - 带有 Spring 5 的响应式(Reactive) WebSockets - 如何获取初始消息

c - Win32_NetworkAdapterConfiguration::EnableDHCP 的奇怪行为?

networking - 如何在Powershell中检索Vista的网络状态(例如 “Local Only”, “Local and Internet”)

Java、Jacob 和 Microsoft Word : how to properly handle events?

java - 如何通过 JsonSystem(Itemscript JSON 库)使用内存数据存储?

java - requestLegacyExternalStorage 在 Android 11 - API 30 中不起作用

sql-server - 从 WiX 引导 SQL Express?

java - Outlook在java中为jacob.jar定制文件夹的整数值

Java 和 Autoit 无法发送 TAB、ENTER 等按键?

java jackson json 处理器 - 在 RestTemplate 中使用 - EnumSet 的处理