Home > Enterprise >  Manage AzureAD through Java
Manage AzureAD through Java

Time:06-21

I'm making a java project, and part of it's functionality is managing some parts of an Azure Active Directory. This is an issue, in that some features of powershell 5 don't function correctly for what I'm doing, and pwsh.exe doesn't seem to read correctly. I've copied the exact same command I put in the java file, ran it raw in powershell, worked. Ran through Java, failed.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class test {
    public static void main(String[] args) {
        String arg = "Install-Module -Name AzureAD; Import-Module AzureAD -UseWindowsPowerShell; Connect-AzureAD -Credential (New-Object System.Management.Automation.PSCredential (\"{INSERTUSERNAMEHERE}\", (ConvertTo-SecureString \"{INSERTPASSWORDHERE}\" -AsPlainText -Force)))";
        //System.out.println(arg);
        try {
            PowerShell.run(arg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
//******************************************************
public class PowerShell {
    public static String run(String param) throws IOException {
        String command = "pwsh.exe -command "   param;
        Process powerShellProcess = Runtime.getRuntime().exec(command);
        powerShellProcess.getOutputStream().close();
        String line;
        System.out.println("Standard Output:");
        BufferedReader stdout = new BufferedReader(new InputStreamReader(powerShellProcess.getInputStream()));
        while ((line =stdout.readLine()) != null) {
            System.out.println(line);
        }
        stdout.close();
        System.out.println("Standard Error:");
        BufferedReader stderr = new BufferedReader(new InputStreamReader(powerShellProcess.getErrorStream()));
        while ((line = stderr.readLine()) != null) {
            System.out.println(line);
        }
        stderr.close();
        System.out.println("Done");
        try {
            System.out.println("Press any key to exit");
            System.in.read();
        }
        catch(IOException e) {
            e.printStackTrace();
        }
        return command;
    }
    
}

When ran in powershell 7, the log in succeeds to the AzureCloud.

Account                Environment TenantId                             TenantDomain                AccountTyp
                                                                                                               e
-------                ----------- --------                             ------------                ----------
{USERNAME}             AzureCloud  ########-####-####-####-############ {DOMAIN}.com

When ran in Java, it returns this.

Standard Output:
Standard Error:
ParserError:
Line |
   1 |  . t.Automation.PSCredential ({USERNAME}, (Conver .
     |                                         ~
     | Missing argument in parameter list.

Done
Press any key to exit

it doesn't make sense that it only works in actual powershell and not when I tell it to run commands through pwsh.exe

CodePudding user response:

This appears to be happening because some of the backslashes used to escape quotes in the string are being removed before the final command-line arguments are being passed.

You should be able to fix this by changing this line:

String arg = "Install-Module -Name AzureAD; Import-Module AzureAD -UseWindowsPowerShell; Connect-AzureAD -Credential (New-Object System.Management.Automation.PSCredential (\"{INSERTUSERNAMEHERE}\", (ConvertTo-SecureString \"{INSERTPASSWORDHERE}\" -AsPlainText -Force)))";

to the below (assuming you're using an up to date version of the JDK that supports Text Blocks)

String arg = """
    Install-Module -Name AzureAD; Import-Module AzureAD-UseWindowsPowerShell; Connect-AzureAD -Credential (New-Object System.Management.Automation.PSCredential (\\"{INSERTUSERNAMEHERE}\\", (ConvertTo-SecureString \\"{INSERTPASSWORDHERE}\\" -AsPlainText -Force)))
""";

If the version of Java you're using doesn't support text blocks, then you can change that line to this (although it doesn't look as good in my opinion):

String arg = "Install-Module -Name AzureAD; Import-Module AzureAD -UseWindowsPowerShell; Connect-AzureAD -Credential (New-Object System.Management.Automation.PSCredential (\\\"{INSERTUSERNAMEHERE}\\\", (ConvertTo-SecureString \\\"{INSERTPASSWORDHERE}\\\" -AsPlainText -Force)))";
  • Related