Merha
Merha

Reputation: 119

How to run Powershell commands via Java on Server

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.

Upvotes: 0

Views: 2774

Answers (1)

Ganesh chaitanya
Ganesh chaitanya

Reputation: 658

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.

Upvotes: 1

Related Questions