I am trying to execute a set of commands in a powershell session via java, but so far no luck. My goal is to search for a computer object in AD with domain = "domain.com".
I started with one team. Unfortunately, the following command runs successfully in my powershell hint:
Get-ADComputer -Filter { Name -like "hostname" } –Server abcd:3268 -SearchBase 'DC=domain,DC=com' | FT DNSHostName
But it creates different exceptions / errors using two different approaches.
I tried the main way to execute powershell commands and then passed the command as an argument. This did not work, as a result, another error occurred, described below.
Next, I tried using the jPowerShell library (profesorfalken) without too much luck. Check the error in the latter
Code for the first attempt:
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; } }
Output:
Get-ADComputer -Filter {hostname "}} -Server abcd: 3268 -SearchBase 'DC = domain, DC = com' | FT DNSHostName
The following is the standard output from the command:
Here is the standard command error (if any):
Get-ADComputer: request for parsing errors: "Host-name-name" Error Message: "Syntax error" at position: "13". → ERROR On line: 1 char: 1 → ERROR + Get-ADComputer -Filter {Hostname} -Server abcd .. → ERROR + ~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~ ~ → ERROR + CategoryInfo: parserError: (:) [Get-ADComputer], exclusion ADFilterParsingException → ERROR + FullyQualifiedErrorId: ActiveDirectoryCmdlet: Microsoft.ActiveDirectory.Management.ADFilterParsingException, Micr → ERROR osoft.ActiveDirectory.Management.Commands.GetADComputer → ERROR → ERROR
Code for the 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 {hostname "}} -Server abcd: 3268 -SearchBase 'DC = domain, DC = com' | FT DNSHostName
Search Result: hostname
Get-ADComputer: The positional parameter cannot be found, which takes the argument '-Server'. On line: 1 char: 1 + Get-ADComputer -Filter {Name-name "hostname"} -Server abcd .. + ~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo: InvalidArgument : (:) [Get-ADComputer], ParameterBindingException + FullyQualifiedErrorId: PositionalParameterNotFound, Microsoft.ActiveDirectory.Management.Commands.GetADComputer
From what I was looking for and understood, the host name that is passed to the Java method is not considered as a string in powershell. These errors relate to powershell, with which I have little to worry.
EDIT: After Matthias R. Jessen's answer, I get no errors in the second case; but it seems the library itself does not match the value.
So, speaking of the first method, I get an error, as mentioned in the first case. I want to continue the first method!
I almost lost faith in the JPowershell external JAR panel. I do not get an error in the second output; but without getting a way out. He behaves as if there is no team!
Please kindly help me solve this problem!