Пример #1
0
        /// <summary>
        /// This method removes an existing AD group
        /// </summary>
        /// <param name="group_identity"></param>
        /// <returns></returns>
        public MSActorReturnMessageModel RemoveADGroup(string group_identity)
        {
            UtilityController util = new UtilityController();

            try
            {
                using (PowerShell powershell = PowerShell.Create())
                {
                    PSCommand command = new PSCommand();
                    command.AddCommand("Remove-ADGroup");
                    command.AddParameter("identity", group_identity);
                    command.AddParameter("confirm", false);
                    powershell.Commands = command;
                    powershell.Invoke();
                    if (powershell.Streams.Error.Count > 0)
                    {
                        throw powershell.Streams.Error[0].Exception;
                    }
                    powershell.Streams.ClearStreams();

                    MSActorReturnMessageModel successMessage = new MSActorReturnMessageModel(SuccessCode, "");
                    return(successMessage);
                }
            }
            catch (Exception e)
            {
                if (!e.Message.Contains(cantFindObjectError))
                {
                    return(util.ReportError(e));
                }
                return(util.ReportHiddenError(e));
            }
        }
Пример #2
0
        /// <summary>
        /// Delete entry for user
        /// </summary>
        /// <param name="employeeid"></param>
        /// <param name="samaccountname"></param>
        /// <returns></returns>
        public MSActorReturnMessageModel RemoveADObject(string employeeid, string samaccountname)
        {
            UtilityController         util           = new UtilityController();
            MSActorReturnMessageModel successMessage = new MSActorReturnMessageModel(SuccessCode, "");

            try
            {
                string   dName;
                PSObject user = util.getADUser(employeeid, samaccountname);
                if (user == null)
                {
                    return(successMessage);
                }
                Debug.WriteLine(user);
                dName = user.Properties["DistinguishedName"].Value.ToString();

                using (PowerShell powershell = PowerShell.Create())
                {
                    PSCommand command = new PSCommand();
                    command.AddCommand("Get-ADUser");
                    command.AddParameter("Identity", dName);
                    command.AddCommand("Get-ADObject");
                    command.AddCommand("Remove-ADObject");
                    command.AddParameter("confirm", false);
                    command.AddParameter("recursive");
                    powershell.Commands = command;
                    powershell.Invoke();
                    if (powershell.Streams.Error.Count > 0)
                    {
                        throw powershell.Streams.Error[0].Exception;
                    }
                    powershell.Streams.ClearStreams();

                    return(successMessage);
                }
            }
            catch (Exception e)
            {
                if (!e.Message.Contains(cantFindObjectError))
                {
                    return(util.ReportError(e));
                }

                return(util.ReportHiddenError(e));
            }
        }
Пример #3
0
        /// <summary>
        /// This is a driver method to be called from the MSActorController. it creates a new user in AD, and returns
        /// the status message of the request.
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public MSActorReturnMessageModel NewADUserDriver(ADUserModel user)
        {
            // Project P0975: Update retry delays from 1 second to 3 seconds, attempting to
            // reduce error reports from delays in creating user accounts
            try
            {
                using (PowerShell powershell = PowerShell.Create())
                {
                    //Password nonsense to follow
                    PSCommand command = new PSCommand();
                    command.AddCommand("ConvertTo-SecureString");
                    command.AddParameter("AsPlainText");
                    command.AddParameter("String", user.accountPassword);
                    command.AddParameter("Force");
                    powershell.Commands = command;
                    Collection <PSObject> passHashCollection = powershell.Invoke();
                    if (powershell.Streams.Error.Count > 0)
                    {
                        throw powershell.Streams.Error[0].Exception;
                    }
                    powershell.Streams.ClearStreams();
                    PSObject toPass = passHashCollection.First();   //this is the password wrapped in a psobject

                    command = new PSCommand();
                    command.AddCommand("new-aduser");
                    command.AddParameter("name", user.name); //Name used to be emplid, but has since been changed
                    command.AddParameter("accountpassword", toPass);
                    command.AddParameter("changepasswordatlogon", user.changepasswordatlogon);
                    command.AddParameter("city", user.city);
                    //command.AddParameter("country", user.country);
                    command.AddParameter("department", user.department);
                    command.AddParameter("description", user.description);
                    command.AddParameter("displayname", user.displayname);
                    command.AddParameter("employeeid", user.employeeid);
                    command.AddParameter("enabled", user.enabled);
                    command.AddParameter("givenname", user.givenname);
                    command.AddParameter("officephone", user.officephone);
                    command.AddParameter("initials", user.initials);
                    command.AddParameter("office", user.office);
                    command.AddParameter("postalcode", user.postalcode);
                    command.AddParameter("samaccountname", user.samaccountname);
                    command.AddParameter("state", user.state);
                    command.AddParameter("streetaddress", user.streetaddress);
                    command.AddParameter("surname", user.surname);
                    command.AddParameter("Title", user.title);
                    command.AddParameter("type", user.type);
                    command.AddParameter("userprincipalname", user.userprincipalname);
                    command.AddParameter("path", user.path);
                    if (user.ipphone != null)
                    {
                        Hashtable attrHash = new Hashtable
                        {
                            { "ipPhone", user.ipphone }
                        };
                        command.AddParameter("OtherAttributes", attrHash);
                    }
                    powershell.Commands = command;
                    powershell.Invoke();
                    if (powershell.Streams.Error.Count > 0)
                    {
                        throw powershell.Streams.Error[0].Exception;
                    }
                    powershell.Streams.ClearStreams();

                    bool   adFinished            = false;
                    int    count                 = 0;
                    String objectNotFoundMessage = "Cannot find an object with identity";
                    while (adFinished == false && count < 6)
                    {
                        try
                        {
                            command = new PSCommand();
                            command.AddCommand("get-aduser");
                            command.AddParameter("identity", user.samaccountname);
                            powershell.Commands = command;
                            Collection <PSObject> check = powershell.Invoke();
                            if (powershell.Streams.Error.Count > 0)
                            {
                                if (powershell.Streams.Error[0].Exception.Message.Contains(objectNotFoundMessage))
                                {
                                    System.Threading.Thread.Sleep(3000);
                                }
                                else
                                {
                                    throw powershell.Streams.Error[0].Exception;
                                }
                            }
                            powershell.Streams.ClearStreams();
                            if (check.FirstOrDefault() != null)
                            {
                                adFinished = true;
                            }
                            count++;
                        }
                        catch (Exception e)
                        {
                            if (e.Message.Contains(objectNotFoundMessage))
                            {
                                System.Threading.Thread.Sleep(3000);
                                count++;
                            }
                            else
                            {
                                throw e;
                            }
                        }
                    }

                    if (count == 6)
                    {
                        throw new Exception("Retry count exceeded. May indicate account creation issue");
                    }
                }

                MSActorReturnMessageModel successMessage = new MSActorReturnMessageModel(SuccessCode, "");
                return(successMessage);
            }
            catch (Exception e)
            {
                if (!e.Message.Contains(accountExistsError))
                {
                    return(util.ReportError(e));
                }
                return(util.ReportHiddenError(e));
            }
        }
