当前位置: 首页 > 面试题库 >

通过Java执行与ADS相关的Powershell命令不起作用,当使用2种不同方式时会给出2种不同错误

戴品
2023-03-14
问题内容

我一直在尝试通过Java在Powershell会话中执行一组命令,但还没有运气。我的目标是在AD中使用domain =“
domain.com”搜索计算机对象

我从一个命令开始。不幸的是,以下命令已在我的Powershell提示符中成功运行:

Get-ADComputer -Filter { Name -like "hostname" } –Server a.b.c.d:3268 -SearchBase 'DC=domain,DC=com' | FT DNSHostName
# hostname is actual hostname provided by user and accepted in argument of Java methods
# a.b.c.d is the IP-Address of my domain controller, and I'm trying to search a computer object in AD with the domain = "domain.com".

但是,它通过2种不同的方法产生不同的异常/错误。

  1. 我尝试了执行powershell命令的基本方法,然后将命令作为参数传递给它。那没有用,导致了下面描述的不同错误。

  2. 接下来,我尝试使用jPowerShell库(profesorfalken)再次没有运气。最后检查错误

首次尝试的代码:

public String executeCommand(String hostname){
        String output = "";
        try{
//          String firstPartCommand = "Get-ADComputer -Filter { Name -like (", secondPartCommand = ") } –Server a.b.c.d:3268 -SearchBase 'DC=domain,DC=com' | FT DNSHostName"; 
            String firstPartCommand = "Get-ADComputer -Filter { Name -like \""+hostname+"\" } –Server a.b.c.d:3268 -SearchBase \'DC=domain,DC=com\' | FT DNSHostName";

            Runtime rt = Runtime.getRuntime();
            String[] cmds = new String[]{
                "powershell.exe", firstPartCommand.trim()
            };
            System.out.println(firstPartCommand);

            Process pr = rt.exec(cmds);
            pr.getOutputStream().close();
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(pr.getErrorStream()));

            System.out.println("Here is the standard output of the command:\n");
            String s = null;
            while ((s = stdInput.readLine()) != null) {
            System.out.println(s+" -> OUTPUT");
            output+=s;
            //displayTF.setText(s);
            }
            stdInput.close();
            System.out.println("Here is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
            System.out.println(s+" -> ERROR");
            }
            stdError.close();
            return output;
        }
        catch(Exception ex){
            ex.printStackTrace(System.out);
            output = "Some exception occured, SORRY!";
            return output;
        }
    }

输出:

Get-ADComputer -Filter {名称-like“ hostname”} –服务器abcd:3268 -SearchBase’DC =
domain,DC = com’| FT DNSHostName

这是命令的标准输出:

这是命令的标准错误(如果有):

Get-ADComputer:解析查询时出错:名称类似主机名错误消息:位置13时出现语法错误。->错误在第1行:1字符:->错误+ Get-
ADComputer-过滤器{名称-like主机名}-服务器abcd …->错误+ ~ ~ ~ ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~~ ~~->错误+
CategoryInfo:ParserError:(:) [Get-ADComputer],ADFilterParsingException->错误+
FullyQualifiedErrorId:ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Micr->错误osoft.ActiveDirectory.Management.Commands。
GetADComputer->错误->错误

第二次尝试的代码:

public String execute(String hostname){
        String output = "";
        PowerShell powershell = null;
        try{            
            powershell = PowerShell.openSession();
//            String cmd = "$variable = \""+hostname+"\"";
//            //Execute a command in PowerShell session
//            PowerShellResponse response = powershell.executeCommand(cmd);
//            //Print results
//            System.out.println("Variable Initialisation:" + response.getCommandOutput());
            String firstPartCommand = "Get-ADComputer -Filter { Name -like \"", secondPartCommand = "\" } –Server 10.0.239.236:3268 -SearchBase 'DC=AD,DC=SBI' | FT DNSHostName"; 
            String finalCommand = firstPartCommand+hostname+secondPartCommand;
            System.out.println(finalCommand);
            PowerShellResponse response = powershell.executeCommand(finalCommand);
            //PowerShellResponse response = powershell.executeCommand("Get-Process powershell -FileVersionInfo");
            output = response.getCommandOutput();
            System.out.println("Search result: "+hostname+"\n" + output);
            return output;
        }
        catch(Exception ex){
            return "Failed!";
        }
        finally {
       //Always close PowerShell session to free resources.
            if (powershell != null)
                powershell.close();
        }
    }

