Quantcast
Channel: Exchange Server 2010 forum
Viewing all articles
Browse latest Browse all 8820

Managing Exchange 2010 using C# .Net Web Service

$
0
0

Hi,
  I'm trying to create mailbox enable users in Exchange 2010 using C# .Net web serive, but it is not working. Same code works well as a console application. Web service is returning "No provisioning handler is installed" which is basically happening when it invokes "Enable-Mailbox" command. Please see the code attached below, if I change the command to "Get-Mailbox" then the same web service functions without any issue.

Infrastructure Details:

  • Windows 2008 R2 Server
  • IIS 6.1
  • Exchange 2010 Management tool
  • Visual studio 2008

The place which return error is loacted at the line has the following code "if (pipeLine.Error != null && pipeLine.Error.Count > 0)". Please help me with this, I would appreciate if anyone have done Exchange 2010 Mailbox provisioning with C# Web Service and help me out. Thanks in advance.

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Management.Automation;using System.Management.Automation.Runspaces;using System.Security;using Humana.ESE.PowerShell.POC.Common.DataTransferObjects;using System.Security.Principal;using System.Runtime.InteropServices;namespace Humana.ESE.PowerShell.POC.Core.BusinessObjects
{publicclass ExchangeMailboxTest
  {// These stuffs required for impersonationpublicconstint LOGON32_LOGON_INTERACTIVE = 2;publicconstint LOGON32_LOGON_SERVICE = 3;publicconstint LOGON32_PROVIDER_DEFAULT = 0;

    [DllImport("advapi32.dll", SetLastError = true)]publicstaticexternbool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);// Holds the new impersonation token.private IntPtr _userToken = IntPtr.Zero;private WindowsImpersonationContext _impersonationContext = null;public DTO ExecuteEnableMailboxCommand()
    {
      DTO response = new DTO();
      StringBuilder strBld = new StringBuilder();
      Pipeline pipeLine = null;
      Runspace myRunSpace = null;try
      {
        ICollection<PSObject> results;// create runspace// add command.
        RunspaceConfiguration rc = RunspaceConfiguration.Create();
        PSSnapInException snapEx = null;
        PSSnapInInfo info = rc.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapEx);// This is where be begin the Impersonation
        BeginImpersonation("ADDomain");

        myRunSpace = RunspaceFactory.CreateRunspace(rc);

        PSCommand psCommand = new PSCommand();

        myRunSpace.Open();

        pipeLine = myRunSpace.CreatePipeline();
        string checkstring = String.Empty;using (pipeLine)
        {
          Command cmd = new Command("set-adserversettings");
          cmd.Parameters.Add("viewentireforest", System.Boolean.Parse("true"));
          pipeLine.Commands.Add(cmd);try
          {
            pipeLine.Invoke();
          }catch (Exception e1)
          {
            Console.WriteLine("Error 1: " + e1.Message);
            response.Response = "Error 1:" + e1.Message;return response;
          }

          cmd = new Command("Enable-Mailbox");
          cmd.Parameters.Add("Identity", "UserDN");
          cmd.Parameters.Add("Alias", "TESTUser");
          cmd.Parameters.Add("Database", "DatabaseName");//If the above 4 line commented and Get-Mailbox command is used then it works// without issue and returns all the Mailbox with wildcard name "TEST"//cmd = new Command("Get-Mailbox");//cmd.Parameters.Add("Identity", "*TEST*");


          pipeLine = myRunSpace.CreatePipeline();
          pipeLine.Commands.Add(cmd);
          try
          {// get response
            results = pipeLine.Invoke();
          }catch(Exception e2)
          {
            response.Response = "Error 5: "+e2.Message;return response;
          }// print any errors// NOTE: These error are NOT thrown as exceptions!// Be sure to check this to ensure that no errors// happened while executing the command.if (pipeLine.Error != null&& pipeLine.Error.Count > 0)
          {
            strBld.Append("ERROR: There were pipeline errors...\n");foreach (object item in pipeLine.Error.ReadToEnd())
            {
              checkstring = checkstring + item.ToString() + "|";
              strBld.Append("Error: " + item.ToString() + "\n");
            }
          }foreach (PSObject obj in results)
          {
            strBld.Append(obj.ToString() + "\n");
          }

        }
      }
      catch (Exception e)
      {
        strBld.Append(e.Message);
      }finally
      {
        pipeLine.Dispose();
        pipeLine = null;
        myRunSpace.Close();
        myRunSpace = null;

      }
      response.Response = strBld.ToString();
      return response;


    }
    privatevoid BeginImpersonation(string domain)
    {//Please change the User Name and Passwordstring UserName = "ADUserID";string Password = "Password";

      EndImpersonation();

      Console.WriteLine("User Before Impersonation: " + WindowsIdentity.GetCurrent().Name);bool success = LogonUser(
          UserName,
          domain,
          Password,
          2,
          0,ref _userToken);// Did it work?if (!success) thrownew Exception(string.Format("LogonUser returned error {0}", System.Runtime.InteropServices.Marshal.GetLastWin32Error()));

      WindowsIdentity fakeId = new WindowsIdentity(_userToken);
      _impersonationContext = fakeId.Impersonate();//Console.WriteLine("User After Impersonation: " + WindowsIdentity.GetCurrent().Name );

    }

    privatevoid EndImpersonation()
    {if (_impersonationContext != null)
      {try
        {
          _impersonationContext.Undo();
        }finally
        {
          _impersonationContext.Dispose();
          _impersonationContext = null;
        }
      }
    }
  }
}

   

 


Viewing all articles
Browse latest Browse all 8820

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>