Example #1
0
        /// <summary>
        /// Disables this computer account in Active Directory.
        /// </summary>
        public void Disable()
        {
            using (var entry = ToDirectoryEntry())
            {
                var uac = (int)entry.Properties["userAccountControl"].Value;
                entry.Properties["userAccountControl"].Value = uac | (int)ADS_USER_FLAG.ACCOUNTDISABLE;
                entry.CommitChanges();

                // Now this object is "out-of-date", so reload.
                Initialize(DN.Parse(entry.Path));
            }
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Computer"/> class.
        /// </summary>
        /// <param name="domain">The domain of the computer.</param>
        /// <param name="computerName">SAM Account Name of the computer.</param>
        public Computer(string domain, string computerName)
        {
            var context = new DirectoryContext(DirectoryContextType.Domain, domain);

            var domainObject = Domain.GetDomain(context);

            using (var search = new DirectorySearcher())
            {
                search.SearchRoot = domainObject.GetDirectoryEntry();
                search.Filter     = "(samAccountName=" + computerName + "$)";

                var result = search.FindOne();

                if (result == null)
                {
                    throw new ArgumentException("Computer does not exist in this domain!");
                }

                Initialize(DN.Parse(result.Path));
            }
        }