简体   繁体   English

Java与WMI的互操作

[英]Java interoperation with WMI

I have a question wmi4java lib: Can it connect to a remote Win machine and run WMI queries there? 我有一个问题wmi4java lib:它可以连接到远程Win机器并在那里运行WMI查询吗? Searched the documentation and even looked at source code of the main class and found nothing that points to remote connection with authentication. 搜索了文档,甚至查看了主类的源代码,没有发现任何指向身份验证的远程连接的信息。

The github page gives no examples on connecting to a remote machine: https://github.com/profesorfalken/WMI4Java github页面未提供有关连接到远程计算机的示例: https : //github.com/profesorfalken/WMI4Java

and javadoc for Class WMI4Java mentions localhost only: WMI4Java类的Javadoc仅提及localhost:

https://jar-download.com/java-documentation-javadoc.php?a=WMI4Java&g=com.profesorfalken&v=1.6 https://jar-download.com/java-documentation-javadoc.php?a=WMI4Java&g=com.profesorfalken&v=1.6

eg: WMI4Java.get().computerName(".").namespace("root/cimv2").getWMIObject("Win32_BaseBoard"); 例如:WMI4Java.get()。computerName(“。”)。namespace(“ root / cimv2”)。getWMIObject(“ Win32_BaseBoard”);

If I specify another host (aka computerName("10.10.172.214")...) it will obviously throw exception since it found no credential. 如果我指定另一个主机(又名computerName(“ 10.10.172.214”)...),则显然会引发异常,因为它找不到凭据。

I know this lib uses internally jPowerShell so there must be a way. 我知道此库在内部使用jPowerShell,因此必须有一种方法。 Thank you. 谢谢。

Found how to to it via getRawWMIObjectOutput call. 通过getRawWMIObjectOutput调用找到方法。 This is not ideal as it doesn't look elegant and you get output as string instead of neat map of properties. 这是不理想的,因为它看起来并不优雅,并且您将获得输出为字符串而不是整洁的属性映射。 But anyway here it is: 但是无论如何,它是:

_log.info("\n-- Get raw output in String format on remote host (Win32_Process) --\n");
wmiObjectPropsRaw = WMI4Java.get()
    .computerName("10.10.172.214")
    .namespace("root/cimv2")
    .filters(Arrays.asList("$_.Name -eq \"lsass.exe\""))
    .getRawWMIObjectOutput(
       "Win32_Process" 
        + " -Impersonation 3" 
        +  " -Credential (new-object -typename System.Management.Automation.PSCredential"
            + " -argumentlist Administrator, " 
            + " (ConvertTo-SecureString Sysdreamworks123 -AsPlainText -Force))");
_log.info(wmiObjectPropsRaw + "\n");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM