[英]executing ADS related Powershell command through Java does not work giving 2 different errors when using 2 different ways
I have been trying to execute a set of commands in a powershell session through java, with no luck yet. 我一直在尝试通过java在powershell会话中执行一组命令,但还没有运气。 My aim is to search a computer object in AD with the domain = "domain.com".
我的目标是使用domain =“ domain.com”搜索AD中的计算机对象。
I started with a single command. 我从一个命令开始。 Unfortunately, the following command successfully runs in my powershell prompt:
不幸的是,以下命令已在我的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".
But, it produces different exceptions/errors with 2 different approaches. 但是,它通过两种不同的方法产生不同的异常/错误。
I have tried the basic way of executing powershell commands , and then passing the command as argument to it. 我尝试了执行powershell命令的基本方法 ,然后将命令作为参数传递给它。 That did not work, resulted in different error described below.
那没有用,导致了下面描述的不同错误。
Next, I tried using jPowerShell library (profesorfalken) with no luck again. 接下来,我尝试使用jPowerShell库(profesorfalken)再次没有运气。 Check the error in the last
最后检查错误
Code for first attempt: 首次尝试的代码:
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;
}
}
Output: 输出:
Get-ADComputer -Filter { Name -like "hostname" } –Server abcd:3268 -SearchBase 'DC=domain,DC=com' |
Get-ADComputer -Filter {名称-like“ hostname”} –服务器abcd:3268 -SearchBase'DC = domain,DC = com'| FT DNSHostName
FT DNSHostName
Here is the standard output of the command:
这是命令的标准输出:
Here is the standard error of the command (if any):
这是命令的标准错误(如果有):
Get-ADComputer : Error parsing query: ' Name -like hostname' Error Message: 'syntax error' at position: '13'.
Get-ADComputer:分析查询时出错:名称类似于主机名错误消息:位置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
->错误在第1行:1个字符:1->错误+ Get-ADComputer-过滤器{名称-like主机名}-服务器abcd ...->错误+ ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~->错误+ CategoryInfo:ParserError:(:) [Get-ADComputer],ADFilterParsingException->错误+ FullyQualifiedErrorId:ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Micr->错误osoft.ActiveDirectory.Management.Commands。 GetADComputer->错误->错误
Code for second attempt: 再次尝试的代码:
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();
}
}
Output: 输出:
Get-ADComputer -Filter { Name -like "hostname" } –Server abcd:3268 -SearchBase 'DC=domain,DC=com' |
Get-ADComputer -Filter {名称-like“ hostname”} –服务器abcd:3268 -SearchBase'DC = domain,DC = com'| FT DNSHostName
FT DNSHostName
Search result: hostname
搜索结果:主机名
Get-ADComputer : A positional parameter cannot be found that accepts argument '–Server'.
Get-ADComputer:找不到接受参数“ -Server”的位置参数。 At line:1 char:1 + Get-ADComputer -Filter { Name -like "hostname" } –Server abcd ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-ADComputer], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
在第1行:char:1 + Get-ADComputer -Filter {名称-like“ hostname”} –服务器abcd ... + ~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + CategoryInfo: InvalidArgument:(:) [Get-ADComputer],ParameterBindingException + FullyQualifiedErrorId:PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
From what I've searched and understood, the hostname which is passed to the Java method is not getting treated as a string in the powershell. 根据我的搜索和了解,在Powershell中不会将传递给Java方法的主机名视为字符串。 These errors are pertaining to powershell, which I'm not much experienced with.
这些错误与powershell有关,我经验不足。
EDIT: After Mathias R. Jessen's reply, I'm not getting any error in the 2nd case; 编辑:在Mathias R. Jessen的回复之后,在第二种情况下我没有收到任何错误; but, it seems the library itself is not correct up to the mark.
但是,似乎图书馆本身并没有达到标准。
So, talking about the first method, I'm getting the error as mentioned in the first case. 因此,在谈论第一种方法时,我遇到了第一种情况中提到的错误。 I want to get on with the first method only!
我只想采用第一种方法!
I have, almost, lost my faith in the external jPowershell JAR. 我几乎对外部jPowershell JAR失去了信心。 I'm not getting the error in the 2nd output;
我在第二个输出中没有收到错误; but, neither getting the output.
但是,都没有得到输出。 It behaves as if there is no output of the command!
它的行为就像没有该命令的输出!
Request to kindly help me solve this problem! 要求请帮助我解决这个问题!
After struggling for almost 3 days, I found the problem to be in the command string, as expected. 经过将近3天的努力,我发现问题出在预期的命令字符串中。
The correct command (for the first case) should be: 正确的命令(对于第一种情况)应为:
String firstPartCommand = "Get-ADComputer -Filter { Name -eq \'"+hostname+"\' }
-Server a.b.c.d:3268 -SearchBase \'DC=domain,DC=com\' | Select DNSHostName";
The correct command (for the second case) should be: 正确的命令(对于第二种情况)应为:
String firstPartCommand = "Get-ADComputer -Filter { Name -eq \'",
secondPartCommand = "\' } -Server a.b.c.d:3268 -SearchBase \'DC=domain,DC=com\' |
Select DNSHostName";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.