2016-04-28 154 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(); 
    }