private void CreateNewComputer(string computerName, string computerOUPath) { try { // Set default Directory OU Path PrincipalContext setOU = new PrincipalContext(ContextType.Domain, null, computerOUPath); // Create new Computer object ComputerPrincipal newComputerObject = new ComputerPrincipal(setOU, computerName, "password", enabled: true); // Set Extra Computer Info newComputerObject.DisplayName = computerName; // Save settings using (newComputerObject) { newComputerObject.Save(); } // Close Connection newComputerObject.Dispose(); } catch (Exception ex) { Console.WriteLine("Some error to create computer object" + ex.Message); throw; } }
private void btn_createPC_Click(object sender, RoutedEventArgs e) { if (currentPC == null || currentBranch == null) { MessageBox.Show("Before createing new PC, you must select Current Branch and generate free PC name!", "Error", MessageBoxButton.OK, MessageBoxImage.Stop); return; } MessageBoxResult mbr = MessageBox.Show(String.Format("Create new PC with name {0} in OU {1}", currentPC, currentBranch), "Agree or disagree?", MessageBoxButton.YesNo, MessageBoxImage.Question); if (mbr == MessageBoxResult.No) return; if (mbr == MessageBoxResult.Yes) { try { pcx = new PrincipalContext(ContextType.Domain, "bin.bank", String.Format("OU={0},OU=BIN BRANCHES,DC=BIN,DC=BANK", currentBranch), username, password); ComputerPrincipal comp = new ComputerPrincipal(pcx, currentPC, "", true); if (ckbx_description.IsChecked == true) { string diskrip = Interaction.InputBox("Input a description if needed.", "Description for " + currentPC, "Иванов И. В."); comp.Description = (diskrip == "") ? null : diskrip; } comp.Save(); MessageBox.Show("Successfully!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { pcx.Dispose(); } } }
/// <summary> /// ADD Active Directory Computer Obbject /// </summary> /// <param name="computerInfo">ADComputerRequest</param> /// <returns>ResponseMessage</returns> public ResponseMessage AddMachine( RequestComputerCreate computerInfo ) { ResponseMessage status = new ResponseMessage(); status.IsSuccessful = false; status.Message = string.Empty; Session stat = ValidateSession( computerInfo.DomainInfo.SessionKey ); if ( stat.IsAuthenticated == true ) { string uri = FixADURI( computerInfo.DomainInfo.ADHost , computerInfo.DomainInfo.ContainerPath ); if ( string.IsNullOrWhiteSpace( uri ) ) { status.Message = status.Message = "AD Host is not allowed to be empty, kindly provide the AD Host"; return status; } bool isAllowWite = CheckWriteOermission( uri , computerInfo.DomainInfo.BindingUserName , computerInfo.DomainInfo.BindingUserPassword ); try { ComputerPrincipal computer = FindADComputer( computerInfo.SamAccountName , computerInfo.DomainInfo ); if ( computer != null ) { status.Message = @"There is a existing computer with the provided name, kindly choose another name"; return status; } else { var principalContext = new PrincipalContext( ContextType.Domain , computerInfo.DomainInfo.DomainName , computerInfo.DomainInfo.ContainerPath , computerInfo.DomainInfo.BindingUserName , computerInfo.DomainInfo.BindingUserPassword ); computer = new ComputerPrincipal( principalContext ); computer.DisplayName = computerInfo.DisplayName; computer.Description = computerInfo.Description; computer.SamAccountName = computerInfo.SamAccountName; computer.Enabled = true; computer.SetPassword( GenerateADPassword( uri , computerInfo.DomainInfo.BindingUserName , computerInfo.DomainInfo.BindingUserPassword ) ); computer.Save(); status.Message = @"Computer has been added successfully "; status.IsSuccessful = true; return status; } } catch ( Exception ex ) { status.Message = status.Message = "error has accured while adding the desgnated group" + ex; return status; } } else { status.Message = "Kindly authenticate first"; return status; } }
/// <summary> /// Creates a computer with the specified name and description in the specified location. /// </summary> /// <param name="name">The name of the new computer.</param> /// <param name="location">The distinguished name of the OU in which the computer should be created.</param> /// <param name="description">The description of the new computer.</param> /// <returns>A ComputerPrincipal object representing the new computer.</returns> public static ComputerPrincipal CreateComputer(string name, string location, string description) { ComputerPrincipal newComputer = new ComputerPrincipal(GetPrincipalContext(location), name, GetRandomPassword(), true); newComputer.Description = description; newComputer.DisplayName = name; newComputer.Name = name; newComputer.Save(); return newComputer; }