Пример #4
0
        /// <summary>
        /// This method creates a new AD group
        /// </summary>
        /// <param name="group_name"></param>
        /// <param name="group_description"></param>
        /// <param name="group_info"></param>
        /// <param name="group_ad_path"></param>
        /// <param name="group_category"></param>
        /// <param name="group_scope"></param>
        /// <returns></returns>
        public MSActorReturnMessageModel NewADGroup(string group_name, string group_description, string group_info,
                                                    string group_ad_path, string group_category, string group_scope, string samaccountname)
        {
            UtilityController util = new UtilityController();

            try
            {
                using (PowerShell powershell = PowerShell.Create())
                {
                    PSCommand command;

                    if (group_category == "distribution")
                    {
                        // First we need Exchange to enable the distribution group
                        ExchangeController        control = new ExchangeController();
                        MSActorReturnMessageModel msg     = control.EnableDistributionGroup(group_name, group_ad_path, group_description, group_info);
                        if (msg.code == "CMP")
                        {
                            // Then we follow up setting some attributes that Exchange's cmdlet won't set
                            string distinguishedName = "CN=" + group_name + "," + group_ad_path;

                            bool   setADGroupComplete = false;
                            int    count = 0;
                            string objectNotFoundMessage = "Directory object not found";
                            while (setADGroupComplete == false && count < 3)
                            {
                                try
                                {
                                    command = new PSCommand();
                                    command.AddCommand("Set-ADGroup");
                                    command.AddParameter("identity", distinguishedName);
                                    if (group_description != "")
                                    {
                                        command.AddParameter("description", group_description);
                                    }
                                    command.AddParameter("displayname", group_name);
                                    if (group_info != "")
                                    {
                                        Hashtable attrHash = new Hashtable
                                        {
                                            { "info", group_info }
                                        };
                                        command.AddParameter("Add", attrHash);
                                    }
                                    powershell.Commands = command;
                                    powershell.Invoke();
                                    if (powershell.Streams.Error.Count > 0)
                                    {
                                        if (powershell.Streams.Error[0].Exception.Message.Contains(objectNotFoundMessage))
                                        {
                                            System.Threading.Thread.Sleep(1000);
                                        }
                                        else
                                        {
                                            throw powershell.Streams.Error[0].Exception;
                                        }
                                    }
                                    else
                                    {
                                        setADGroupComplete = true;
                                    }
                                    count++;
                                }
                                catch (Exception e)
                                {
                                    if (e.Message.Contains(objectNotFoundMessage))
                                    {
                                        System.Threading.Thread.Sleep(1000);
                                        count++;
                                    }
                                    else
                                    {
                                        throw e;
                                    }
                                }
                            }
                            if (count == 3)
                            {
                                throw new Exception("Retry count exceeded. May indicate distribution group creation issue");
                            }
                            else
                            {
                                return(new MSActorReturnMessageModel(SuccessCode, ""));
                            }
                        }
                        else
                        {
                            return(msg);
                        }
                    }

                    command = new PSCommand();
                    command.AddCommand("New-ADGroup");
                    command.AddParameter("name", group_name);
                    if (group_description != "")
                    {
                        command.AddParameter("description", group_description);
                    }
                    command.AddParameter("groupcategory", group_category);
                    command.AddParameter("displayname", group_name);
                    command.AddParameter("path", group_ad_path);
                    command.AddParameter("groupscope", group_scope);
                    if (group_info != "")
                    {
                        Hashtable attrHash = new Hashtable
                        {
                            { "info", group_info }
                        };
                        command.AddParameter("OtherAttributes", attrHash);
                    }
                    command.AddParameter("samaccountname", samaccountname);
                    powershell.Commands = command;
                    powershell.Invoke();
                    if (powershell.Streams.Error.Count > 0)
                    {
                        throw powershell.Streams.Error[0].Exception;
                    }
                    powershell.Streams.ClearStreams();


                    MSActorReturnMessageModel successMessage = new MSActorReturnMessageModel(SuccessCode, "");
                    return(successMessage);
                }
            }
            catch (Exception e)
            {
                if (!e.Message.Contains(groupExistsError))
                {
                    return(util.ReportError(e));
                }
                return(util.ReportHiddenError(e));
            }
        }
