/// <summary> /// Initializes a new instance of the <see cref="GroupPolicyObject"/> class. /// </summary> /// <param name="domainName">The Name of the Domain where the GPO resides.</param> /// <param name="nameOfGpo">The name of the GPO.</param> public GroupPolicyObject(string domainName, string nameOfGpo) { if (string.IsNullOrEmpty(domainName)) { throw new ArgumentNullException(nameof(domainName)); } if (string.IsNullOrEmpty(nameOfGpo)) { throw new ArgumentNullException(nameof(nameOfGpo)); } DomainName = domainName; Name = nameOfGpo; string dn; try { var context = new DirectoryContext(DirectoryContextType.Domain, domainName); var domainObject = Domain.GetDomain(context).GetDirectoryEntry(); dn = (string)domainObject.Properties["distinguishedName"][0]; _log.Debug($"_DomainDN: {dn}"); } catch (Exception ex) { _log.Error($"Could not find domain: {domainName}", ex); throw; } // Let look up the GPO and put the information into the properties _log.Debug($"Looking up GPO: {nameOfGpo}"); try { var name = $"CN=Policies,CN=System,{dn}"; var path = DirectoryServices.DistinguishedName.Parse(name); var filter = new LdapFilter("displayName", "=", nameOfGpo); using (var query = new LdapQuery(path, filter, false)) { var result = query.FindOne(); DistinguishedName = result.Properties["distinguishedName"][0] as string; _log.Debug($"GPO DistinguishedName: {DistinguishedName}"); } } catch (Exception ex) { _log.Error($"Could not find GPO: {nameOfGpo}. Exception: {ex.Message}", ex); throw; } }
public void Path_Should_ReturnExpectedResult_When_ConstructorProvidedWithStringPath() { // Arrange var expected = "cn=Users,dc=company,dc=com"; // Act var query = new LdapQuery("cn=Users,dc=company,dc=com"); // Assert Assert.Equal(expected, query.Path); }
public void Filter_Should_ReturnExpectedResult_When_ConstructorProvidedWithStringFilter() { // Arrange var expected = "(sn=Jackson*)"; // Act var query = new LdapQuery("cn=Users,dc=company,dc=com", "(sn=Jackson*)"); // Assert Assert.Equal(expected, query.Filter); }
public void Filter_Should_ReturnExpectedResult_When_ConstructorProvidedWithNoFilter() { // Arrange var expected = "(objectClass=*)"; // Act var query = new LdapQuery("cn=Users,dc=company,dc=com"); // Assert Assert.Equal(expected, query.Filter); }
public void Path_Should_ReturnExpectedResult_When_ConstructorProvidedWithDistinguishedNameNoGlobalCatalog() { // Arrange var expected = "LDAP://company.com/cn=Users,dc=company,dc=com"; var dn = new DistinguishedName("cn=Users,dc=company,dc=com"); // Act var query = new LdapQuery(dn, false); // Assert Assert.Equal(expected, query.Path); }