0

I want to run a PowerShell command using Java on a remote windows machine, which is actually to open the inbound firewall port. script.ps1 contains the below command

PowerShell cmd:- netsh advfirewall firewall add rule name="Open Port (8077)" dir=in action=allow protocol=TCP localport=(8077)

enter image description here

The below code works fine locally. But I want to do same on a remote machine from my local machine only and I can't do anything manually (not even creating a ps1 file over there). I have admin rights on the remote computer.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestMain2 {

    public static void main(String[] args) throws IOException {
        String command = "powershell.exe \"C:\\Users\\Administrator\\Desktop\\agent_port\\script.ps1\"";

        // Executing the command
        Process powerShellProcess = Runtime.getRuntime().exec(command);
        // Getting the results
        powerShellProcess.getOutputStream().close();
        String line;
        System.out.println("Standard Output:");
        BufferedReader stdout = new BufferedReader(new InputStreamReader(powerShellProcess.getInputStream()));
        while ((line = stdout.readLine()) != null) {
            System.out.println(line);
        }
        stdout.close();
        System.out.println("Standard Error:");
        BufferedReader stderr = new BufferedReader(new InputStreamReader(powerShellProcess.getErrorStream()));
        while ((line = stderr.readLine()) != null) {
            System.out.println(line);
        }
        stderr.close();
        System.out.println("Done");

    }
}

I tried this link also :- Running Powershell script remotely through Java

5
  • Why add layers of complexity? PowerShell natively handles remote execution via invoke-command (and other methods); wrapping this up in Java is just asking for trouble. How is it that you have admin rights on the remote machine, yet "can't do anything over there"? Commented Dec 13, 2017 at 14:55
  • 1
    Your link example needs enable winrm service in remotely service. By default, Azure Windows VM does not enable winrm service. Commented Dec 14, 2017 at 9:24
  • @alroc Yes, I can't do it manually anything there because I have to do it through java code only. As we have some business requirement for that. And I have tried the below invoke command also but the 5986 port is not opened by default:- invoke-command -ComputerName "192.168.0.0" -filepath "C:\Users\Administrator\Desktop\agent_port\script.ps1" -credential "password" Commented Dec 14, 2017 at 9:25
  • You need open winrm service port(598/5986) on remotely service. Commented Dec 14, 2017 at 9:25
  • 1
    @Ankit4mjis You could use Azure Custom Script Extension to open port. I add the example how to do this. Hope it helps. Commented Dec 21, 2017 at 7:10

1 Answer 1

1

Your link example needs enable Winrm Sevice on remotely VM. By default, Azure Windows VM does not allow winrm service. So, you could not use the example.

For Azure VM, you could use Azure Custom Script Extension to do this.

You could use this example. Add following code.

        //Add Azure Custom Script Extension
        windowsVM.update()
            .defineNewExtension("shuitest")
            .withPublisher("Microsoft.Compute")
            .withType("CustomScriptExtension")
            .withVersion("1.9")
            .withMinorVersionAutoUpgrade()
            .withPublicSetting("commandToExecute", "netsh advfirewall firewall add rule name=\"Open Port 8077\" dir=in action=allow protocol=TCP localport=8077")
            .attach()
        .apply();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.