Results 1 to 7 of 7
  1. #1
    yelllow4u is offline Member
    Join Date
    Jul 2007
    Posts
    10
    Rep Power
    0

    Default Trying to run Powershell from Java

    I am trying to run a Powershell command from Java that creates an Active Directory group. I don't get any errors, but it doesn't work either. I can take the output from this class and plug it into the CMD prompt and it works fine. So I am thinking that the PS parser is doing something funky with my String. Any ideas?

    Java Code:
    public class CreateAD {
        public int creategroup(String groupname, String description){
            String runadcreate;
            int errorfree =0;
            
            
            runadcreate = "powershell Import-Module ActiveDirectory \n" +
                          "powershell New-ADGroup -Path '\"OU=ClearCase_Access,OU=Application_Groups,DC=gs,DC=mydomain,DC=net\"' " +
                          "-Name '\"" + groupname + "\"' -sAMAccountName '\"" + groupname + "\"' -GroupCategory Security " +
                          "-GroupScope DomainLocal -Description '\"" + description + "\"'";
            
            String [] command = {"CMD", "/C", runadcreate};
            
            StringBuilder errors = new StringBuilder();
            ProcessBuilder probuilder = new ProcessBuilder(command);
             
            try {
                Process newad = probuilder.start();
                
                //capture any errors
                String bufferError = null;  
                BufferedReader cmdError = new BufferedReader(new InputStreamReader(newad.getErrorStream()));  
                while ((bufferError = cmdError.readLine()) != null) {                
                       errors.append(bufferError).append("\n");     
                }
                
                //output any errors to an Option Pane for the user to read.
                if (errors.length() != 0) {
                    JOptionPane.showMessageDialog(null,
                            "Active Directory Error: \n" + errors.toString(), "Error", JOptionPane.ERROR_MESSAGE);
                    errorfree =1;
                }
                
            } catch (IOException ex) {
               JOptionPane.showMessageDialog(null,
                        "Could not create Active Directory group:" + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
            
            System.out.println(runadcreate);
            return errorfree;
        }
    }
    This is the output:
    powershell Import-Module ActiveDirectory
    powershell New-ADGroup -Path '"OU=ClearCase_Access,OU=Application_Groups,DC=gs, DC=mydomain,DC=net"' -Name. '"Test_Group"' -sAMAccountName. '"Test_Group"' -GroupCategory Security -GroupScope DomainLocal -Description. '"VOB Admin: Bill Smith"'

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,160
    Rep Power
    6

    Default Re: Trying to run Powershell from Java

    There isn't really anything in your code that would make the Java code wait until the process has actually finished executing. There are two basic ways;

    a) use waitFor()
    b) drain the getInputStream() to get the actual output of the process (you're only draining the error stream)

    These stackoverflow threads might be of use:

    Java Process with Input/Output Stream - Stack Overflow

    java - process.waitFor() never returns - Stack Overflow
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    yelllow4u is offline Member
    Join Date
    Jul 2007
    Posts
    10
    Rep Power
    0

    Default Re: Trying to run Powershell from Java

    I played around with this a bit and it still doesn't create an AD group.

    Java Code:
    public class CreateAD {
        public int creategroup(String groupname, String description) throws InterruptedException{
            String runadcreate;
            int errorfree =0;
             
             
            runadcreate = "powershell Import-Module ActiveDirectory \n" +
                          "powershell New-ADGroup -Path '\"OU=ClearCase_Access,OU=Application_Groups,DC=gs,DC=mydomain,DC=net\"' " +
                          "-Name '\"" + groupname + "\"' -sAMAccountName '\"" + groupname + "\"' -GroupCategory Security " +
                          "-GroupScope DomainLocal -Description '\"" + description + "\"'";
             
            String [] command = {"CMD", "/C", runadcreate};
             
            StringBuilder errors = new StringBuilder();
            StringBuilder output = new StringBuilder();
            ProcessBuilder probuilder = new ProcessBuilder(command);
              
            try {
                Process newad = probuilder.start();
    
                //capture any output from the CMD
                String outputOutput = null;  
                BufferedReader cmdOutput = new BufferedReader(new InputStreamReader(newad.getInputStream()));
                while ((outputOutput = cmdOutput.readLine()) != null) {                
                       output.append(outputOutput).append("\n");     
                }
                newad.waitFor();
                 
                //capture any errors
                String bufferError = null; 
                BufferedReader cmdError = new BufferedReader(new InputStreamReader(newad.getErrorStream())); 
                while ((bufferError = cmdError.readLine()) != null) {               
                       errors.append(bufferError).append("\n");    
                }
                 
                //output any errors to an Option Pane for the user to read.
                if (errors.length() != 0) {
                    JOptionPane.showMessageDialog(null,
                            "Active Directory Error: \n" + errors.toString(), "Error", JOptionPane.ERROR_MESSAGE);
                    errorfree =1;
                }
                 
            } catch (IOException ex) {
               JOptionPane.showMessageDialog(null,
                        "Could not create Active Directory group:" + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
             
            System.out.println(runadcreate);
            return errorfree;
        }
    }

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,160
    Rep Power
    6

    Default Re: Trying to run Powershell from Java

    Yes well the command code to run is also a bit weird; there is already a newline in there :/
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Trying to run Powershell from Java

    I was thinking about that newline.
    I really don't think sending multiple commands on a single exec will work will it?

    What exactly are you seeing in your logs?
    If nothing, then add some more debugging in there.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    yelllow4u is offline Member
    Join Date
    Jul 2007
    Posts
    10
    Rep Power
    0

    Default Re: Trying to run Powershell from Java

    Quote Originally Posted by Tolls View Post
    I was thinking about that newline.
    I really don't think sending multiple commands on a single exec will work will it?

    What exactly are you seeing in your logs?
    If nothing, then add some more debugging in there.
    You might have something there. I will split this up into two exec statements and see if that works. That new line in the middle of the statement is needed because the Import-Module statement needs to be on its own or Powershell freaks out. Then again I wonder if I can just use JNDI to create the group and throw Powershell out the window.

  7. #7
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,160
    Rep Power
    6

    Default Re: Trying to run Powershell from Java

    JNDI would indeed be a nice Java-centric way to do it, if you don't need powershell for anything else and you know exactly which records to create in the AD.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •