Esempio n. 1
0
 public MSActorController()
 {
     util = new UtilityController();
 }
Esempio n. 2
0
 public FileServerController()
 {
     util = new UtilityController();
 }
Esempio n. 3
0
        /// <summary>
        /// ...
        /// </summary>
        /// <param name="employeeid"></param>
        /// <param name="searchbase"></param>
        /// <param name="old_samaccountname"></param>
        /// <param name="new_samaccountname"></param>
        /// <param name="userprincipalname"></param>
        /// <returns></returns>
        public MSActorReturnMessageModel ChangeUsername(string employeeid, string old_samaccountname, string new_samaccountname, string userprincipalname)
        {
            UtilityController util = new UtilityController();

            try
            {
                // debugging:
                // $user = Get-ADUser -Filter "employeeid -eq '9999998'" -SearchBase 'OU=Accounts,DC=spudev,DC=corp' -Properties cn,displayname,givenname,initials
                // $userDN =$($user.DistinguishedName)
                // Set - ADUser - identity $userDN - sAMAccountName ‘wclinton’ -UserPrincipalName ‘wclinton @spudev.corp’  -ErrorVariable Err

                string   dName;
                PSObject user = util.getADUser(employeeid, old_samaccountname);
                if (user == null)
                {
                    throw new Exception("User was not found.");
                }
                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("Set-Variable");
                    command.AddParameter("Name", "user");
                    powershell.Commands = command;
                    powershell.Invoke();
                    if (powershell.Streams.Error.Count > 0)
                    {
                        throw powershell.Streams.Error[0].Exception;
                    }
                    powershell.Streams.ClearStreams();

                    command = new PSCommand();
                    command.AddScript("$($user.DistinguishedName)");
                    command.AddCommand("Set-Variable");
                    command.AddParameter("Name", "userDN");
                    powershell.Commands = command;
                    powershell.Invoke();
                    if (powershell.Streams.Error.Count > 0)
                    {
                        throw powershell.Streams.Error[0].Exception;
                    }
                    powershell.Streams.ClearStreams();

                    command = new PSCommand();
                    command.AddScript(String.Format("Set-ADUser -Identity $userDN -sAMAccountName {0} -UserPrincipalName {1} -ErrorVariable Err", new_samaccountname, userprincipalname));
                    powershell.Commands = command;
                    powershell.Invoke();
                    if (powershell.Streams.Error.Count > 0)
                    {
                        throw powershell.Streams.Error[0].Exception;
                    }
                    powershell.Streams.ClearStreams();

                    command = new PSCommand();
                    command.AddScript(String.Format("Rename-ADObject -Identity $userDN -NewName {0}", new_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)
            {
                return(util.ReportError(e));
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Set password
        /// </summary>
        /// <param name="employeeid"></param>
        /// <param name="samaccountname"></param>
        /// <param name="accountpassword"></param>
        /// <param name="changepasswordatlogon"></param>
        /// <returns></returns>
        public MSActorReturnMessageModel SetPassword(string employeeid, string samaccountname, string accountpassword, string changepasswordatlogon)
        {
            MSActorReturnMessageModel errorMessage;
            UtilityController         util = new UtilityController();

            try
            {
                PSSessionOption option = new PSSessionOption();
                using (PowerShell powershell = PowerShell.Create())
                {
                    // Try without the runspace stuff first
                    //Runspace runspace = RunspaceFactory.CreateRunspace();
                    //powershell.Runspace = runspace;
                    //runspace.Open();

                    PSObject user = util.getADUser(employeeid, samaccountname);
                    if (user == null)
                    {
                        throw new Exception("User was not found.");
                    }

                    PSCommand command = new PSCommand();
                    command.AddCommand("ConvertTo-SecureString");
                    command.AddParameter("String", accountpassword);
                    command.AddParameter("AsPlainText");
                    command.AddParameter("Force");
                    powershell.Commands = command;
                    Collection <PSObject> pwd = powershell.Invoke();
                    if (powershell.Streams.Error.Count > 0)
                    {
                        throw powershell.Streams.Error[0].Exception;
                    }
                    powershell.Streams.ClearStreams();

                    if (pwd.Count != 1)
                    {
                        // This may not be reached anymore
                        throw new Exception("Unexpected return from creating password secure string.");
                    }

                    command = new PSCommand();
                    command.AddCommand("Set-ADAccountPassword");
                    command.AddParameter("Identity", user);
                    command.AddParameter("NewPassword", pwd[0]);
                    command.AddParameter("Reset");
                    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("Set-AdUser");
                    command.AddParameter("Identity", user);
                    command.AddParameter("ChangePasswordAtLogon", Boolean.Parse(changepasswordatlogon));
                    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)
            {
                return(util.ReportError(e));
            }
        }
Esempio n. 5
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));
            }
        }
Esempio n. 6
0
 public ADController()
 {
     util = new UtilityController();
 }
Esempio n. 7
0
 public ExchangeController()
 {
     util = new UtilityController();
 }