0

I'm trying to create mailbox on exchange server from java. I am creating a batch file on the server and trying to execute it, so it runs and creates the mailbox. The batch file is being created but is not being created. I've figured out that it is trying to look for the file in my local machine instead of the server. Is there a way I can force it to run on the server? My code is as below

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

Would appreciate any suggestion / help. I am not an advanced programmer and have done this coding with the help of internet.

SR1092
  • 555
  • 1
  • 7
  • 31

1 Answers1

0

I would use another solution which is Remote Powershell (more info's here, and a performance tip here). So you could adjust your code to perform the following:

$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

This is the correct way to interact with MS Exchange from remote. Nobody would try to create a CMD, then trigger that and trow it away after that, as it would be to complicated (consider also the security on the Exchange Server side). By the way to implement the above in Java check the StackOverflow posting here and here.

BastianW
  • 2,628
  • 7
  • 29
  • 38