使用jenkins的Powershell脚本运行java程序

时间:2017-03-19 04:45:29

标签: java powershell jenkins

这是我的代码,用于运行powershell脚本以在远程机器上从java程序运行命令:

      ProcessBuilder launcher = new ProcessBuilder();
      Map<String, String> environment = launcher.environment();
      List<String> commands = new ArrayList<String>();
      commands.add("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe");
      commands.add("$pass = ConvertTo-SecureString -String mypassword -AsPlainText -Force;");
      commands.add("$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist \"myuser\",$pass;");
      commands.add("Invoke-Command -ComputerName " + server + " -ScriptBlock { " + command + "} -Credential $mycred;");

      launcher.redirectErrorStream(true); 

      launcher.command(commands);
      System.out.println (commands);

      Process p = launcher.start(); // And launch a new process BufferedReader
      BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
      StringBuilder builder = new StringBuilder();

      while ((line = stdInput.readLine()) != null) {
          System.out.println(line);
          builder.append(line);
          builder.append(System.getProperty("line.separator"));
      }
      line = builder.toString();

当我将它作为java程序运行或甚至作为ant脚本运行时,该程序可以正常工作。

但是当我从Jenkins在奴隶机器上运行相同的ant命令时,我得到下面给出的错误。有人可以帮我解决这个问题。我不知道为什么只有在从Jenkins运行时才会出现此错误。

"Connecting to remote server <server> failed with the 
     [exec] following error message : WinRM cannot process the request. The following 
     [exec] error with errorcode 0x8009030d occurred while using Negotiate authentication: 
     [exec] A specified logon session does not exist. It may already have been terminated. "

0 个答案:

没有答案