none
Not able to Execute Remote PowerShell from Java through Linux server RRS feed

  • Question


  • I have a simple PowerShell script present under remote Window Server at location C:\IAM\Skype\checkPowerShell.ps1

    Under the checkPowerShell.ps1, I have written below command:

    New-item C:\Users\test_iam\tempfolder -itemType directory

    I am using dependent jar : jsch-0.1.55.jar and Wrote below Code: 


    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.Properties;


    import com.jcraft.jsch.Channel;
    import com.jcraft.jsch.ChannelExec;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.Session;


    public class SkypeProvision {
        public SkypeProvision() {
            super();
        }
            public static void main(String[] args) {

                    try
                    {

                            String command = "powershell.exe  \"C:\\IAM\\Skype\\checkPowerShell.ps1\" ";

                    //      String command =  "Powershell.exe C:/IAM/Skype/checkPowerShell.ps1";

                String host = "*.*.*.*";  

    // Hide the hostname, User, Password knowingly 

                String user = "******";
                String password = "*******";

                JSch jsch = new JSch();
                Session session = jsch.getSession(user, host, 22);
                Properties config = new Properties();
                config.put("StrictHostKeyChecking", "no");
                session.setConfig(config);;
                session.setPassword(password);
                session.connect();

                Channel channel = session.openChannel("exec");


                ((ChannelExec)channel).setCommand(command);
                channel.setInputStream(null);
                ((ChannelExec)channel).setErrStream(System.err);

                InputStream input = channel.getInputStream();
                channel.connect();

                System.out.println("Channel Connected to machine " + host + " server with command: " + command );

                try{
                    InputStreamReader inputReader = new InputStreamReader(input);
                    BufferedReader bufferedReader = new BufferedReader(inputReader);
                    String line = null;

                    while((line = bufferedReader.readLine()) != null){
                        System.out.println(line);
                    }
                    bufferedReader.close();
                    inputReader.close();
                }catch(IOException ex){
                    ex.printStackTrace();
                }

                channel.disconnect();
                session.disconnect();
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }

    Created the jar file and running using  below command from remote server

    #javac -cp jsch-0.1.55.jar SkypeProvision.java

    # jar -cvf SkypeProvision.jar SkypeProvision.class

    # java -cp SkypeProvision.jar:jsch-0.1.55.jar SkypeProvision

    It runs the jar file without any issue and give output:

    Channel Connected to machine ****** server with command: powershell.exe  "C:\IAM\Skype\checkPowerShell.ps1"

    But it doesn't perform any function on Remote Window server.

    Can anyone please help.????

    Friday, April 19, 2019 10:54 AM

All replies

  • Please ask Java questions in a Java forum.  This forum does not support Java.


    \_(ツ)_/

    Friday, April 19, 2019 11:46 AM
    Moderator
  • Hi,

    Thanks for your question.

    As Jrv said, this is a Java problem, please ask it in a java forum.

    If you want to run your command remotely by powershell.

    Invoke-Command -ComputerName "remote server" -ScriptBlock {New-item C:\Users\test_iam\tempfolder -itemType directory}

    Best regards,

    Lee


    Just do it.

    Monday, April 22, 2019 6:11 AM
    Moderator
  • Hi,
    Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.
    Best Regards,
    Lee

    Just do it.

    Thursday, April 25, 2019 8:43 AM
    Moderator