我一直在尝试通过 java 在 powershell 会话中执行一组命令,但还没有成功。我的目标是在 AD 中搜索域为“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".
但是,它通过两种不同的方法产生不同的异常/错误。
我已经尝试过执行powershell命令的基本方法 https://stackoverflow.com/questions/29545611/executing-powershell-commands-in-java-program,然后将命令作为参数传递给它。这不起作用,导致了下面描述的不同错误。
接下来,我尝试使用jPowerShell 库 (profesorfalken) https://github.com/profesorfalken/jPowerShell再次没有运气。检查最后的错误
第一次尝试的代码:
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" } –服务器
a.b.c.d:3268 -SearchBase 'DC=domain,DC=com' | FT DNS 主机名
这是命令的标准输出:
这是命令的标准错误(如果有):
Get-ADComputer:解析查询时出错:“名称类似主机名”错误
消息:“语法错误”,位置:“13”。 -> 错误 在行:1 字符:1
-> 错误
+ Get-ADComputer -Filter { Name -like hostname} -Server a.b.c.d ... -> 错误
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~
-> 错误
+ CategoryInfo : ParserError: (:) [Get-ADComputer], ADFilterParsingException -> 错误
+ FullQualifiedErrorId: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();
}
}
Output:
Get-ADComputer -Filter { Name -like "hostname" } –服务器
a.b.c.d:3268 -SearchBase 'DC=domain,DC=com' | FT DNS 主机名
搜索结果:主机名
Get-ADComputer:位置参数不能
发现接受参数“–Server”。行:1 字符:1
+ Get-ADComputer -Filter { 名称 - 如“主机名” } –服务器 a.b.c.d ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADComputer], ParameterBindingException
+ FullQualifiedErrorId:PositionalParameterNotFound、Microsoft.ActiveDirectory.Management.Commands.GetADComputer
根据我的搜索和理解,传递给 Java 方法的主机名在 powershell 中不会被视为字符串。这些错误与 powershell 有关,我对此没有太多经验。
EDIT: After 马蒂亚斯·R·杰森 https://stackoverflow.com/users/712649/mathias-r-jessen回复,在第二种情况下我没有收到任何错误;但是,图书馆本身似乎并不正确。
因此,谈论第一种方法时,我遇到了第一种情况中提到的错误。我只想继续第一种方法!
我几乎对外部 jPowershell JAR 失去了信心。我在第二个输出中没有收到错误;但是,都没有得到输出。它的行为就好像没有命令的输出一样!
请求好心人帮我解决这个问题!