none
Exchange 2010 Remote Powershell - Enable-Mailbox and Set-Mailbox Microsoft.Exchange.Data references RRS feed

  • Question

  • Both Enable-Mailbox and Set-Mailbox cmdlets, and likely many others, specify paramenters which reference assemblies in the Microsoft.Exchange.Data namespace. For example, Set-Mailbox has an optional paramenter EmailAddresses which is an Microsoft.Exchange.Data.ProxyAddressCollection type, and Enable-Mailbox has a required paramenter ExternalEmailAddress which is a Microsoft.Exchange.Data.ProxyAddress type.

    How can I reference the required asseblies to be used in C# code, for example like this:

          Microsoft.Exchange.Data.ProxyAddress externalemailaddr = new Microsoft.Exchange.Data.SmtpProxyAddress(emailmail, true);
    
          using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfoExchange))
          {
            runspace.Open();
    
            using (PowerShell powershell = PowerShell.Create())
            {
              powershell.AddCommand("Enable-Mailbox");
              powershell.AddParameter("Identity", emailmail);
              powershell.AddParameter("ExternalEmailAddress", externalemailaddr);
              powershell.AddParameter("Confirm", false);
              powershell.Runspace = runspace;
    
              try
              {
                results = powershell.Invoke();
    


    Chris
    Friday, August 26, 2011 7:47 PM

Answers

  • The OrganizationUnit parameter need to be set like this:
    powershell.AddParameter("OrganizationalUnit", "OU=myOrg",DC=myDomain,DC=com");
    Below is a working example of the New-MailBox cmdlet.
     
                using (Runspace runspace = RunspaceFactory.CreateRunspace(GetExchangeConnectionInfo()))
                {
                    runspace.Open();
    
                    using (PowerShell powershell = PowerShell.Create())
                    {
                        powershell.AddCommand("New-MailBox");
                        powershell.AddParameter("UserPrincipalName", emailaddress);
                        powershell.AddParameter("Name", accountname);
                        powershell.AddParameter("Password", ToSecureString(password));
                        powershell.AddParameter("DisplayName", displayname);
                        powershell.AddParameter("FirstName", firstname);
                        powershell.AddParameter("LastName", lastname);
                        powershell.AddParameter("OrganizationalUnit", "OU=" + organizationunit + ",DC=soniceaglehq,DC=com");
                        powershell.AddParameter("Database", "MigrationProject");
                        powershell.AddParameter("ResetPasswordOnNextLogon", false);
    
                        powershell.Runspace = runspace;
    
                        try
                        {
                            results = powershell.Invoke();
                        }
                        catch (Exception ex)
                        {
                            ErrorText = "Error: " + ex.ToString();
                        }
    
                        if (results.Count == 0)
                            ErrorText = "Error: CreateMailbox failed for unknown reason.";
                    }
    

    Chris
    • Marked as answer by mangia Friday, September 30, 2011 6:24 PM
    Friday, September 30, 2011 6:24 PM

All replies

  • The Exchange Team has published an article on accessing remote Powershell through C#

    http://blogs.technet.com/b/exchange/archive/2009/11/02/3408653.aspx

     


    regards Thomas Paetzold visit my blog on: http://sus42.wordpress.com

    • Edited by Peddy1st Thursday, September 8, 2011 8:22 PM
    Friday, August 26, 2011 9:24 PM
  • Thanks, I have seen and read through that post. However, using the Exchange 2010 Remote Powershell (as in the post refenced) does not have the same access to data types as used in the console PowerShell.

    For example: OrganizationalUnit is a Microsoft.Exchange.Configuration.Tasks.OrganizationalUnitIdParameter type, not a string like in the console powershell .

    ExchangeEmailAxddress is a Microsoft.Exchange.Data.ProxyAddress type.

    There are many Microsoft.Exchange.Data.... type not support (apparently) via C#.


    Chris
    Friday, August 26, 2011 11:21 PM
  • Yes, this is right, but is your programm working or do you get errormessages? If you get errormessages it would be nice if you post them here.

     


    regards Thomas Paetzold visit my blog on: http://sus42.wordpress.com
    Thursday, September 1, 2011 4:15 PM
  • My initial post about Exchange 2010 Remote Powershell - Enable-Mailbox and Set-Mailbox is working but not completely as I would like or expect.  

    Like I said, the are optional paraments, like OrganizationalUnit that I would like to use, but I I cannot find (in this specific example) how to create a reference to make a 'Microsoft.Exchange.Configuration.Tasks.OrganizationalUnitIdParameter' data type, not a string like would like and not like what's used in the console powershell. 

    So to ansewer your question directly; For a 'New-MailBox' cmdlet, if I write code like:

          using (Runspace runspace = RunspaceFactory.CreateRunspace(exchangeConnectionInfo))
          {
            runspace.Open();
    
            using (PowerShell powershell = PowerShell.Create())
            {
              powershell.AddCommand("New-MailBox");
              powershell.AddParameter("UserPrincipalName", emailaddress); // unique in Exchange Account to login & read email
              powershell.AddParameter("Name", displayname);        // unique in AD User account name
              powershell.AddParameter("Password", ToSecureString(password));
              powershell.AddParameter("DisplayName", displayname);
              powershell.AddParameter("FirstName", firstname);
              powershell.AddParameter("LastName", lastname);
              <strong>powershell.AddParameter("OrganizationUnit", companyname);</strong>
              powershell.AddParameter("Database", "MigrationProject");
              powershell.AddParameter("EmailAddressPolicyEnabled", true);
              powershell.AddParameter("ResetPasswordOnNextLogon", false);
              if (alias.Length > 0)
                powershell.AddParameter("Alias", alias);
    
              powershell.Runspace = runspace;
    
              try
              {
                results = powershell.Invoke();
              }
              catch (Exception ex)
              {
                ErrorText = "Error: " + ex.ToString();
              }
    
              if (results.Count == 0)
                ErrorText = "Error: CreateMailbox failed for unknown reason.";
            }
          }
    
    
    The the error is:

    System.Management.Automation.RemoteException: A parameter cannot be found that matches parameter name 'OrganizationUnit'.
       at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings)
       at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
       at System.Management.Automation.PowerShell.Invoke()
       at PowerShellTest.Program.Create_Mailbox(String firstname, String lastname, String emailaddress, String domainemail, String companyname, String password, String alias) in C:\SVN\Development\PowerShellTest\PowerShellTest\Program.cs:line 205}


    Chris
    Thursday, September 1, 2011 5:38 PM
  • The OrganizationUnit parameter need to be set like this:
    powershell.AddParameter("OrganizationalUnit", "OU=myOrg",DC=myDomain,DC=com");
    Below is a working example of the New-MailBox cmdlet.
     
                using (Runspace runspace = RunspaceFactory.CreateRunspace(GetExchangeConnectionInfo()))
                {
                    runspace.Open();
    
                    using (PowerShell powershell = PowerShell.Create())
                    {
                        powershell.AddCommand("New-MailBox");
                        powershell.AddParameter("UserPrincipalName", emailaddress);
                        powershell.AddParameter("Name", accountname);
                        powershell.AddParameter("Password", ToSecureString(password));
                        powershell.AddParameter("DisplayName", displayname);
                        powershell.AddParameter("FirstName", firstname);
                        powershell.AddParameter("LastName", lastname);
                        powershell.AddParameter("OrganizationalUnit", "OU=" + organizationunit + ",DC=soniceaglehq,DC=com");
                        powershell.AddParameter("Database", "MigrationProject");
                        powershell.AddParameter("ResetPasswordOnNextLogon", false);
    
                        powershell.Runspace = runspace;
    
                        try
                        {
                            results = powershell.Invoke();
                        }
                        catch (Exception ex)
                        {
                            ErrorText = "Error: " + ex.ToString();
                        }
    
                        if (results.Count == 0)
                            ErrorText = "Error: CreateMailbox failed for unknown reason.";
                    }
    

    Chris
    • Marked as answer by mangia Friday, September 30, 2011 6:24 PM
    Friday, September 30, 2011 6:24 PM