Skip to content

Managing Business Units

This article describes how to manage Business Units using PowerShell scripts and C# code. The code samples included in this article can be used in stand-alone scripts and applications as well as in PowerShell scripts executed by Business Rules, Custom Commands, and Scheduled Tasks.

Creating a Business Unit

To create a Business Unit, first you need to bind to the container where you want to create it. All container objects support the IADsContainer interface, using which you can create, delete, and enumerate child objects. To create a new Business Unit, you need to call the Create method of the interface and pass “adm-BusinessUnit” as the first parameter of the method and the relative distinguished name (RDN) of the new Business Unit as the second parameter. The object returned by the IADsContainer::Create method will support the IAdmBusinessUnit interface, using which you can define membership rules for the Business Unit. To save a new Business Unit, call IADs::SetInfo.

The following code sample creates a Business Unit.

[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

# Connect to the Adaxes service
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")

# Bind to the 'Business Units' container
$businessUnitsPath = $admService.Backend.GetConfigurationContainerPath(
    "BusinessUnits")
$businessUnitsContainer = $admService.OpenObject($businessUnitsPath,
    $NULL, $NULL, 0)

# Create a new Business Unit
$unit = $businessUnitsContainer.Create("adm-BusinessUnit", "CN=My Unit")
$unit.Description = "My description"

$rules = $unit.GetMembershipRules()
# [TODO] define membership rules
$unit.SetMembershipRules($rules)

# Save the Business Unit
$unit.SetInfo()
using System;
using Interop.Adsi;
using Interop.Adsi.BusinessUnits;
using Softerra.Adaxes.Adsi;
using Softerra.Adaxes.Interop.Adsi.PersistentObjects;
class Program
{
    static void Main(string[] args)
    {
        // Connect to the Adaxes service
        AdmNamespace adsNS = new AdmNamespace();
        IAdmService admService = adsNS.GetServiceDirectly("localhost");

        // Bind to the 'Business Units' container
        String businessUnitsPath = admService.Backend.GetConfigurationContainerPath(
            "BusinessUnits");
        IADsContainer businessUnitsContainer = (IADsContainer)admService.OpenObject(
            businessUnitsPath, null, null, 0);

        // Create a new Business Unit
        IAdmBusinessUnit unit = (IAdmBusinessUnit)businessUnitsContainer.Create(
            "adm-BusinessUnit", "CN=My Unit");
        unit.Description = "My description";

        IAdmBusinessUnitMembershipRules rules = unit.GetMembershipRules();
        // [TODO] define membership rules
        unit.SetMembershipRules(rules);

        // Save the Business Unit
        unit.SetInfo();
    }
}

If your script is executed by a Business Rule, Custom Command or Scheduled Task, you can use a predefined PowerShell variable $Context to get the ADS path of the Business Units container and bind to the container.

# Bind to the Business Units container
$businessUnitsPath = $Context.GetWellKnownContainerPath("BusinessUnits")
$businessUnitsContainer = $Context.BindToObject($businessUnitsPath)

# Create a new Business Unit
$unit = $businessUnitsContainer.Create("adm-BusinessUnit", "CN=My Unit")
$unit.Description = "My description"

$rules = $unit.GetMembershipRules()
# [TODO] define membership rules
$unit.SetMembershipRules($rules)

# Save the Business Unit
$unit.SetInfo()
How to create a Business Unit in a specific container

The following code sample creates a Business Unit in the container called My Container.

[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

# Connect to the Adaxes service
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")

# Bind to the Business Units container
$businessUnitsPath = $admService.Backend.GetConfigurationContainerPath(
    "BusinessUnits")
# Build the ADS path of the child container 'My Container'
$businessUnitsPathObj = New-Object "Softerra.Adaxes.Adsi.AdsPath"`
    $businessUnitsPath
$myContainerAdsPath = $businessUnitsPathObj.CreateChildPath("CN=My Container")

$myContainer = $admService.OpenObject($myContainerAdsPath, $NULL, $NULL, 0)

# Create a new Business Unit
$unit = $myContainer.Create("adm-BusinessUnit", "CN=My Unit")
$unit.Description = "My description"

$rules = $unit.GetMembershipRules()
# [TODO] define membership rules
$unit.SetMembershipRules($rules)

# Save the Business Unit
$unit.SetInfo()
using System;
using Interop.Adsi;
using Interop.Adsi.BusinessUnits;
using Softerra.Adaxes.Adsi;
using Softerra.Adaxes.Interop.Adsi.PersistentObjects;
class Program
{
    static void Main(string[] args)
    {
        // Connect to the Adaxes service
        AdmNamespace adsNS = new AdmNamespace();
        IAdmService admService = adsNS.GetServiceDirectly("localhost");

        // Bind to the Business Units container
        String businessUnitsPath = admService.Backend.GetConfigurationContainerPath(
            "BusinessUnits");
        // Build the ADS path of the child container 'My Container'
        AdsPath businessUnitsPathObj = new AdsPath(businessUnitsPath);
        AdsPath myContainerAdsPath = businessUnitsPathObj.CreateChildPath(
            "CN=My Container");

        IADsContainer myContainer = (IADsContainer)admService.OpenObject(
            myContainerAdsPath.ToString(), null, null, 0);

        // Create a new Business Unit
        IAdmBusinessUnit unit = (IAdmBusinessUnit)myContainer.Create(
            "adm-BusinessUnit", "CN=My Unit");
        unit.Description = "My description";

        IAdmBusinessUnitMembershipRules rules = unit.GetMembershipRules();
        // [TODO] define membership rules
        unit.SetMembershipRules(rules);

        // Save the Business Unit
        unit.SetInfo();
    }
}

Note

For information on how to create containers for Business Units, see Creating Business Unit containers.

Modifying a Business Unit

To modify an existing Business Unit, first you need to bind to the directory object representing the Business Unit. For more information on how to bind to Adaxes-specific objects, see Binding to Adaxes-specific objects.

After you’ve bound to a Business Unit object, you can use ADSI interfaces like IAdmBusinessUnit and IADs to modify the Business Unit.

The following code sample binds to an existing Business Unit located in container My Container and deletes all membership rules of the Business Unit.

[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

# Connect to the Adaxes service
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")

# Build the ADS path of the Business Unit
$businessUnitsPath = $admService.Backend.GetConfigurationContainerPath(
    "BusinessUnits")
$businessUnitsPathObj = New-Object "Softerra.Adaxes.Adsi.AdsPath" `
    $businessUnitsPath
$myContainerPathObj = $businessUnitsPathObj.CreateChildPath(
    "CN=My Container")
$unitPath = $myContainerPathObj.CreateChildPath("CN=My Unit")

$unit = $admService.OpenObject($unitPath.ToString(),
    $NULL, $NULL, 0)

# Delete membership rules
$rules = $unit.GetMembershipRules()
$rules.Clear()
$unit.SetMembershipRules($rules)

# Save the changes
$unit.SetInfo()
using System;
using Interop.Adsi.BusinessUnits;
using Softerra.Adaxes.Adsi;
using Softerra.Adaxes.Interop.Adsi.PersistentObjects;
class Program
{
    static void Main(string[] args)
    {
        AdmNamespace adsNS = new AdmNamespace();
        IAdmService admService = adsNS.GetServiceDirectly("localhost");

        // Build the ADS path of the Business Unit
        String businessUnitsPath = admService.Backend.GetConfigurationContainerPath(
            "BusinessUnits");
        AdsPath businessUnitsPathObj = new AdsPath(businessUnitsPath);
        AdsPath myContainerPathObj = businessUnitsPathObj.CreateChildPath(
            "CN=My Container");
        AdsPath unitPath = myContainerPathObj.CreateChildPath("CN=My Unit");

        IAdmBusinessUnit unit =
            (IAdmBusinessUnit)admService.OpenObject(unitPath.ToString(),
                null, null, 0);

        // Delete membership rules
        IAdmBusinessUnitMembershipRules rules = unit.GetMembershipRules();
        rules.Clear();
        unit.SetMembershipRules(rules);

        // Save the changes
        unit.SetInfo();
    }
}

Defining membership rules

Membership of a Business Unit is determined by membership rules that allow including AD objects that correspond to certain search criteria, members of groups, child objects of Organizational Units, etc.

To get the membership rules of a Business Unit, call the GetMembershipRules method of the IAdmBusinessUnit interface. Membership rules are represented by the IAdmBusinessUnitMembershipRules interface, using which you can create, modify, and enumerate membership rules.

To create a new membership rule, call the Create method of the IAdmBusinessUnitMembershipRules interface and pass the membership rule type as the parameter of the method. For a list of possible membership rule types, see the ADM_BUSINESSUNITMEMBERSHIPTYPE_ENUM enumeration.

The object returned by the Create method will support the IAdmBusinessUnitMembershipRule interface and one of its derivatives specific to the membership rule type.

The table below shows which interfaces are supported by different membership rule types.

What is Included or Excluded Membership Rule Type Interface
Specific Objects ADM_BUSINESSUNITMEMBERSHIPTYPE_SPECIFIC IAdmBusinessUnitSpecificObjectRule, IAdmBusinessUnitSpecificObjectRule2
Group Members ADM_BUSINESSUNITMEMBERSHIPTYPE_GROUP IAdmBusinessUnitGroupRule, IAdmBusinessUnitGroupRule2
Container Children ADM_BUSINESSUNITMEMBERSHIPTYPE_CONTAINER IAdmBusinessUnitContainerRule, IAdmBusinessUnitContainerRule2
Query Results ADM_BUSINESSUNITMEMBERSHIPTYPE_QUERY IAdmBusinessUnitQueryRule, IAdmBusinessUnitQueryRule2

To add a membership rule to the collection of membership rules, call the Add method of the IAdmBusinessUnitMembershipRules interface.

To set a collection of membership rules to a Business Unit, call the SetMembershipRules method of the Business Unit object, and then call the SetInfo method to save the changes.

Example 1: Include user John Smith and exclude user Bob Jones

# The $unit variable refers to a Business Unit

$rules = $unit.GetMembershipRules()

# Include John Smith
$jsmithDN = "CN=John Smith,CN=Users,DC=company,DC=com"
$jsmith = $admService.OpenObject("Adaxes://$jsmithDN", $NULL, $NULL, 0)
$includeRule = $rules.Create("ADM_BUSINESSUNITMEMBERSHIPTYPE_SPECIFIC")
$includeRule.Exclude = $False
$includeRule.Object = $jsmith
$rules.Add($includeRule)

# Exclude Bob Jones
$bjonesDN = "CN=Bob Jones,CN=Users,DC=company,DC=com"
$bjones = $admService.OpenObject("Adaxes://$bjonesDN", $NULL, $NULL, 0)
$excludeRule = $rules.Create("ADM_BUSINESSUNITMEMBERSHIPTYPE_SPECIFIC")
$excludeRule.Exclude = $True
$excludeRule.Object = $bjones
$rules.Add($excludeRule)

$unit.SetMembershipRules($rules)

# Save the changes
$unit.SetInfo()
// The unit variable refers to a Business Unit

IAdmBusinessUnitMembershipRules rules = unit.GetMembershipRules();

// Include John Smith
const String jsmithDN = "CN=John Smith,CN=Users,DC=company,DC=com";
IAdmTop jsmith = (IAdmTop)admService.OpenObject(
"Adaxes://" + jsmithDN, null, null, 0);

IAdmBusinessUnitSpecificObjectRule includeRule =
(IAdmBusinessUnitSpecificObjectRule)rules.Create(
    ADM_BUSINESSUNITMEMBERSHIPTYPE_ENUM.ADM_BUSINESSUNITMEMBERSHIPTYPE_SPECIFIC);
includeRule.Exclude = false;
includeRule.Object = jsmith;
rules.Add(includeRule);

// Exclude Bob Jones
const String bjonesDN = "CN=Bob Jones,CN=Users,DC=company,DC=com";
IAdmTop bjones = (IAdmTop)admService.OpenObject(
"Adaxes://" + bjonesDN, null, null, 0);
IAdmBusinessUnitSpecificObjectRule excludeRule =
(IAdmBusinessUnitSpecificObjectRule)rules.Create(
    ADM_BUSINESSUNITMEMBERSHIPTYPE_ENUM.ADM_BUSINESSUNITMEMBERSHIPTYPE_SPECIFIC);
excludeRule.Exclude = true;
excludeRule.Object = bjones;
rules.Add(excludeRule);

unit.SetMembershipRules(rules);

// Save the changes
unit.SetInfo();

Example 2: Include members of the My Group group

# The $unit variable refers to a Business Unit

$rules = $unit.GetMembershipRules()

$groupDN = "CN=My Group,OU=Groups,DC=company,DC=com"
$group = $admService.OpenObject("Adaxes://$groupDN", $NULL, $NULL, 0)

$rule = $rules.Create("ADM_BUSINESSUNITMEMBERSHIPTYPE_GROUP")
$rule.Exclude = $False
$rule.Group = $group
$rule.IncludeDirectMembersOnly = $False
$rules.Add($rule)

$unit.SetMembershipRules($rules)

# Save the changes
$unit.SetInfo()
// The unit variable refers to a Business Unit

IAdmBusinessUnitMembershipRules rules = unit.GetMembershipRules();

const String groupDN = "CN=My Group,OU=Groups,DC=company,DC=com";
IAdmGroup group = (IAdmGroup)admService.OpenObject(
"Adaxes://" + groupDN, null, null, 0);

IAdmBusinessUnitGroupRule rule =
(IAdmBusinessUnitGroupRule)rules.Create(
    ADM_BUSINESSUNITMEMBERSHIPTYPE_ENUM.ADM_BUSINESSUNITMEMBERSHIPTYPE_GROUP);
rule.Exclude = false;
rule.Group = group;
rule.IncludeDirectMembersOnly = false;
rules.Add(rule);

unit.SetMembershipRules(rules);

// Save the changes
unit.SetInfo();

Example 3: Include child objects of the Organizational Unit called My Unit

# The $unit variable refers to a Business Unit

$rules = $unit.GetMembershipRules()

$ouDN = "OU=My Unit,DC=company,DC=com"
$ou = $admService.OpenObject("Adaxes://$ouDN", $NULL, $NULL, 0)

$rule = $rules.Create("ADM_BUSINESSUNITMEMBERSHIPTYPE_CONTAINER")
$rule.Exclude = $False
$rule.Container = $ou
$rule.Scope = "ADS_SCOPE_SUBTREE"
$rules.Add($rule)

$unit.SetMembershipRules($rules)

# Save the changes
$unit.SetInfo()
IAdmBusinessUnitMembershipRules rules = unit.GetMembershipRules();

const String ouDN = "OU=My Unit,DC=company,DC=com";
IADsContainer ou = (IADsContainer)admService.OpenObject(
"Adaxes://" + ouDN, null, null, 0);

IAdmBusinessUnitContainerRule rule =
(IAdmBusinessUnitContainerRule)rules.Create(
    ADM_BUSINESSUNITMEMBERSHIPTYPE_ENUM.ADM_BUSINESSUNITMEMBERSHIPTYPE_CONTAINER);
rule.Exclude = false;
rule.Container = ou;
rule.Scope = ADS_SCOPEENUM.ADS_SCOPE_SUBTREE;
rules.Add(rule);

unit.SetMembershipRules(rules);

// Save the changes
unit.SetInfo();

Example 4: Include all users whose department is set to Sales

# The $unit variable refers to a Business Unit

$rules = $unit.GetMembershipRules()

$rule = $rules.Create("ADM_BUSINESSUNITMEMBERSHIPTYPE_QUERY")
$rule.Exclude = $False
$rule.BaseObjectPath = $NULL # search in all managed AD domains
$rule.Scope = "ADS_SCOPE_SUBTREE"
$rule.Filter = "(&(department=Sales)(objectClass=user)(objectCategory=person))"
$rules.Add($rule)

$unit.SetMembershipRules($rules)

# Save the changes
$unit.SetInfo()
// The unit variable refers to a Business Unit

IAdmBusinessUnitMembershipRules rules = unit.GetMembershipRules();

IAdmBusinessUnitQueryRule rule =
(IAdmBusinessUnitQueryRule)rules.Create(
    ADM_BUSINESSUNITMEMBERSHIPTYPE_ENUM.ADM_BUSINESSUNITMEMBERSHIPTYPE_QUERY);
rule.Exclude = false;
rule.BaseObjectPath = null; // search in all managed AD domains
rule.Scope = ADS_SCOPEENUM.ADS_SCOPE_SUBTREE;
rule.Filter = "(&(department=Sales)(objectClass=user)(objectCategory=person))";
rules.Add(rule);

unit.SetMembershipRules(rules);

// Save the changes
unit.SetInfo();

Defining membership rules using templates

By using value references in membership rules, you can create dynamic Business Units that will have different members for different users. Before building a list of Business Unit members, Adaxes will replace value references contained in membership rules with corresponding properties of the logged in user account. As a result, membership rule parameters will be different depending on who is logged in.

Tip

Value references in Business Units are processed only when viewing Business Unit members and in Security Role assignments.

To specify templates in membership rule parameters, you need to use the following interfaces (notice 2 at the end):

Membership Rule Type Interface
Specific Objects IAdmBusinessUnitSpecificObjectRule2
Group Members IAdmBusinessUnitGroupRule2
Container Children IAdmBusinessUnitContainerRule2
Query Results IAdmBusinessUnitQueryRule2

Example 1: Include the user’s manager and exclude the user itself

# The $unit variable refers to a Business Unit

$rules = $unit.GetMembershipRules()

# Include the user's manager
$includeRule = $rules.Create("ADM_BUSINESSUNITMEMBERSHIPTYPE_SPECIFIC")
$includeRule.ObjectDnTemplate = "%manager%"
$includeRule.Exclude = $False
$rules.Add($includeRule)

# Exclude the user's account
$excludeRule = $rules.Create("ADM_BUSINESSUNITMEMBERSHIPTYPE_SPECIFIC")
$excludeRule.ObjectDnTemplate = "%distinguishedName%"
$excludeRule.Exclude = $True
$rules.Add($excludeRule)

$unit.SetMembershipRules($rules)

# Save the changes
$unit.SetInfo()
// The unit variable refers to a Business Unit

IAdmBusinessUnitMembershipRules rules = unit.GetMembershipRules();

// Include the user's manager
IAdmBusinessUnitSpecificObjectRule2 includeRule =
(IAdmBusinessUnitSpecificObjectRule2)rules.Create(
    ADM_BUSINESSUNITMEMBERSHIPTYPE_ENUM.ADM_BUSINESSUNITMEMBERSHIPTYPE_SPECIFIC);
includeRule.ObjectDnTemplate = "%manager%";
includeRule.Exclude = false;
rules.Add(includeRule);

// Exclude the user's account
IAdmBusinessUnitSpecificObjectRule2 excludeRule =
(IAdmBusinessUnitSpecificObjectRule2)rules.Create(
    ADM_BUSINESSUNITMEMBERSHIPTYPE_ENUM.ADM_BUSINESSUNITMEMBERSHIPTYPE_SPECIFIC);
excludeRule.ObjectDnTemplate = "%distinguishedName%";
excludeRule.Exclude = true;
rules.Add(excludeRule);

unit.SetMembershipRules(rules);

// Save the changes
unit.SetInfo();

Example 2: Include members of group Managers located in the Organizational Unit with the same name as the user’s company name

# The $unit variable refers to a Business Unit

$rules = $unit.GetMembershipRules()

$groupDnTemplate = "CN=Managers,OU=%company%,DC=domain,DC=com"

$rule = $rules.Create("ADM_BUSINESSUNITMEMBERSHIPTYPE_GROUP")
$rule.GroupDnTemplate = $groupDnTemplate
$rule.Exclude = $False
$rule.IncludeDirectMembersOnly = $False
$rules.Add($rule)

$unit.SetMembershipRules($rules)

# Save the changes
$unit.SetInfo()
// The unit variable refers to a Business Unit

IAdmBusinessUnitMembershipRules rules = unit.GetMembershipRules();

const String groupDnTemplate = "CN=Managers,OU=%company%,DC=domain,DC=com";

IAdmBusinessUnitGroupRule2 rule =
(IAdmBusinessUnitGroupRule2)rules.Create(
    ADM_BUSINESSUNITMEMBERSHIPTYPE_ENUM.ADM_BUSINESSUNITMEMBERSHIPTYPE_GROUP);
rule.GroupDnTemplate = groupDnTemplate;
rule.Exclude = false;
rule.IncludeDirectMembersOnly = false;
rules.Add(rule);

unit.SetMembershipRules(rules);

// Save the changes
unit.SetInfo();

Example 3: Include objects located in the user’s Organizational Unit

# The $unit variable refers to a Business Unit

$rules = $unit.GetMembershipRules()

$ouDnTemplate = "%adm-ParentDN%"

$rule = $rules.Create("ADM_BUSINESSUNITMEMBERSHIPTYPE_CONTAINER")
$rule.ContainerDnTemplate = $ouDnTemplate
$rule.Exclude = $False
$rule.Scope = "ADS_SCOPE_SUBTREE"
$rules.Add($rule)

$unit.SetMembershipRules($rules)

# Save the changes
$unit.SetInfo()
// The unit variable refers to a Business Unit

IAdmBusinessUnitMembershipRules rules = unit.GetMembershipRules();

const String ouDnTemplate = "%adm-ParentDN%";

IAdmBusinessUnitContainerRule2 rule =
(IAdmBusinessUnitContainerRule2)rules.Create(
    ADM_BUSINESSUNITMEMBERSHIPTYPE_ENUM.ADM_BUSINESSUNITMEMBERSHIPTYPE_CONTAINER);
rule.ContainerDnTemplate = ouDnTemplate;
rule.Exclude = false;
rule.Scope = ADS_SCOPEENUM.ADS_SCOPE_SUBTREE;
rules.Add(rule);

unit.SetMembershipRules(rules);

// Save the changes
unit.SetInfo();

Example 4: Include users whose department is the same as the department of the logged in user and located in the user’s domain

# The $unit variable refers to a Business Unit

$rules = $unit.GetMembershipRules()

$rule = $rules.Create("ADM_BUSINESSUNITMEMBERSHIPTYPE_QUERY")
$rule.BaseObjectDnTemplate = "%adm-DomainDN%"
$rule.Exclude = $False
$rule.Scope = "ADS_SCOPE_SUBTREE"
$rule.Filter = "(&(department=%department%)(objectClass=user)(objectCategory=person))"
$rules.Add($rule)

$unit.SetMembershipRules($rules)

# Save the changes
$unit.SetInfo()
// The unit variable refers to a Business Unit

IAdmBusinessUnitMembershipRules rules = unit.GetMembershipRules();

IAdmBusinessUnitQueryRule2 rule =
(IAdmBusinessUnitQueryRule2)rules.Create(
    ADM_BUSINESSUNITMEMBERSHIPTYPE_ENUM.ADM_BUSINESSUNITMEMBERSHIPTYPE_QUERY);
rule.BaseObjectDnTemplate = "%adm-DomainDN%";
rule.Exclude = false;
rule.Scope = ADS_SCOPEENUM.ADS_SCOPE_SUBTREE;
rule.Filter = "(&(department=%department%)(objectClass=user)(objectCategory=person))";
rules.Add(rule);

unit.SetMembershipRules(rules);

// Save the changes
unit.SetInfo();

Getting Business Unit members

To get the members of a Business Unit, you need to bind to the Business Unit object and call the IAdmBusinessUnit::Members method of the object. Business Unit members are represented by the IAdmBusinessUnitMembers interface.

To get a specific member, call the IAdmBusinessUnitMembers::GetObject method. The interfaces supported by the ADSI object representing a member will depend on the object type of the member. To get the type of a directory object, use the Class property of the IADs interface. All object types support the IADs and IAdmTop interfaces. User objects support the IADsUser interface, group objects support the IADsGroup interface. For more information, see Interfaces supported by directory objects.

The following code sample outputs information about users that are members of a Business Unit.

[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

$unitName = "My Unit"
$containerName = "My Container" # Set to $NULL if the Business Unit
                                # is located in the root container
# Connect to the Adaxes service
$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")

# Build the ADS path of the Business Unit
$businessUnitsPath = $admService.Backend.GetConfigurationContainerPath(
"BusinessUnits")
$pathObj = New-Object "Softerra.Adaxes.Adsi.AdsPath" `
$businessUnitsPath
if ($containerName -ne $NULL)
{
    $pathObj = $pathObj.CreateChildPath("CN=$containerName")
}
$pathObj = $pathObj.CreateChildPath("CN=$unitName")

# Bind to the Business Unit
$unit = $admService.OpenObject($pathObj.ToString(), $NULL, $NULL, 0)

$members = $unit.Members()
for ($i = 0; $i -lt $members.Count; $i++)
{
    $member = $members.GetObject($i)
    if (-not($member.Class -ieq "user"))
    {
        continue # we output user accounts only
    }

    $name = $member.Get("name")
    $username = $member.Get("sAMAccountName")
    $dn = $member.Get("distinguishedName")
    if ($member.AccountDisabled)
    {
        $status = "disabled"
    }
    else
    {
        $status =  "enabled"
    }

    $expirationDate = $member.AccountExpirationDate.ToShortDateString()

    $info = @"
Name: $name
Username: $username
DN: $dn
Account Status: $status
Expiration Date: $expirationDate

"@
    Write-Host $info
}
using System;
using Interop.Adsi;
using Interop.Adsi.BusinessUnits;
using Softerra.Adaxes.Adsi;
using Softerra.Adaxes.Interop.Adsi.PersistentObjects;
class Program
{
static void Main(string[] args)
{
    const String unitName = "My Unit";
    const String containerName = "My Container"; // Set to null if the
                                                 // Business Unit is located
                                                 // in the root container
    // Connect to the Adaxes service
    AdmNamespace adsNS = new AdmNamespace();
    IAdmService admService = adsNS.GetServiceDirectly("localhost");

    // Build the ADS path of the Business Unit
    String businessUnitsPath = admService.Backend.GetConfigurationContainerPath(
        "BusinessUnits");
    AdsPath pathObj = new AdsPath(businessUnitsPath);
    if (!String.IsNullOrEmpty(containerName))
    {
        pathObj = pathObj.CreateChildPath("CN=" + containerName);
    }
    pathObj = pathObj.CreateChildPath("CN=" + unitName);

    // Bind to the Business Unit
    IAdmBusinessUnit unit =
        (IAdmBusinessUnit)admService.OpenObject(pathObj.ToString(),
            null, null, 0);

    IAdmBusinessUnitMembers members = unit.Members();
    for (int i = 0; i < members.Count; i++)
    {
        IADs member = members.GetObject(i);
        if (!StringComparer.OrdinalIgnoreCase.Equals(member.Class, "user"))
        {
            continue; // we output user accounts only
        }

        IADsUser user = (IADsUser)member;

        String name = (String)user.Get("name");
        String username = (String)user.Get("sAMAccountName");
        String dn = (String)user.Get("distinguishedName");
        String status = user.AccountDisabled ? "disabled" : "enabled";
        String expirationDate = user.AccountExpirationDate.ToShortDateString();

        String info = String.Format(
            "Name: {0}\n" +
            "Username: {1}\n" +
            "DN: {2}\n" +
            "Account Status: {3}\n" +
            "Expiration Date: {4}\n",
            name, username, dn, status, expirationDate);
        Console.WriteLine(info);
    }
}
}

Tip

To determine whether a user is a member of a Business Unit, use the IAdmBusinessUnit::IsMember method.

See also