0

I want to run PowerShell scripts via a java Servlet. In localhost (Windows 10 Pro 64bit) testing, everything workes fine. Now I deployed my code on a remote server (Tomcat 8.5 on Windows Server 2012 R2 64bit) and tried to run it, but there is no output from the powershell scripts. They seem not to run at all or don't communicate back. (there is no cmd_line printed at all and check_ps_execution stays false.)

I've tried already to change '"powershell " + ps_..._status.ps1 " + username' to '"cmd /C powershell " + ps_..._status.ps1 " + username' to run it via CLI instead of PowerShell but it had the same effect.

There is no error message, but no output neither.

I run the ps-Script directly at the server via cmd and powershell and in both cases it worked fine. They are located in the same path as on localhost.

public static boolean checkForwardingStatus(String username) throws IOException {
    boolean forwarding = false;
    boolean check_ps_execution = false;

    Runtime runtime = Runtime.getRuntime();
    Process proc = runtime.exec("cmd /C powershell " + ps_script_path + "check_forwarding_status.ps1 " + username);
    InputStream is = proc.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader reader = new BufferedReader(isr);
    String cmd_line;
    while ((cmd_line = reader.readLine()) != null) {
        check_ps_execution = true;
        logger.info(cmd_line);
        if (cmd_line.contains("ForwardingAddress")) {
            if (cmd_line.contains("[something it should contain]")) {
                forwarding = true;
            }
        }
    }
    reader.close();
    proc.getOutputStream().close();
    if (!check_ps_execution) {
        logger.error("PS Script checkForwardingStatus did not run!");
    }
    logger.info("Forwarding: " + forwarding);
    return forwarding;
}

The PowerShell Skript:

$user_name=$args[0]
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://[server]/PowerShell/ -Authentication Kerberos
Import-PSSession $Session
Get-Mailbox -Identity "$user_name" | fl *Forwarding*, *DeliverToMailboxAndForward*, *WindowsEmailAddress*

What can I try to run the script?

UPDATE: When I log onto the server, run the PowerShell and execute my example script with my own username it works fine. But if I try a different username I get following error code:

FullyQualifiedErrorId : ErrorNoCommandsImportedBecauseOfSkipping,Microsoft.PowerShell.Commands.ImportPSSessionCommand

So I guess it's an authorization problem.

Merha
  • 119
  • 13

1 Answers1

1

File was not run, because server was not able to find the file. Once the file is deployed onto the server, you might need to download the file from server onto the machine in which the war is deployed.

Try to implement error stream for the RunTime exec to see the error exactly. You can follow https://stackoverflow.com/a/5748494/4597596 for the same.

Ganesh chaitanya
  • 638
  • 6
  • 18
  • Isn't it possible to run the script on the server directly? I do not want to bother the user with file execution, and there are about 4-5 scripts to be run via one process, and the output of the scripts is handled by the server. – Merha May 08 '19 at 09:56
  • Your server does not have a powershell env to execute the script in the server. So you have to move the file to an env where you can execute it. – Ganesh chaitanya May 08 '19 at 10:09
  • I could log on the server, run a power shell and execute the script, so I don't understand why java shouldn't be able to do that too. I guess it's more an authorization problem. I'll edit my question accordingly. – Merha May 08 '19 at 11:24
  • When you log into server, you are execution env is the machine OS, but in server, the execution env OS is your app server which doesn't have Powershell in it. – Ganesh chaitanya May 08 '19 at 16:22
  • 1
    This really helped me, thank you. The error stream made clear that the script was run under [server name] which was not authorized to do so. I now give credentials to the commands in the powershell scripts and it works :D – Merha May 15 '19 at 12:13
  • Cool, happy to help – Ganesh chaitanya May 15 '19 at 12:17