-
Notifications
You must be signed in to change notification settings - Fork 808
Feature: Import AD computers #3368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Tyrix
wants to merge
8
commits into
BornToBeRoot:main
Choose a base branch
from
Tyrix:feature/ImportAdComputers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6cdec70
Feature (RDP): Active Directory computer import (OU subtree, flat hie…
Tyrix 07a3cc1
Feature (settings): remember Remote Desktop AD import LDAP search base
Tyrix 5f5aaad
Documentation: Edit contributors list
Tyrix d7d1f84
Merge branch 'main' into feature/ImportAdComputers
BornToBeRoot cdb1d57
Update Source/NETworkManager.Localization/Resources/Strings.resx
BornToBeRoot a6798b3
Feature: Move Import button to profile view
BornToBeRoot f68417b
Merge branch 'main' into pr/3368
BornToBeRoot 82c5964
Feature: Import dialog to chose an import method
BornToBeRoot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 113 additions & 5 deletions
118
Source/NETworkManager.Localization/Resources/Strings.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
Source/NETworkManager.Utilities/ActiveDirectory/ActiveDirectoryComputerRecord.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| namespace NETworkManager.Utilities.ActiveDirectory; | ||
|
|
||
| /// <summary> | ||
| /// Represents a computer account returned from Active Directory LDAP search. | ||
| /// </summary> | ||
| /// <param name="ProfileName">Display name for the profile (typically sAMAccountName without trailing '$').</param> | ||
| /// <param name="DnsHostName">DNS host name used for RDP when present.</param> | ||
| public readonly record struct ActiveDirectoryComputerRecord(string ProfileName, string DnsHostName); |
103 changes: 103 additions & 0 deletions
103
Source/NETworkManager.Utilities/ActiveDirectory/ActiveDirectoryComputerSearcher.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.DirectoryServices; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace NETworkManager.Utilities.ActiveDirectory; | ||
|
|
||
| /// <summary> | ||
| /// Queries Active Directory for computer accounts under a search base, including subtrees. | ||
| /// Uses the current Windows identity to bind to the directory. | ||
| /// </summary> | ||
| public static class ActiveDirectoryComputerSearcher | ||
| { | ||
| private const int LdapPageSize = 500; | ||
|
|
||
| /// <summary> | ||
| /// Returns computer accounts under <paramref name="ldapSearchRoot"/> with subtree scope. | ||
| /// </summary> | ||
| /// <param name="ldapSearchRoot">Distinguished name or LDAP path (with or without LDAP:// prefix).</param> | ||
| /// <param name="excludeDisabledComputerAccounts">When true, computer accounts with ACCOUNTDISABLE are omitted.</param> | ||
| /// <returns>Sorted list by profile name.</returns> | ||
| /// <exception cref="ArgumentException">When <paramref name="ldapSearchRoot"/> is null or whitespace.</exception> | ||
| /// <exception cref="InvalidOperationException">When the directory search fails.</exception> | ||
| public static IReadOnlyList<ActiveDirectoryComputerRecord> GetComputersInSubtree( | ||
| string ldapSearchRoot, | ||
| bool excludeDisabledComputerAccounts) | ||
| { | ||
| ArgumentException.ThrowIfNullOrWhiteSpace(ldapSearchRoot); | ||
|
|
||
| var ldapPath = NormalizeLdapPath(ldapSearchRoot.Trim()); | ||
|
|
||
| var ldapFilter = excludeDisabledComputerAccounts | ||
| ? "(&(&(objectCategory=computer)(objectClass=computer))(!(userAccountControl:1.2.840.113556.1.4.803:=2)))" | ||
| : "(&(objectCategory=computer)(objectClass=computer))"; | ||
|
|
||
| try | ||
| { | ||
| using var directoryEntry = new DirectoryEntry(ldapPath); | ||
| using var directorySearcher = new DirectorySearcher(directoryEntry) | ||
| { | ||
| SearchScope = SearchScope.Subtree, | ||
| Filter = ldapFilter, | ||
| PageSize = LdapPageSize, | ||
| Tombstone = false | ||
| }; | ||
|
|
||
| directorySearcher.PropertiesToLoad.Add("dnsHostName"); | ||
| directorySearcher.PropertiesToLoad.Add("name"); | ||
| directorySearcher.PropertiesToLoad.Add("sAMAccountName"); | ||
|
|
||
| var computers = new List<ActiveDirectoryComputerRecord>(); | ||
|
|
||
| using var searchResults = directorySearcher.FindAll(); | ||
| foreach (SearchResult searchResult in searchResults) | ||
| { | ||
| var dnsHostName = GetFirstPropertyString(searchResult, "dnsHostName"); | ||
| var nameAttribute = GetFirstPropertyString(searchResult, "name"); | ||
| var samAccountName = GetFirstPropertyString(searchResult, "sAMAccountName"); | ||
|
|
||
| var profileName = !string.IsNullOrEmpty(samAccountName) | ||
| ? samAccountName.TrimEnd('$') | ||
| : nameAttribute; | ||
|
|
||
| if (string.IsNullOrWhiteSpace(profileName)) | ||
| profileName = nameAttribute; | ||
|
|
||
| if (string.IsNullOrWhiteSpace(profileName)) | ||
| continue; | ||
|
|
||
| computers.Add(new ActiveDirectoryComputerRecord(profileName.Trim(), dnsHostName ?? string.Empty)); | ||
| } | ||
|
|
||
| computers.Sort((left, right) => | ||
| string.Compare(left.ProfileName, right.ProfileName, StringComparison.OrdinalIgnoreCase)); | ||
|
|
||
| return computers; | ||
| } | ||
| catch (COMException exception) | ||
| { | ||
| throw new InvalidOperationException( | ||
| "Active Directory search failed. Verify the search base, permissions, and domain connectivity.", | ||
| exception); | ||
| } | ||
| } | ||
|
|
||
| private static string NormalizeLdapPath(string input) | ||
| { | ||
| if (input.StartsWith("LDAP://", StringComparison.OrdinalIgnoreCase) || | ||
| input.StartsWith("LDAPS://", StringComparison.OrdinalIgnoreCase) || | ||
| input.StartsWith("GC://", StringComparison.OrdinalIgnoreCase)) | ||
| return input; | ||
|
|
||
| return "LDAP://" + input; | ||
| } | ||
|
|
||
| private static string GetFirstPropertyString(SearchResult searchResult, string propertyName) | ||
| { | ||
| if (!searchResult.Properties.Contains(propertyName) || searchResult.Properties[propertyName].Count == 0) | ||
| return string.Empty; | ||
|
|
||
| return searchResult.Properties[propertyName][0]?.ToString() ?? string.Empty; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.