title: running Add-mailboxpermission from aspx page with impersonation
body: <p>I am trying to execute Add-Mailboxpermission from a ASPX page with impersonation of a specific user. an application error from MSExchange Common source is logged in the Event viewer of the web server.</p>
  
  <blockquote>
    <p>Watson report about to be sent to dw20.exe for process id: 2992, with parameters: E12, c-buddy-RTL-AMD64, 08.03.0083.006, w3wp, M.E.D.Directory, M.E.D.D.ConnectionPoolManager.BlockImpersonatedCallers, M.E.Common.FailFastException, c84f, 08.03.0213.000.  ErrorReportingEnabled: False</p>
  </blockquote>
  
  <p>The same code for executing a different Exchange CMDlets including the impersonation call is used and completes without any error.</p>
  
  <pre><code>    String ErrorText = "";
      RunspaceConfiguration config = RunspaceConfiguration.Create();
      PSSnapInException warning;
      sDecryptedPwd = SecurityManager.Decrypt(AdminPassword, true);
  
    using (new Impersonator(AdminUserName, "domain name", sDecryptedPwd))
      {
          // Load Exchange PowerShell snap-in.
          config.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out warning);
          if (warning != null)
              throw warning;
  
          using (Runspace thisRunspace = RunspaceFactory.CreateRunspace(config))
          {
              try
              {
                  thisRunspace.Open();
                  using (Pipeline thisPipeline = thisRunspace.CreatePipeline())
                  {
                      //Please change parameter values.
                      thisPipeline.Commands.Add("Add-MailboxPermission");
                      thisPipeline.Commands[0].Parameters.Add("Identity", sMailboxName);
                      thisPipeline.Commands[0].Parameters.Add("User", sUserName);
                      thisPipeline.Commands[0].Parameters.Add("AccessRights", sAccessRights);
                      thisPipeline.Commands[0].Parameters.Add("DomainController", sDomainController);
  
                      iLogManager.Info("Identity: " + sMailboxName + " User: " + sUserName + " AccessRights: " + sAccessRights + " DomainController: " + sDomainController);
  
                      try
                      {
                          thisPipeline.Invoke();
                          iLogManager.Info(thisPipeline.Commands[0].CommandText);
                      }
                      catch (Exception ex)
                      {
                          ErrorText = "Error: " + ex.ToString();
                      }
                      // Check for errors in the pipeline and throw an exception if necessary.
                      if (thisPipeline.Error != null &amp;&amp; thisPipeline.Error.Count &gt; 0)
                      {
                          StringBuilder pipelineError = new StringBuilder();
                          pipelineError.AppendFormat("Error calling Add-MailboxPermission.");
                          foreach (object item in thisPipeline.Error.ReadToEnd())
                          {
                              pipelineError.AppendFormat("{0}\n", item.ToString());
                          }
  
                          ErrorText = ErrorText + "Error: " + pipelineError.ToString() + Environment.NewLine;
                      }
                  }
              }
  
              finally
              {
                  thisRunspace.Close();
              }
          }
      }
      if (ErrorText == "")
          return "no error occurred.";
      else
          return ErrorText;
  </code></pre>
  
  <p>Does Exchange allows impersonation of Exchange CMDlets that modifies ACL's or am I doing something wrong here..</p>
  
  <p>Can anybody help me here, there is not much information about Exchange Powershell CMdlets with impersonation from ASPX page.</p>