We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script repository

Copy custom attribute values between Adaxes services

February 25, 2021 Views: 1378

The script copies values of custom attributes from one Adaxes service to another. Execute the script in Windows PowerShell on the computer where an Adaxes service is installed. When prompted, first specify the credentials of the Adaxes service account (specified during Adaxes installation) for the source service and then for the target one.

Parameters:

  • $adaxesServiceSourceName - Specifies the fully-qualified domain name (FQDN) of the computer where the source Adaxes service is installed.
  • $adaxesServiceTargetName - Specifies the fully-qualified domain name (FQDN) of the computer where the target Adaxes service is installed.
  • $customAttributesInfo - Maps LDAP names of the custom attributes whose values will be copied with their numbers.

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

$adaxesServiceSourceName = "AdaxesServiceSource.example.com" # TODO: modify me
$adaxesServiceTargetName = "AdaxesServiceTarget.example.com" # TODO: modify me

$customAttributesInfo = @{
    "adm-CustomAttributeText" = 1..40
    "adm-CustomAttributeBoolean" = 1..40
    "adm-CustomAttributeDate" = 1..15
    "adm-CustomAttributeInt" = 1..5
    "adm-CustomAttributeTextMultiValue" = 1..10
    "adm-CustomAttributeTimestamp" = 1..15
    "adm-CustomAttributeBinary" = 1..15
} # TODO: modify me

function SearchExtendingObjects ($searcher, $list, $filter)
{
    $searcher.SearchFilter = $filter
    $searcher.PageSize = 500

    try
    {
        $searchIterator = $searcher.ExecuteSearch()
        while ($True)
        {
            # Get the next page of search results
            $searchResults = $searchIterator.GetNextPage()
            $searchResults | %{$list.Add([guid]$_.Properties["adm-ExtendingObjectGuid"].Value, $_)}
            if (($searchResults.Length -eq 0) -and ($searchIterator.IsSearchCompleted))
            {
                break # no more results to return
            }
        }
    }
    finally
    {
        if ($searchIterator) { $searchIterator.Dispose() }
    }
}

function CopyCustomAttributeValues ($targetExtendingObject, $sourceSearchResult, $customAttributesInfo)
{
    foreach ($attributeNameTemplate in $customAttributesInfo.Keys)
    {
        $attributeNumbers = $customAttributesInfo[$attributeNameTemplate]
        foreach ($number in $attributeNumbers)
        {
            $attributeName = "$attributeNameTemplate$number"
            if (!$sourceSearchResult.ContainsProperty($attributeName))
            {
                continue
            }

            $values = $sourceSearchResult.GetPropertyByName($attributeName).Values.ToArray()
            if ($values.Count -eq 1)
            {
                $values = $values[0]
            }
            else
            {
                $values = $values
            }
            $targetExtendingObject.Put($attributeName, $values)
        }
    }
}

# Get credentails
$adaxesServiceSourceCred = Get-Credential -Message "Enter credentials for $adaxesServiceSourceName Adaxes service"
$adaxesServiceTargetCred = Get-Credential -Message "Enter credentials for $adaxesServiceTargetName Adaxes service"

$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admSourceService = $admNS.GetServiceDirectly($adaxesServiceSourceName)
$extendingObjectSourceContainer = $admSourceService.OpenObject("Adaxes://CN=Extending Objects,CN=Adaxes Configuration,CN=Adaxes", $adaxesServiceSourceCred.UserName, $adaxesServiceSourceCred.GetNetworkCredential().Password, 0)
$admTargetService = $admNS.GetServiceDirectly($adaxesServiceTargetName)
$extendingObjectTargetContainer = $admTargetService.OpenObject("Adaxes://CN=Extending Objects,CN=Adaxes Configuration,CN=Adaxes", $adaxesServiceTargetCred.UserName, $adaxesServiceTargetCred.GetNetworkCredential().Password, 0)

# Build filter
$filter = New-Object System.Text.StringBuilder
[void]$filter.Append("(&(objectClass=adm-ExtendingObject)(|")
foreach ($attributeNameTemplate in $customAttributesInfo.Keys)
{
    $attributeNumbers = $customAttributesInfo[$attributeNameTemplate]
    foreach ($number in $attributeNumbers)
    {
        $attributeName = "$attributeNameTemplate$number"
        [void]$filter.Append("($attributeName=*)")
    }
}
[void]$filter.Append("))")

$searchResultsSourceService = @{}
SearchExtendingObjects $extendingObjectSourceContainer $searchResultsSourceService $filter.ToString()

$searchResultsTargetService = @{}
SearchExtendingObjects $extendingObjectTargetContainer $searchResultsTargetService "(objectClass=adm-ExtendingObject)"

foreach ($extendingObjectGuid in $searchResultsSourceService.Keys)
{
    $sourceSearchResult = $searchResultsSourceService[$extendingObjectGuid]
    if ($searchResultsTargetService.ContainsKey($extendingObjectGuid))
    {
        $targetSearchResult = $searchResultsTargetService[$extendingObjectGuid]
        $targetExtendingObject = $admTargetService.OpenObject($targetSearchResult.AdsPath, $adaxesServiceTargetCred.UserName, $adaxesServiceTargetCred.GetNetworkCredential().Password, 0)
        CopyCustomAttributeValues $targetExtendingObject $sourceSearchResult $customAttributesInfo
    }
    else
    {
        $targetExtendingObject = $extendingObjectTargetContainer.Create($sourceSearchResult.ObjectType, "CN=" + $sourceSearchResult.Name)
        $targetExtendingObject.Put("adm-ExtendingObjectGuid", $sourceSearchResult.GetPropertyByName("adm-ExtendingObjectGuid").Values[0])
        CopyCustomAttributeValues $targetExtendingObject $sourceSearchResult $customAttributesInfo
    }
    $targetExtendingObject.SetInfo()
}
Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers