【发布时间】:2017-02-08 17:32:34
【问题描述】:
我一直在尝试通过 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 种不同的方法产生不同的异常/错误。
我尝试了basic way of executing powershell commands,然后将命令作为参数传递给它。这不起作用,导致下面描述的不同错误。
接下来,我再次尝试使用jPowerShell library (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 { Name -like "hostname" } –Server a.b.c.d:3268 -SearchBase 'DC=domain,DC=com' | FT DNSHostName
这是命令的标准输出:
这是命令的标准错误(如果有的话):
Get-ADComputer : 解析查询时出错: 'Name -like hostname' 错误 消息:“语法错误”在位置:“13”。 -> 错误在 line:1 char:1 -> 错误 + Get-ADComputer -Filter { Name -like hostname} -Server a.b.c.d ... -> 错误 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ -> 错误 + 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 { Name -like "hostname" } –Server a.b.c.d:3268 -SearchBase 'DC=domain,DC=com' | FT DNSHostName
搜索结果:主机名
Get-ADComputer : 位置参数不能 发现接受参数“–Server”。在行:1 字符:1 + Get-ADComputer -Filter { Name -like "hostname" } –Server a.b.c.d ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-ADComputer], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
根据我的搜索和理解,传递给 Java 方法的主机名在 powershell 中没有被视为字符串。这些错误与我没有太多经验的 powershell 有关。
编辑:Mathias R. Jessen's 回复后,我在第二种情况下没有收到任何错误;但是,图书馆本身似乎并不正确。
所以,谈到第一种方法,我得到了第一种情况中提到的错误。我只想继续第一种方法!
我几乎对外部 jPowershell JAR 失去了信心。我在第二个输出中没有收到错误;但是,既没有得到输出。它的行为就像没有命令的输出一样!
请求帮助我解决这个问题!
【问题讨论】:
-
第二个示例中有一个破折号 - 将
–Server替换为-Server -
@MathiasR.Jessen - 谢谢,现在我没有在第二个输出中得到错误;但是,既没有得到输出。它的行为就像没有命令的输出一样!另外,你能帮助你解释为什么第一个代码不起作用吗?
标签: java powershell active-directory command command-execution