/// <summary> /// This method changes the surname of a user in AD. /// </summary> /// <param name="employeeid"></param> /// <param name="samaccountname"></param> /// <param name="field"></param> /// <param name="value"></param> /// <returns></returns> public MSActorReturnMessageModel ChangeUserValueDriver(string employeeid, string samaccountname, string field, string value) { try { if (value == "") { value = null; } string dName; PSObject user = util.getADUser(employeeid, samaccountname); if (user == null) { throw new Exception("User was not found."); } dName = user.Properties["DistinguishedName"].Value.ToString(); using (PowerShell powershell = PowerShell.Create()) { PSCommand command = new PSCommand(); command.AddCommand("Set-ADUser"); command.AddParameter("Identity", dName); if (field.ToLower() == "ipphone") { if (value != null) { Hashtable attrHash = new Hashtable { { field, value } }; command.AddParameter("replace", attrHash); } else { String[] attrArray = new String[1]; attrArray[0] = field; command.AddParameter("clear", attrArray); } } else { command.AddParameter(field, value); } command.AddParameter("ErrorVariable", "Err"); 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)); } }
/// <summary> /// ... /// </summary> /// <param name="employeeid"></param> /// <param name="samaccountname"></param> /// <param name="ipphone"></param> /// <returns></returns> public MSActorReturnMessageModel SetIPPhone(string employeeid, string samaccountname, string ipphone) { UtilityController util = new UtilityController(); try { string dName; PSObject user = util.getADUser(employeeid, 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-ADUser"); if (ipphone != null) { Hashtable ipPhoneHash = new Hashtable { { "ipPhone", ipphone } }; command.AddParameter("replace", ipPhoneHash); } 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)); } }
/// <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)); } }
public MSActorReturnMessageModel AddUserFolderAccess(string employeeid, string samaccountname, string computername, string path, string accesstype) { try { PSObject user = util.getADUser(employeeid, samaccountname); if (user == null) { MSActorReturnMessageModel errorMessage = new MSActorReturnMessageModel(ErrorCode, "User was not found."); var customEx = new Exception("User was not found", new Exception()); Elmah.ErrorSignal.FromCurrentContext().Raise(customEx); return(errorMessage); } else { string identity = user.Properties["SamAccountName"].Value as string; return(AddFolderAccess(identity, computername, path, accesstype)); } }catch (Exception e) { return(util.ReportError(e)); } }
/// <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)); } }
/// <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)); } }