Exemplo n.º 1
0
        public void CreateOrganization(string organizationId, string programId, string offerId, string domain,
                                       string adminName, string adminEmail, string adminPassword)
        {
            ExchangeLog.LogStart("CreateOrganization");
            ExchangeLog.DebugInfo("organizationId: {0}", organizationId);
            ExchangeLog.DebugInfo("programId: {0}", programId);
            ExchangeLog.DebugInfo("offerId: {0}", offerId);
            ExchangeLog.DebugInfo("domain: {0}", domain);
            ExchangeLog.DebugInfo("adminEmail: {0}", adminEmail);

            bool     organizationCreated = false;
            Runspace runSpace            = null;

            try
            {
                runSpace = OpenRunspace();

                #region Create new organization
                ExchangeLog.LogStart("New-Organization");

                Command cmd = new Command("New-Organization");
                cmd.Parameters.Add("Name", organizationId);
                cmd.Parameters.Add("ProgramId", programId);
                cmd.Parameters.Add("OfferId", offerId);
                cmd.Parameters.Add("DomainName", domain);
                cmd.Parameters.Add("Location", OrganizationDefaultLocation);

                // run command and get DN of created organization
                Collection <PSObject> result = ExecuteShellCommand(runSpace, cmd);
                string organizationDN        = GetResultObjectDN(result);
                ExchangeLog.LogInfo("Organization DN: {0}", organizationDN);
                ExchangeLog.LogEnd("New-Organization");
                organizationCreated = true;
                #endregion

                #region Create administrator mailbox
                ExchangeLog.LogStart("New-Mailbox");

                SecureString secureAdminPassword = SecurityUtils.ConvertToSecureString(adminPassword);
                cmd = new Command("New-Mailbox");
                cmd.Parameters.Add("Organization", organizationId);
                cmd.Parameters.Add("Name", adminName);
                cmd.Parameters.Add("UserPrincipalName", adminEmail);
                cmd.Parameters.Add("Password", secureAdminPassword);

                // run command and get DN of created mailbox
                result = ExecuteShellCommand(runSpace, cmd);
                string adminDN = GetResultObjectDN(result);
                ExchangeLog.LogInfo("Administrator account DN: {0}", adminDN);
                ExchangeLog.LogEnd("New-Mailbox");
                #endregion

                #region Add admin account to "Organization Management" group
                ExchangeLog.LogStart("Add-RoleGroupMember");

                cmd = new Command("Add-RoleGroupMember");
                cmd.Parameters.Add("Identity", GetOrganizationManagementGroupDN(organizationDN));
                cmd.Parameters.Add("Member", adminDN);
                cmd.Parameters.Add("BypassSecurityGroupManagerCheck", true);

                // run command
                ExecuteShellCommand(runSpace, cmd);
                ExchangeLog.LogEnd("Add-RoleGroupMember");
                #endregion
            }
            catch (Exception ex)
            {
                // delete organization if it was created
                if (organizationCreated)
                {
                    DeleteOrganizationInternal(runSpace, organizationId);
                }

                throw ex;
            }
            finally
            {
                CloseRunspace(runSpace);
            }
            ExchangeLog.LogEnd("CreateOrganization");
        }