小编典典

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

java

我一直在尝试通过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失去了信心。我在第二个输出中没有收到错误;但是,都没有得到输出。它的行为就像没有该命令的输出!

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


阅读 299

收藏
2020-11-23

共1个答案

小编典典

经过将近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";
2020-11-23