Share via


HOWTO: Give Service Account Access to all mailboxes on an Exchange 2007 Server

This is my first attempt to write a Powershell sample to my readers. In this sample we will give Send-As, Receive-As, ms-Exch-Store-Admin rights to an active directory user. We will be piping two commands here… Get-MailboxServer and the result will go to Add-AdPermission.

You may give same permissions using Exchange Power Shell Window

C:\>Get-MailboxServer –Identity MSGEX07 | Add-AdPermission –User UserA –AccessRights GenericRead, GenericWrite –ExtendedRights Send-As, Receive-As, ms-Exch-Store-Admin

Here is the sample…

 using System;
using System.Collections.Generic;
using System.Text;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;

namespace PowershellSample
{
    class Program
    {
        static void Main(string[] args)
        {
            RunspaceConfiguration config = RunspaceConfiguration.Create();
            PSSnapInException warning;

            // 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.
                        Command cmdGetMailbox = new Command("Get-MailboxServer");
                        Command cmdAddAdPermission = new Command("Add-AdPermission");

                        cmdGetMailbox.Parameters.Add("Identity", @"MSGEX07");
                        cmdAddAdPermission.Parameters.Add("User", "UserA");
                        cmdAddAdPermission.Parameters.Add("AccessRights", new string[] { "GenericRead", "GenericWrite" });
                        cmdAddAdPermission.Parameters.Add("ExtendedRights", new string[] { "Send-As", "Receive-As", "ms-Exch-Store-Admin" });

                        thisPipeline.Commands.Add(cmdGetMailbox);
                        thisPipeline.Commands.Add(cmdAddAdPermission);

                        try
                        {
                            thisPipeline.Invoke();
                        }
                        catch (Exception exx)
                        {
                            Console.Write("Error: " + exx.ToString());
                        }

                        // Check for errors in the pipeline and throw an exception if necessary.
                        if (thisPipeline.Error != null && thisPipeline.Error.Count > 0)
                        {
                            StringBuilder pipelineError = new StringBuilder();
                            pipelineError.AppendFormat("Error calling cmdLets...");
                            foreach (object item in thisPipeline.Error.ReadToEnd())
                            {
                                pipelineError.AppendFormat("{0}\n", item.ToString());
                            }

                            throw new Exception(pipelineError.ToString());
                        }
                    }
                }

                finally
                {
                    thisRunspace.Close();
                }

            }
        }
    }
}