通过Java管理AzureAD



我正在做一个java项目,它的部分功能是管理Azure Active Directory的某些部分。这是一个问题,因为powershell 5的一些功能不适合我正在做的事情,pwsh.exe似乎无法正确读取。我复制了我在java文件中输入的完全相同的命令,在powershell中原始运行,成功了。运行Java,失败。

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;
}

}

在powershell 7中运行,登录成功。

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

当在Java中运行时,它返回这个

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

它只在实际的powershell中工作,而不是当我告诉它通过pwsh.exe运行命令时,这是没有意义的

出现这种情况似乎是因为在传递最后的命令行参数之前,字符串中用于转义引号的一些反斜杠被删除了。

你应该可以通过修改这一行来解决这个问题:

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)))";

到下面(假设您使用的是支持文本块的JDK的最新版本)

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)))
""";

如果你使用的Java版本不支持文本块,那么你可以将这一行更改为以下内容(尽管在我看来它看起来不那么好):

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)))";

最新更新