private PowerShell initalize(String powerShellExecutablePath) throws PowerShellNotAvailableException {
        String codePage = PowerShellCodepage.getIdentifierByCodePageName(Charset.defaultCharset().name());
        ProcessBuilder pb;

        //Start powershell executable in process
        if (OSDetector.isWindows()) {
            pb = new ProcessBuilder("cmd.exe", "/c", "chcp", codePage, ">", "NUL", "&", powerShellExecutablePath,
                    "-ExecutionPolicy", "Bypass", "-NoExit", "-NoProfile", "-Command", "-");
        } else {
            pb = new ProcessBuilder(powerShellExecutablePath, "-nologo", "-noexit", "-Command", "-");
        }

        //Merge standard and error streams
        pb.redirectErrorStream(true);

        try {
            //Launch process
            p = pb.start();

            if (p.waitFor(5, TimeUnit.SECONDS) && !p.isAlive()) {
                throw new PowerShellNotAvailableException(
                        "Cannot execute PowerShell. Please make sure that it is installed in your system. Errorcode:" + p.exitValue());
            }
        } catch (IOException | InterruptedException ex) {
            throw new PowerShellNotAvailableException(
                    "Cannot execute PowerShell. Please make sure that it is installed in your system", ex);
        }

        //Prepare writer that will be used to send commands to powershell
        this.commandWriter = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(p.getOutputStream())), true);

        // Init thread pool. 2 threads are needed: one to write and read console and the other to close it
        this.threadpool = Executors.newFixedThreadPool(2);

        //Get and store the PID of the process
        this.pid = getPID();

        return this;
    }
private PowerShell initalize ( String powerShellExecutablePath ) throws PowerShellNotAvailableException { String codePage = PowerShellCodepage . getIdentifierByCodePageName ( Charset . defaultCharset ( ) . name ( ) ) ; ProcessBuilder pb ; if ( OSDetector . isWindows ( ) ) { pb = new ProcessBuilder ( "cmd.exe" , "/c" , "chcp" , codePage , ">" , "NUL" , "&" , powerShellExecutablePath , "-ExecutionPolicy" , "Bypass" , "-NoExit" , "-NoProfile" , "-Command" , "-" ) ; } else { pb = new ProcessBuilder ( powerShellExecutablePath , "-nologo" , "-noexit" , "-Command" , "-" ) ; } pb . redirectErrorStream ( true ) ; try { p = pb . start ( ) ; if ( p . waitFor ( 5 , TimeUnit . SECONDS ) && ! p . isAlive ( ) ) { throw new PowerShellNotAvailableException ( "Cannot execute PowerShell. Please make sure that it is installed in your system. Errorcode:" + p . exitValue ( ) ) ; } } catch ( IOException | InterruptedException ex ) { throw new PowerShellNotAvailableException ( "Cannot execute PowerShell. Please make sure that it is installed in your system" , ex ) ; } this . commandWriter = new PrintWriter ( new OutputStreamWriter ( new BufferedOutputStream ( p . getOutputStream ( ) ) ) , true ) ; this . threadpool = Executors . newFixedThreadPool ( 2 ) ; this . pid = getPID ( ) ; return this ; }
Initializes PowerShell console in which we will enter the commands