Пример #5
0
        public MSActorReturnMessageModel NewMoveRequest(string identity, string targetdatabase)
        {
            try
            {
                PSSessionOption option = new PSSessionOption();
                using (PowerShell powershell = PowerShell.Create())
                {
                    using (Runspace runspace = RunspaceFactory.CreateRunspace())
                    {
                        powershell.Runspace = runspace;
                        runspace.Open();

                        ConnectToExchange(powershell, runspace);

                        PSCommand command = new PSCommand();
                        command.AddCommand("Get-MoveRequest");
                        command.AddParameter("Identity", identity);
                        powershell.Commands = command;
                        Collection <PSObject> existingMoveRequests = powershell.Invoke();
                        if (powershell.Streams.Error.Count > 0)
                        {
                            RemoteException ex = powershell.Streams.Error[0].Exception as RemoteException;
                            // ManagementObjectNotFoundException is okay; it means there was not an existing move request
                            if (!ex.SerializedRemoteException.TypeNames.Contains("Microsoft.Exchange.Configuration.Tasks.ManagementObjectNotFoundException"))
                            {
                                throw powershell.Streams.Error[0].Exception;
                            }
                        }
                        powershell.Streams.ClearStreams();

                        // If there already is a move request we need to figure out what to do about it
                        if (existingMoveRequests.Count > 0)
                        {
                            string moveRequestStatus = existingMoveRequests[0].Properties["Status"].Value.ToString();
                            if (moveRequestStatus != "Completed")
                            {
                                // Is the same move request in flight or are we conflicting with another one?
                                if (existingMoveRequests[0].Properties["TargetDatabase"].Value.ToString() == targetdatabase)
                                {
                                    MSActorReturnMessageModel pndMessage = new MSActorReturnMessageModel(PendingCode, "");
                                    return(pndMessage);
                                }
                                else
                                {
                                    MSActorReturnMessageModel errMessage = new MSActorReturnMessageModel(ErrorCode, "Request still exists to move this mailbox to a different database");
                                    return(errMessage);
                                }
                            }
                            else
                            // Remove the completed move request and go on to make a new one
                            {
                                command = new PSCommand();
                                command.AddCommand("Remove-MoveRequest");
                                command.AddParameter("Identity", identity);
                                command.AddParameter("Confirm", false);
                                powershell.Commands = command;
                                powershell.Invoke();
                                if (powershell.Streams.Error.Count > 0)
                                {
                                    throw powershell.Streams.Error[0].Exception;
                                }
                                powershell.Streams.ClearStreams();
                            }
                        }

                        command = new PSCommand();
                        command.AddCommand("New-MoveRequest");
                        command.AddParameter("Identity", identity);
                        command.AddParameter("TargetDatabase", targetdatabase);
                        command.AddParameter("BadItemLimit", 1000);
                        command.AddParameter("AcceptLargeDataLoss");
                        powershell.Commands = command;
                        powershell.Invoke();
                        if (powershell.Streams.Error.Count > 0)
                        {
                            if (powershell.Streams.Error[0].Exception.Message.Contains("is already in the target database"))
                            {
                                return(util.ReportHiddenError(powershell.Streams.Error[0].Exception));
                            }
                            else
                            {
                                throw powershell.Streams.Error[0].Exception;
                            }
                        }
                        else
                        {
                            MSActorReturnMessageModel pendingMessage = new MSActorReturnMessageModel(PendingCode, "");
                            return(pendingMessage);
                        }
                        // powershell.Streams.ClearStreams();  -- is unreachable here
                    }
                }
            }
            catch (Exception e)
            {
                return(util.ReportError(e));
            }
        }