Script Repository


Import possible values for property from file

February 23, 2021
1633

The script imports possible values for an AD property from a text file and saves them in the property pattern you specify.

The script must be run on the computer where Adaxes service is installed. To run it, save the text of the script to a PS1 file on the computer.

When the script runs, enter credentials of an Adaxes service administrator.

Syntax:

Edit Remove
PowerShell
.\UpdatePropertyPattern.ps1 -filePath "C:\Import\Values.txt" `
    -patternName "User Pattern" -propertyLDAPName c

Parameters:

  • filePath - Specifies a path to the text file with values that will be imported.
  • $patternName - Specifies a name of the property pattern to import values to.
  • $propertyLDAPName - Specifies the LDAP display name of the property for which the possible values are specified.

Edit Remove
PowerShell
param(
    [Parameter(Mandatory=$true)]
    $filePath, 
    [Parameter(Mandatory=$true)]
    $patternName,
    [Parameter(Mandatory=$true)]
    $propertyLDAPName
)

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

# Check file path
if ([System.String]::IsNullOrEmpty($filePath) -or !(Test-Path $filePath))
{
    Write-Warning "The specified file does not exist or insufficient permissions to access it."
    return
}

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

# Prompt for credentials of a service administrator
$credential = Get-Credential

# Find the Property Pattern
$propertyPatternsPath = $admService.Backend.GetConfigurationContainerPath("PropertyPatterns")
$searcher = $admService.OpenObject($propertyPatternsPath, $credential.UserName,`
    $credential.GetNetworkCredential().Password, 0)
$searcher.SearchFilter = "(&(objectCategory=adm-PropertyPattern)(name=$patternName))"
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.PageSize = 500
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"

try
{
    $searchResultIterator = $searcher.ExecuteSearch()
    $searchResults = $searchResultIterator.FetchAll()
    if ($searchResults.Count -eq 0)
    {
        Write-Warning "Property Pattern '$patternName' was not found."
        return
    }
    elseif ($searchResults.Count -gt 1)
    {
        Write-Warning "Found more than one Property Pattern with name '$patternName'."
        return
    }
    
    $patternPath = $searchResults[0].AdsPath
}
finally
{
    $searchResultIterator.Dispose()
}

$pattern = $admService.OpenObject($patternPath, $credential.UserName,`
    $credential.GetNetworkCredential().Password, 0)

# Delete an item for the property if it exists
foreach ($item in $pattern.Items)
{
    if ($item.PropertyName -ieq $propertyLDAPName)
    {
        $pattern.Items.Remove($item)
        break
    }
}

# Create a new item for the property
$item = $pattern.Items.Create()
$item.PropertyName = $propertyLDAPName

$constraints = $item.GetConstraints()
$constraint = $constraints.Create(
    "ADM_PROPERTYCONSTRAINTTYPE_VALUERANGE")
$constraint.AreValuesDenied = $False
$values = [System.String[]](Get-Content $filePath)
$constraint.Values = $values
$constraints.Add($constraint)
$item.SetConstraints($constraints)

# Save changes
$item.SetInfo()
$pattern.Items.Add($item)

Comments ( 0 )
No results found.
Leave a comment