Script Repository


Export custom attribute values to CSV

January 09, 2019
2462

The script exports values of custom attributes of an Active Directory object to a CSV file.

Custom attributes and not stored in AD, but can be used in Adaxes the same as any other attributes of AD objects. They are provided by Adaxes and stored on Adaxes Service backend server (AD LDS).

Parameter:

  • $csvFilePath - specifies a a template for the CSV file path that will contain custom attributes of an AD object. You can use value references (e.g. %username%) to insert properties of the object as a part of the path.

Adaxes version 2017.2 and earlier

Edit Remove
PowerShell
$csvFilePath = "\\server\share\CustomAttributes_%name%.csv" # TODO: modify me

function GetAttributeValue ($attributeName)
{
    try
    {
        $value = $Context.TargetObject.Get($attributeName)
    }
    catch
    {
        $value = $NULL
    }
    
    return $value
}

$customAttributesInfo = @{
    "adm-CustomAttributeText" = 1..30
    "adm-CustomAttributeBoolean" = 1..25
    "adm-CustomAttributeDate" = 1..5
    "adm-CustomAttributeInt" = 1..5
    "adm-CustomAttributeTextMultiValue" = 1..10
    "adm-CustomAttributeTimestamp" = 1..5
}

# Get display names for all attributes
$culture = [System.Globalization.CultureInfo]::CurrentCulture
$attributeFriendlyNamesCache = [Softerra.Adaxes.Directory.AttributeFriendlyNamesCache]::GetInstance($culture)

# Build report
$records = New-Object System.Collections.ArrayList
foreach ($attributeNameTemplate in $customAttributesInfo.Keys)
{
    $attributeNumbers = $customAttributesInfo[$attributeNameTemplate]
    foreach ($number in $attributeNumbers)
    {
        $attributeName = "$attributeNameTemplate$number"
        $value = GetAttributeValue $attributeName
        if ($value -eq $NULL)
        {
            continue
        }

        if ($attributeNameTemplate -eq "adm-CustomAttributeTimestamp")
        {
            switch ($value)
            {
                "0"
                {
                    $value = "Never"
                }
                "9223372036854775807"
                {
                    $value = "Never"
                }
                default
                {
                    $value = [DateTime]::FromFileTimeUtc([Int64]::Parse($value))
                }
            }
        }
        
        if ($attributeNameTemplate -eq "adm-CustomAttributeTextMultiValue")
        {
            $value = [System.String]::Join(";", $value)
        }

        if ($attributeFriendlyNamesCache.HasFriendlyName($attributeName))
        {
            $attributeName = $attributeFriendlyNamesCache.GetFriendlyName($attributeName, $Context.TargetObject.Class)
        }
        
        $record = New-Object PSObject
        $record | Add-Member -MemberType NoteProperty -Name "Name" -Value $attributeName
        $record | Add-Member -MemberType NoteProperty -Name "Value" -Value $value
        [void]$records.Add($record)
    }
}

if ($records.Count -eq 0)
{
    $Context.LogMessage("The object does not have any custom attributes specified.", "Information")
    return
}

# Export report to CSV
$records | Export-Csv -Path $csvFilePath -NoTypeInformation

Adaxes version 2018.1 and later

Edit Remove
PowerShell
$csvFilePath = "\\server\share\CustomAttributes_%name%.csv" # TODO: modify me

function GetAttributeValue ($attributeName)
{
    try
    {
        $value = $Context.TargetObject.Get($attributeName)
    }
    catch
    {
        $value = $NULL
    }
    
    return $value
}

$customAttributesInfo = @{
    "adm-CustomAttributeText" = 1..30
    "adm-CustomAttributeBoolean" = 1..25
    "adm-CustomAttributeDate" = 1..5
    "adm-CustomAttributeInt" = 1..5
    "adm-CustomAttributeTextMultiValue" = 1..10
    "adm-CustomAttributeTimestamp" = 1..5
}

# Get display names for all attributes
$path = $Context.GetWellKnownContainerPath("ConfigurationSetSettings")
$configurationContainer = $Context.BindToObject($path)
$culture = [System.Globalization.CultureInfo]::CurrentCulture
$attributeFriendlyNames = $configurationContainer.GetAttributeFriendlyNames($culture.ThreeLetterISOLanguageName, "ADM_GETATTRFRIENDLYNAMESMODE_MERGED")
$attributeFriendlyNamesMap = @{}
foreach ($attributeFriendlyName in $attributeFriendlyNames)
{
    $ldapPropertyName = $attributeFriendlyName.AttributeName

    $typeSpecificFriendlyNames = $attributeFriendlyName.TypeSpecificFriendlyNames
    if ($typeSpecificFriendlyNames.Length -eq 0)
    {
        $attributeFriendlyNamesMap.Add($ldapPropertyName, $attributeFriendlyName.GenericFriendlyName)
        continue
    }
    
    $friendlyName = $NULL
    foreach ($type in $typeSpecificFriendlyNames)
    {
        if ($type.ObjectType -ne $Context.TargetObject.Class)
        {
            continue
        }
        $friendlyName = $type.FriendlyName
        break
    }
    
    if ($friendlyName -eq $NULL)
    {
        $attributeFriendlyNamesMap.Add($ldapPropertyName, $attributeFriendlyName.GenericFriendlyName)
    }
    else
    {
        $attributeFriendlyNamesMap.Add($ldapPropertyName, $friendlyName)
    }
}

# Build report
$records = New-Object System.Collections.ArrayList
foreach ($attributeNameTemplate in $customAttributesInfo.Keys)
{
    $attributeNumbers = $customAttributesInfo[$attributeNameTemplate]
    foreach ($number in $attributeNumbers)
    {
        $attributeName = "$attributeNameTemplate$number"
        $value = GetAttributeValue $attributeName
        if ($value -eq $NULL)
        {
            continue
        }

        if ($attributeNameTemplate -eq "adm-CustomAttributeTimestamp")
        {
            switch ($value)
            {
                "0"
                {
                    $value = "Never"
                }
                "9223372036854775807"
                {
                    $value = "Never"
                }
                default
                {
                    $value = [DateTime]::FromFileTimeUtc([Int64]::Parse($value))
                }
            }
        }
        
        if ($attributeNameTemplate -eq "adm-CustomAttributeTextMultiValue")
        {
            $value = [System.String]::Join(";", $value)
        }

        if ($attributeFriendlyNamesMap.ContainsKey($attributeName))
        {
            $attributeName = $attributeFriendlyNamesMap[$attributeName]
        }
        
        $record = New-Object PSObject
        $record | Add-Member -MemberType NoteProperty -Name "Name" -Value $attributeName
        $record | Add-Member -MemberType NoteProperty -Name "Value" -Value $value
        [void]$records.Add($record)
    }
}

if ($records.Count -eq 0)
{
    $Context.LogMessage("The object does not have any custom attributes specified.", "Information")
    return
}

# Export report to CSV
$records | Export-Csv -Path $csvFilePath -NoTypeInformation

Comments ( 1 )
avatar
George
Jan 08, 2019
This was developed prior to the newest release and therefore does not work since the Friendly name cache is no longer in the look area that this script refers to.
avatar
Support
Jan 09, 2019
Thank you for pointing this out. We have added a script that can be used for exporting values of custom attributes in Adaxes version 2018.1 and later.
Leave a comment