0

I want to run a PowerShell command for create user using Java on a remote windows machine, I am writing this code in eclipse. And below is the output, tried every reference and forms to resolve this issue can someone help me with this.

The command works on PowerShell command line, but it is not working in Java.

        public class PowerShellSession {
        private static String subModule = "PowerShellSession";
        String targetIpAddress;
        String username;
        String password;
    
        public static Object connectPShellLock = new Object();
    
        public PowerShellSession() {}
    
    
        public void exec(String cmd, String credentials)  { 
            //String ex =   "New-LocalUser -Name \"User02\" -Description \"Description of this account.\" -NoPassword";
            String ex = credentials +" Invoke-Command -ScriptBlock {" + cmd + "} -ComputerName " + targetIpAddress +" -Credential $mycred";
    
            String[] args = new String[] { "powershell", ex};
            try {
                execRemote(args);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public void close() {
            String command = "Exit-PSSession";
            String[] args = new String[] { "powershell", command};
            try {
                execRemote(args);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    
        private String getCredentials(String domain, String userName,
                String password) throws IOException {
            String creds = "$Username = '"+userName+"';$PlainPassword ='" + password
                    + "'; $SecurePassword = ConvertTo-SecureString -AsPlainText $PlainPassword -Force;"
                    + "$mycred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $SecurePassword;";
            //creds += "$session = New-PSSession -ComputerName " + domain + " -Credential $mycred;";
    
            String[] args = new String[] { "powershell", creds};
            execRemote(args);
            return creds;
        }
    
        private void execRemote(String[] arguments) throws IOException {
            ProcessBuilder builder = new ProcessBuilder(arguments);
            builder.redirectErrorStream(true);
            Process process = builder.start();
            doProcessIO(process);
        }
    
        // Do the IO for a passed process
        private void doProcessIO(Process p) throws IOException {
            p.getOutputStream().close();
            String line;
            System.out.println("Output:");
            BufferedReader stdout = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));
            while ((line = stdout.readLine()) != null) {
                System.out.println(line);
            }
            stdout.close();
            System.out.println("Error:");
            BufferedReader stderr = new BufferedReader(new InputStreamReader(
                    p.getErrorStream()));
            while ((line = stderr.readLine()) != null) {
                System.out.println(line);
            }
            stderr.close();
            System.out.println("Done");
        }
    
        public static void main(String[] args) throws IOException {
            String ip_add  = "192.168.193.101";
            String user = "gs-259412";
            String pass  = "Windows412";
    
            PowerShellSession psSession = new PowerShellSession();
            String credentials = psSession.getCredentials(ip_add, user, pass);
            psSession.targetIpAddress = ip_add;//;
    
    
    
            String cmdd = "New-LocalUser -Name \"User02\" -Description \"Description of this account.\" -NoPassword";//"Get-Culture";
            if(!credentials.equals("")) {
    
    
                psSession.exec(cmdd, credentials);
    
                System.out.println("Finished PowerShell remote session.");
    
            }
            psSession.close();
        }
    
    }


Output of this program :

Output: Error: Done Output: A positional parameter cannot be found that accepts argument 'of'. + CategoryInfo : InvalidArgument: (:) [New-LocalUser], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewLocalUserCommand + PSComputerName : 192.168.193.131

Error: Done Finished PowerShell remote session.

I tried this link also enter link description here

  • Wheres the error? – Scepticalist Jan 10 '22 at 08:01
  • @Scepticalist I am getting error in output A positional parameter cannot be found that accepts argument 'of'. + CategoryInfo : InvalidArgument: (:) [New-LocalUser], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewLocalUserCommand + PSComputerName : 192.168.193.131 – swaps_stack Jan 10 '22 at 08:39
  • @matt yes they are just examples, in my code i am using actual username and password. – swaps_stack Jan 10 '22 at 08:56
  • Why are you redirecting error and reading the error stream? – matt Jan 10 '22 at 09:11
  • 1
    Given the error message, I'd be looking into quote and space processing. Change the value of `Description` into something without spaces for a test. Replace double quotes in the command with single ones. Or double the double quotes. – n0rd Jan 10 '22 at 09:15
  • From the looks of the error message, your arguments are incorrect. Also, shouldn't each argument be it's own array element for process builder? – matt Jan 10 '22 at 09:15

0 Answers0