我是一个使用Talend Open Studio的java新手,我想知道是否可以用 "Import-Module ActiveDirectory "执行powershell.exe,然后用 "Import-Module ... "在不重载powershell的情况下启动动态命令。
我知道这行不通,但我的想法可以这样翻译。
Runtime.getRuntime().Exec("powershell.exe");
Runtime.getRuntime().Exec("Import-Module ActiveDirectory");
Runtime.getRuntime().Exec("Get-ADUser TestLogin1");
Runtime.getRuntime().Exec("Set-ADUser -Identity TestLogin1 -Company MyCompany1");
Runtime.getRuntime().Exec("Get-ADUser TestLogin2");
Runtime.getRuntime().Exec("Set-ADUser -Identity TestLogin2 -Company MyCompany2");
为了使这个工作,我必须做......。
Runtime.getRuntime().Exec("powershell.exe /c Import-Module ActiveDirectory ; Get-ADUser TestLogin");
Runtime.getRuntime().Exec("powershell.exe /c Import-Module ActiveDirectory ; Set-ADUser -Identity TestLogin1 -Company MyCompany1");
Runtime.getRuntime().Exec("powershell.exe /c Import-Module ActiveDirectory ; Get-ADUser TestLogin_2");
Runtime.getRuntime().Exec("powershell.exe /c Import-Module ActiveDirectory ; Set-ADUser -Identity TestLogin2 -Company MyCompany2");
我不想通过脚本文件,因为第一次更新命令(Set-ADUser)会对下一次更新命令产生影响。
谢谢。
我从profesorfalken的jPowerShell库中找到了一个解决方案。
https:/github.comprofesorfalkenjPowerShell。
//Creates PowerShell session (we can execute several commands in the same session)
try (PowerShell powerShell = PowerShell.openSession()) {
//Execute a command in PowerShell session
PowerShellResponse response = powerShell.executeCommand("Import-Module ActiveDirectory");
//Get an ADUser
PowerShellResponse response = powerShell.executeCommand("Get-ADUser TestLogin1");
//Print results ADUser
System.out.println("PS : " + response.getCommandOutput());
//Set an ADUser
PowerShellResponse response = powerShell.executeCommand("Set-ADUser -Identity TestLogin1 -Company MyCompany1");
//Get an ADUser
PowerShellResponse response = powerShell.executeCommand("Get-ADUser TestLogin2");
//Print results ADUser
System.out.println("PS : " + response.getCommandOutput());
//Set an ADUser
PowerShellResponse response = powerShell.executeCommand("Set-ADUser -Identity TestLogin2 -Company MyCompany2");
} catch(PowerShellNotAvailableException ex) {
//Handle error when PowerShell is not available in the system
//Maybe try in another way?
}