Получение уникальных отделов из Active Directory с примерами кода

Чтобы получить уникальные отделы из Active Directory, вы можете использовать различные методы в зависимости от используемого языка программирования. Вот несколько примеров использования PowerShell и C#:

  1. PowerShell:
    
    # Connect to Active Directory
    Import-Module ActiveDirectory
    # Retrieve unique departments
    $departments = Get-ADUser -Filter * -Properties Department |
               Select-Object -ExpandProperty Department |
               Sort-Object -Unique

Вывод уникальных отделов

$departments


2. C# using System.DirectoryServices.AccountManagement:
```csharp
using System.DirectoryServices.AccountManagement;
// Connect to Active Directory
using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
{
    // Create a user principal object
    UserPrincipal userPrincipal = new UserPrincipal(context);
    // Create a principal searcher with the user principal
    using (PrincipalSearcher searcher = new PrincipalSearcher(userPrincipal))
    {
        // Retrieve the users matching the search criteria and their departments
        var users = searcher.FindAll().Cast<UserPrincipal>();
        var departments = users.Select(u => u.Department).Distinct();

        // Output the unique departments
        foreach (var department in departments)
        {
            Console.WriteLine(department);
        }
    }
}

Эти примеры демонстрируют, как подключиться к Active Directory и получить уникальные отделы пользователей. Скрипт PowerShell использует модуль ActiveDirectory, а код C# — пространство имен System.DirectoryServices.AccountManagement.