Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在服务器上通过Java运行Powershell命令_Java_Powershell - Fatal编程技术网

如何在服务器上通过Java运行Powershell命令

如何在服务器上通过Java运行Powershell命令,java,powershell,Java,Powershell,我想通过java Servlet运行PowerShell脚本。在本地主机(Windows10Pro 64位)测试中,一切正常。现在我将代码部署在远程服务器上(Windows server 2012 R2 64位上的Tomcat 8.5),并尝试运行它,但powershell脚本没有输出。他们似乎根本不跑,或者根本不回电话。(根本没有打印cmd\u行,并且检查执行情况保持false) 我已经尝试将““powershell”+ps_…\u status.ps1”+用户名”更改为““cmd/C pow

我想通过java Servlet运行PowerShell脚本。在本地主机(Windows10Pro 64位)测试中,一切正常。现在我将代码部署在远程服务器上(Windows server 2012 R2 64位上的Tomcat 8.5),并尝试运行它,但powershell脚本没有输出。他们似乎根本不跑,或者根本不回电话。(根本没有打印
cmd\u行
,并且
检查执行情况
保持
false

我已经尝试将“
“powershell”+ps_…\u status.ps1”+用户名
”更改为“
“cmd/C powershell”+ps_…\u status.ps1”+用户名
”,以通过CLI而不是powershell运行,但效果相同

没有错误消息,但也没有输出

我通过cmd和powershell直接在服务器上运行ps脚本,在这两种情况下都运行良好。它们位于本地主机上的相同路径中

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;
}
PowerShell脚本:

$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*
我可以尝试运行什么脚本

更新: 当我登录到服务器时,运行PowerShell并使用我自己的用户名执行我的示例脚本,它工作正常。但如果我尝试其他用户名,我会得到以下错误代码:

FullyQualifiedErrorId : ErrorNoCommandsImportedBecauseOfSkipping,Microsoft.PowerShell.Commands.ImportPSSessionCommand

所以我想这是授权问题。

文件没有运行,因为服务器无法找到该文件。一旦文件部署到服务器上,您可能需要将文件从服务器下载到部署war的机器上


尝试为运行时exec实现错误流,以便准确地查看错误。你也可以照做

难道不能直接在服务器上运行脚本吗?我不想在执行文件时打扰用户,一个进程要运行大约4-5个脚本,脚本的输出由服务器处理。您的服务器没有powershell env在服务器中执行脚本。因此,您必须将文件移动到一个可以执行它的环境中。我可以登录服务器,运行powershell并执行脚本,所以我不明白为什么java也不能这样做。我想这更像是授权问题。我将相应地编辑我的问题。当您登录到服务器时,您是execution env,它是机器操作系统,但在服务器中,execution env OS是您的应用程序服务器,其中没有Powershell。这确实帮助了我,谢谢。错误流表明脚本是在未经授权的[server name]下运行的。现在,我为powershell脚本中的命令提供凭据,它可以工作:D