2016-04-28 77 views
1

您是否可以了解一些Java的API以调用远程PowerShell脚本?通过Java的远程PowerShell脚本

从这个API,我需要的东西,如:

  • 登录到Windows机器。
  • 执行PowerShell脚本执行脚本

  • 获取结果或存在其他的方法来做到这一点? 谢谢

  • +0

    检查此链接:https://social.technet.microsoft.com/Forums/office/en-US/d32537bd-0aef-440e- 8760-6b3085390c37 /执行powershell脚本通过java?论坛= winserverpowershell 在这最后,有一个工作示例,包括错误处理。 – Martin

    +0

    谢谢,但这还不够,我必须登录并执行远程脚本; 例如,我将在我的Ubuntu上运行代码 - >连接到Windows机器 - >在此机器上执行脚本 - >获取执行结果。 虽然,谢谢 – GVArt

    +0

    可能相关:https://serverfault.com/questions/638659/managing-windows-powershell-from-linux-terminal –

    回答

    1

    如果你想通过Java远程执行某些东西,可以考虑​​。我不知道你是否希望保持登录状态(是否需要使用不同的用户?),但是如果你改变你的Windows密码,这种方法会继续工作。有些方法可以保留它(也可以在secure之间)这不是必要的)。我建议使用壳呼叫Apache Commons Exec (ExecuteWatchdog)来构建健壮的解决方案。

    长话短说:您可以绕过这个登录并保持某种便携式解决方案。

    +0

    我会尝试你的解决方案,是的,它需要使用不同的用户。 – GVArt

    +0

    在PowerShell中您可以切换用户。获取它作为参数,它将能够为你做出魔术。 – Mark

    0

    你可以尝试的ProcessBuilder和重定向输出流,就像这样:

    的javac 1.8.0_60编译和运行:Java版本 “1.8.0_91”

    import java.util.*; 
    import java.io.*; 
    public class Test { 
        public static void main(String ... args) { 
         try { 
          ProcessBuilder launcher = new ProcessBuilder(); 
          Map<String, String> environment = launcher.environment(); 
          launcher.redirectErrorStream(true); 
          launcher.directory(new File("\\\\remote_machine\\Snaps\\")); 
          launcher.command("powershell.exe", ".\\Script.ps1"); 
          Process p = launcher.start(); // And launch a new process 
          BufferedReader stdInput = new BufferedReader(new 
          InputStreamReader(p.getInputStream())); 
          String line; 
          System.out.println("Output :"); 
          while ((line = stdInput.readLine()) != null) { 
           System.out.println(line); 
          } 
         } catch (Exception e){ 
          e.printStackTrace(); 
         } 
        } 
    } 
    

    Script.ps1是一个用于测试,它是打印到输出流的简单文件:

    Write-Host 'Hello World!' 
    
    +0

    这不是个好主意。不安全。如果机器需要证书来做一些工作人员,那么你的变体是不好的 – GVArt

    0

    你好,我没有专门用PowerShell尝试过,但我已经用批处理文件和Windows Shell主机尝试过了。使用RMI的替代方法是使用提供的远程处理功能DCOM提供的窗口。这样你就不需要在Windows机器上部署一个额外的远程服务。相反,您可以使用每台Windows机器提供的DCOM服务器。 Windows机器公开了一些名为Windows Shell Host的东西。这个Windows Shell主机可以用来远程执行脚本。由于Windows中的几乎所有东西都以COM对象的形式出现,我的期望是,Powershell也可以作为您需要查找的已注册的COM服务。

    在这个环节,你会找到一个解决方案,以使用Windows Shell中的主机和J-互操作的Java实现DCOM协议的您的问题: How to call a remote bat file using jinterop

    / Create a session 
    JISession session = JISession.createSession(<domain>, <user>, <password>); 
    session.useSessionSecurity(true); 
    
    // Execute command 
    JIComServer comStub = new JIComServer(JIProgId.valueOf("WScript.Shell"),<IP>, session); 
    IJIComObject unknown = comStub.createInstance(); 
    final IJIDispatch shell =  (IJIDispatch)JIObjectFactory.narrowObject((IJIComObject)unknown.queryInterface(IJIDispatch.I ID)); 
    JIVariant results[] = shell.callMethodA("Exec", new Object[]{new JIString("%comspec% /c asadmin.bat")}); 
    

    如果您需要从批量输出你可以使用StdOut来读取它。

    JIVariant stdOutJIVariant = wbemObjectSet_dispatch.get("StdOut"); 
    IJIDispatch stdOut = (IJIDispatch)JIObjectFactory.narrowObject(stdOutJIVariant.getObjectAsComObject()); 
    
    // Read all from stdOut 
    while(!((JIVariant)stdOut.get("AtEndOfStream")).getObjectAsBoolean()){ 
        System.out.println(stdOut.callMethodA("ReadAll").getObjectAsString().getString()); 
    } 
    
    -1

    您可以使用JPowerShell库:https://github.com/profesorfalken/jPowerShell

    PowerShell powerShell = null; 
    try { 
        //Creates PowerShell session 
        PowerShell powerShell = PowerShell.openSession(); 
        //Increase timeout to give enough time to the script to finish 
        Map<String, String> config = new HashMap<String, String>(); 
        config.put("maxWait", "80000"); 
    
        //Execute script 
        PowerShellResponse response = powerShell.configuration(config).executeScript("./myPath/MyScript.ps1"); 
    
        //Print results if the script 
        System.out.println("Script output:" + response.getCommandOutput()); 
    } catch(PowerShellNotAvailableException ex) { 
        //Handle error when PowerShell is not available in the system 
        //Maybe try in another way? 
    } finally { 
        //Always close PowerShell session to free resources. 
        if (powerShell != null) 
         powerShell.close(); 
    }