输出:

Get-ADComputer -Filter {名称-like“ hostname”} –服务器abcd:3268 -SearchBase’DC =
domain,DC = com’| FT DNSHostName

搜索结果:主机名

Get-ADComputer:找不到接受参数“ -Server”的位置参数。在第1行:char:1 + Get-ADComputer -Filter
{名称-like“ hostname”} –服务器abcd … + ~ ~ ~ ~
~ ~ ~ ~ ~ ~ ~ ~ ~~~~ + + CategoryInfo:
InvalidArgument:(:) [Get-ADComputer],ParameterBindingException +
FullyQualifiedErrorId:PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

根据我的搜索和了解,在Powershell中不会将传递给Java方法的主机名视为字符串。这些错误与powershell有关,我经验不足。

编辑: 在Mathias R. Jessen的)回复之后,在第二种情况下我没有收到任何错误;但是,似乎图书馆本身并没有达到标准。

因此,在谈论第一种方法时,我遇到了第一种情况中提到的错误。我只想采用第一种方法!

我几乎对外部jPowershell JAR失去了信心。我在第二个输出中没有收到错误;但是,都没有得到输出。它的行为就像没有该命令的输出!

要求请帮助我解决这个问题!


问题答案:

经过将近3天的努力,我发现问题出在预期的命令字符串中。

正确的命令(对于第一种情况)应为:

String firstPartCommand = "Get-ADComputer -Filter { Name -eq \'"+hostname+"\' } 
-Server a.b.c.d:3268 -SearchBase \'DC=domain,DC=com\' | Select DNSHostName";

正确的命令(对于第二种情况)应为:

String firstPartCommand = "Get-ADComputer -Filter { Name -eq \'", 
secondPartCommand = "\' }  -Server a.b.c.d:3268 -SearchBase \'DC=domain,DC=com\' |
 Select DNSHostName";


 类似资料:
  • 问题内容: 我正在关注一个React教程,这是作者提供的用于创建基本React组件的示例代码: 他声称这是ES6。 但是后来我看到了另一种创建组件的方法。 嗯,我现在很困惑。是否有任何标准的反应方式? 问题答案: 在React中,您可以创建所谓的有状态和无状态功能组件。无状态组件是不需要维护状态的简单可重用组件。这是一个简短的演示(http://codepen.io/PiotrBerebecki/

  • 问题内容: 我想使用2个不同的PHP页面发送2个具有不同值的表单时遇到问题。 我的ajax代码如下: 对于第二种形式,我更改了var并将其替换为 这是行不通的,因为我真的不知道原因。我在哪里做错了? 问题答案: 不要重复自己。 如果需要两次相同的功能,请不要复制和粘贴代码。创建一个函数,将变量用作变量部分,两次调用该函数。

  • 但后来我看到了另一种创建组件的方法。 嗯,我现在很困惑。在React中有什么标准的做事方式吗?

  • 而输出是 谁能详细说明一下1380605909318和61341428160000之间的区别?

  • 问题内容: 我正在尝试运行以下内容。 如果我删除以下行: 至.... 一切都会正常。如果没有,我得到以下错误: 命令不同步; 您现在不能运行此命令 在研究,我认为这可能是由于多个库MySQLi查询同时运行,其中使用但在所有样品和通用数据导向似乎并不适用。 有任何想法吗? 问题答案: MySQL客户端不允许您执行新的查询,在该查询中仍需要从正在进行的查询中获取行。有关常见错误,请参见MySQL文档中

  • 我正在构建一个基于阿尔卑斯山的docker图像。 当我以shell形式运行时,它工作正常,但当我以exec形式运行时,它会给出 /箱子/箱:[sh,:未找到 我试着使用bin/sh、sh、bin/ash、ash。所有这些都有相同的错误。