通过Java执行ADS相关的Powershell命令在使用2种不同方式时不会产生2种不同的错误

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

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

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

但是,它使用两种不同的方法产生不同的exception/错误。

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

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


首次尝试代码:

 public String executeCommand(String hostname){ String output = ""; try{ // String firstPartCommand = "Get-ADComputer -Filter { Name -like (", secondPartCommand = ") } –Server abcd:3268 -SearchBase 'DC=domain,DC=com' | FT DNSHostName"; String firstPartCommand = "Get-ADComputer -Filter { Name -like \""+hostname+"\" } –Server abcd: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 {Name-like“hostname”} -Server abcd:3268 -SearchBase’DC = domain,DC = com’| FT DNSHostName

以下是该命令的标准输出:

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

Get-ADComputer:解析查询时出错:’Name-like hostname’错误消息:位于’13’的’语法错误’。 – > ERROR At line:1 char:1 – > ERROR + Get-ADComputer -Filter {Name-like hostname} -Server abcd … – > ERROR + ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~ – > ERROR + CategoryInfo:ParserError:(:) [Get-ADComputer],ADFilterParsingException – > ERROR + FullyQualifiedErrorId:ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Micr – > ERROR osoft.ActiveDirectory.Management.Commands。 GetADComputer – > ERROR – > ERROR


第二次尝试的代码:

 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 {Name-like“hostname”} -Server abcd:3268 -SearchBase’DC = domain,DC = com’| FT DNSHostName

搜索结果:主机名

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


从我搜索和理解的内容来看,传递给Java方法的主机名不会被视为powershell中的字符串。 这些错误与powershell有关,我对此并不熟悉。


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

所以,谈到第一种方法,我得到了第一种情况中提到的错误。 我想继续使用第一种方法!

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

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

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

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

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

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

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