Running java program with Powershell scripts from jenkins

0

This is my code to run a powershell script to run a command on remote machine from a java program:

      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();

This program works perfectly when I run it as a java program or even as a ant Script.

But when I run the same ant command from Jenkins on a slave machine, I get the error given below. Can someone please help me with this issue. I'm clueless why its giving this error only when running from 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. "
java
powershell
jenkins
asked on Stack Overflow Mar 19, 2017 by hash • edited Mar 19, 2017 by MartinTeeVarga

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0