0
votes

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

1
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"?alroc
Your link example needs enable winrm service in remotely service. By default, Azure Windows VM does not enable winrm service.Shui shengbao
@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"Ankit4mjis
You need open winrm service port(598/5986) on remotely service.Shui shengbao
@Ankit4mjis You could use Azure Custom Script Extension to open port. I add the example how to do this. Hope it helps.Shui shengbao

1 Answers

1
votes

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