远程运行批处理文件以从 Java 在 Exchange 2010 上创建邮箱



我正在尝试从java在交换服务器上创建邮箱。我正在服务器上创建一个批处理文件并尝试执行它,因此它运行并创建邮箱。正在创建批处理文件,但未创建。我已经发现它正在尝试在我的本地机器而不是服务器中查找文件。有没有办法强制它在服务器上运行?我的代码如下

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

将不胜感激任何建议/帮助。我不是高级程序员,并且在互联网的帮助下完成了此编码。

我会使用另一种解决方案,即远程Powershell(更多信息在这里,性能提示在这里(。因此,您可以调整代码以执行以下操作:

$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中实现上述内容,请查看此处和此处的StackOverflow发布。

最新更新