运行 从 Java 远程批处理文件以在 exchange 2010 上创建邮箱
run batch file remotely to create mailbox on exchange 2010 from Java
我正在尝试从 java 在 Exchange 服务器上创建邮箱。我正在服务器上创建一个批处理文件并尝试执行它,因此它 运行s 并创建了邮箱。正在创建批处理文件,但尚未创建。我发现它正在尝试在我的本地计算机而不是服务器中查找文件。有什么方法可以强制它在服务器上 运行 吗?我的代码如下
package com.exchange;
import java.io.*;
public class CreateMailbox { public static void main(String[] args) throws IOException, InterruptedException { try {
String COMMAND1="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
-command \".'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1';Connect-ExchangeServer
-auto;Enable-Mailbox admin123@imbl.corp -Database \"TESTDB\"\"> C:\Users\admin\Desktop\ActiveSyncDeviceAccessRule_output.txt 2>C:\Users\admin\Desktop\standardError.txt";
System.out.println(COMMAND1);
String ErrorCommand1 = "echo %errorlevel% >C:\exitCode.txt";
String SPACE = " ";
final File file = new File("\\192.168.205.245\C$\Users\admin\Desktop\Create.bat");
Boolean CreatenewFileVar = file.createNewFile();
if(CreatenewFileVar)
{
System.out.println("File Created in: " + file);
}
PrintWriter writer = new PrintWriter(file, "UTF-8");
writer.println(COMMAND1);
writer.println(ErrorCommand1);
writer.println(SPACE);
writer.close();
Process p1 = Runtime.getRuntime().exec("cmd /c Start \\192.168.205.245\c$\Users\admin\Desktop\Create.bat");
int exitVal = p1.waitFor();
System.out.println("Exited with error code "+exitVal);
} catch (final IOException e) {
System.out.println("Error " + e); } } }
如有任何建议/帮助,我们将不胜感激。我不是高级程序员,并且是在互联网的帮助下完成此编码的。
我会使用另一种解决方案,即 Remote Powershell(更多信息 here, and a performance tip here)。因此,您可以调整代码以执行以下操作:
$CASServer = 'fqdn'
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://$CASServer/powershell" -Authentication Kerberos
Import-PSSession $Session
$password = ConvertTo-SecureString -String "Pa55w0rd!"
New-Mailbox -UserPrincipalName chris@contoso.com -Alias chris -Database "Mailbox Database 1" -Name ChrisAshton -OrganizationalUnit Users -Password $password -FirstName Chris -LastName Ashton -DisplayName "Chris Ashton" -ResetPasswordOnNextLogon $true
这是远程与 MS Exchange 交互的正确方法。没有人会尝试创建一个 CMD,然后触发它并在之后将其丢弃,因为它太复杂了(还要考虑 Exchange Server 端的安全性)。顺便说一句,在 Java 中实现上述内容,检查 Whosebug 发布 here and here.
我正在尝试从 java 在 Exchange 服务器上创建邮箱。我正在服务器上创建一个批处理文件并尝试执行它,因此它 运行s 并创建了邮箱。正在创建批处理文件,但尚未创建。我发现它正在尝试在我的本地计算机而不是服务器中查找文件。有什么方法可以强制它在服务器上 运行 吗?我的代码如下
package com.exchange;
import java.io.*;
public class CreateMailbox { public static void main(String[] args) throws IOException, InterruptedException { try {
String COMMAND1="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
-command \".'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1';Connect-ExchangeServer
-auto;Enable-Mailbox admin123@imbl.corp -Database \"TESTDB\"\"> C:\Users\admin\Desktop\ActiveSyncDeviceAccessRule_output.txt 2>C:\Users\admin\Desktop\standardError.txt";
System.out.println(COMMAND1);
String ErrorCommand1 = "echo %errorlevel% >C:\exitCode.txt";
String SPACE = " ";
final File file = new File("\\192.168.205.245\C$\Users\admin\Desktop\Create.bat");
Boolean CreatenewFileVar = file.createNewFile();
if(CreatenewFileVar)
{
System.out.println("File Created in: " + file);
}
PrintWriter writer = new PrintWriter(file, "UTF-8");
writer.println(COMMAND1);
writer.println(ErrorCommand1);
writer.println(SPACE);
writer.close();
Process p1 = Runtime.getRuntime().exec("cmd /c Start \\192.168.205.245\c$\Users\admin\Desktop\Create.bat");
int exitVal = p1.waitFor();
System.out.println("Exited with error code "+exitVal);
} catch (final IOException e) {
System.out.println("Error " + e); } } }
如有任何建议/帮助,我们将不胜感激。我不是高级程序员,并且是在互联网的帮助下完成此编码的。
我会使用另一种解决方案,即 Remote Powershell(更多信息 here, and a performance tip here)。因此,您可以调整代码以执行以下操作:
$CASServer = 'fqdn'
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://$CASServer/powershell" -Authentication Kerberos
Import-PSSession $Session
$password = ConvertTo-SecureString -String "Pa55w0rd!"
New-Mailbox -UserPrincipalName chris@contoso.com -Alias chris -Database "Mailbox Database 1" -Name ChrisAshton -OrganizationalUnit Users -Password $password -FirstName Chris -LastName Ashton -DisplayName "Chris Ashton" -ResetPasswordOnNextLogon $true
这是远程与 MS Exchange 交互的正确方法。没有人会尝试创建一个 CMD,然后触发它并在之后将其丢弃,因为它太复杂了(还要考虑 Exchange Server 端的安全性)。顺便说一句,在 Java 中实现上述内容,检查 Whosebug 发布 here and here.