0

I have a question wmi4java lib: Can it connect to a remote Win machine and run WMI queries there? 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

and javadoc for Class WMI4Java mentions localhost only:

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

e.g: 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.

I know this lib uses internally jPowerShell so there must be a way. Thank you.

  • May I suggest you to use WinRM instead of WMI (it's way much easier and *robust*). https://stackoverflow.com/questions/4610063/recommended-libraries-howtos-for-using-wmi-with-java might also be helpful –  Oct 19 '17 at 17:43
  • Thanks RC. We'll use WinRM for our future release but now we use j-interop which fails in customer's firewall since this lib uses deprecated and insecure protocol SMBv1. So, I'm looking to replace j-interop with wmi4java as a temp workaround to customer's issue. But for that I need to be able to connect to remote Win machine and here comes my challenge. – Daniel Burtman Oct 19 '17 at 18:28
  • I see, IMHO, you should seek another replacement lib. Maybe https://github.com/chenlichao-cn/wmi4j (found via Google, seems to do what you need) –  Oct 19 '17 at 18:52
  • RC thank you, great suggestion. I'll certainly explore it. On the first glance it does not seem to be dependent on PowerShell being installed on the same machine. – Daniel Burtman Oct 20 '17 at 14:25
  • wmi4j internally uses j-interop which brings me back to square one. Cannot use jinterop since it uses SMBv1 under the hood and any firewall resets connection if it detects packets with this unsafe protocol. – Daniel Burtman Oct 20 '17 at 15:33

1 Answers1

0

Found how to to it via getRawWMIObjectOutput call. 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");