0 votes

All of our custom command use %adm-CustomAttributeText1% and we want to update that feild to department. Is there a script that we could run that will look trough out custom commands and charng from one to the other?

by (5.8k points)
0

Hello,

Do we understand correctly that you need to replace %adm-CustomAttributeText1% with %department%? Could you, please, specify the types of actions where the update should be performed? If it is only about PowerShell scripts, you can use the following script from our repository: https://www.adaxes.com/script-repository/search-and-replace-text-in-adaxes-scripts-s224.htm. In your case, the $configurationObjectInfos variable should look like the following:

# A hash table with types of configuration objects and their aliases
$configurationObjectInfos = @{
    "CustomCommands" = "adm-CustomCommand";
}

If the replacement should be performed not only in PowerShell script, please, specify the actions to be updated.

0

Correct I need to replace %adm-CustomAttributeText1% with %department%. but that feild is not use with in powershell scripts rather %adm-CustomAttributeText1% is a condition with in commands that add new users to groups.

0

Hello,

Do we understand correctly that %adm-CustomAttributeText1% should only be replaced in the If <property> <relation> <value> conditions in Custom Commands? Are there any other actions/conditions where the value should be replaced?

0

That is correct %adm-CustomAttributeText1% should only be replaced in the If <property> <relation> <value> conditions in Custom Commands.

1 Answer

0 votes
by (177k points)

Hello,

Thank you for the confirmation. Here is the script:

param(
    [Parameter(Mandatory=$true,Position=1)]
    [String]$textToSearch,
    [Parameter(Position=2)]
    [String]$replaceWith
)

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

# A hash table with types of configuration objects and their aliases
$configurationObjectInfos = @{
    "CustomCommands" = "adm-CustomCommand";
}

function CheckConditions($conditions, $textToSearch, $replaceWith, $objectType, $actionConditionList)
{
    $scriptConditionCount = 0
    foreach ($condition in $conditions)
    {            
        if ($condition.Class -ne "adm-AttributeOperatorValueCondition")
        {
            continue
        }

        # Check whether the value contains the specified string
        $objectCondition = $condition.GetCondition()
        $value = $objectCondition.Value

        if($null -eq $value)
        {
            continue
        }

        $valueADsType = $value.ADsType
        $valueString = $value.GetObjectProperty([ref]$valueADsType).ToString()
        if (!($valueString.ToLower().Contains($textToSearch.ToLower())))
        {
            continue
        }

        # Get condition description
        $conditionDescription = $objectCondition.GetDescription($NULL)
        [void]$actionConditionList.AppendLine("`tCondition: $conditionDescription")

        $scriptConditionCount++

        if ([System.String]::IsNullOrEmpty($replaceWith))
        {
            continue
        }

        # Update condition
        $valueString = $valueString.Replace($textToSearch, $replaceWith)
        $value.PutObjectProperty($valueADsType, $valueString)
        $objectCondition.Value = $value
        $condition.SetCondition($objectCondition)
        $condition.SetInfo()
    }
    return ,$scriptConditionCount
}

function CheckActionAndConditionSets($actionsAndConditionsSets, $textToSearch, $replaceWith, $objectType, $actionConditionList)
{
    $scriptConditionCount = 0
    foreach ($actionsAndConditionsSet in $actionsAndConditionsSets)
    {
        # Check conditions
        $result = CheckConditions $actionsAndConditionsSet.Conditions $textToSearch $replaceWith $objectType $actionConditionList
        $scriptConditionCount += $result[0]

        foreach ($elseIf in $actionsAndConditionsSet.ElseIfConditionedActions)
        {
            $result = CheckConditions $elseIf.Conditions $textToSearch $replaceWith $objectType $actionConditionList
            $scriptConditionCount += $result[0]
        }
    }
    return ,$scriptConditionCount
}

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

$totalConditions = 0

foreach ($alias in $configurationObjectInfos.Keys)
{
    # Bind to the configuration container that contains objects of the current type
    $configurationContainerPath = $admService.Backend.GetConfigurationContainerPath($alias)
    $configurationContainer = $admService.OpenObject($configurationContainerPath, $NULL, $NULL, 0)

    # Find configuration objects of the current type
    $type = $configurationObjectInfos[$alias]
    $configurationContainer.SearchFilter =  "(objectCategory=$type)"
    $configurationContainer.PageSize = 500
    $configurationContainer.SearchScope = "ADS_SCOPE_SUBTREE"
    $configurationContainer.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"

    $searcherResult = $configurationContainer.ExecuteSearch()
    $objects = $searcherResult.FetchAll()
    $searcherResult.Dispose()

    # Search scripts for the specified string
    foreach ($objectID in $objects)
    {
        # Bind to the Custom Command
        $object = $admService.OpenObject($objectID.AdsPath, $NULL, $NULL, 0)

        $actionConditionList = New-Object "System.Text.StringBuilder"

        # Perform search in conditions
        $result = CheckActionAndConditionSets $object.ConditionedActions $textToSearch $replaceWith $object.ObjectType $actionConditionList

        if ($result[0] -eq 0)
        {
            continue
        }

        # Output conditions found
        $objectDisplayName = [Softerra.Adaxes.Utils.ObjectNameHelper]::GetObjectName($objectID.AdsPath, "IncludeParentPath")
        Write-Host "Object name: $objectDisplayName" 

        Write-Host "Action/Condition description:"
        Write-Host $actionConditionList.ToString()

        $totalConditions = $totalConditions + $result[0]
        Write-Host "Search text found in" $result[0] "condition(s)"
        Write-Host
    }
}

if ([System.String]::IsNullOrEmpty($replaceWith))
{
    Write-Host "Total conditions found:" $totalConditions
}
else
{
    Write-Host "Search text replaced in $totalConditions conditions."
}

To run the script, save it on the computer where Adaxes service is installed with the ps1 extension (e.g. MyScript.ps1) and execute using the following command:

.\MyScript.ps1 -textToSearch "%adm-CustomAttributeText1%" -replaceWith "%department%"

In the command:

  • textToSearch - Specifies the text to search in Custom Command conditions.
  • replaceWith - Specifies the text to replace all occurrences of the text specified in the textToSearch parameter in Custom Command conditions.
0

Great, I also have Business Rules where I need to do the same swap. Would I use this same script?

0

Hello,

Yes, you can use the very same script. For it to also update Business Rules, the $configurationObjectInfos variable should look like the following:

# A hash table with types of configuration objects and their aliases
$configurationObjectInfos = @{
    "BusinessRules" = "adm-BusinessRule";
    "CustomCommands" = "adm-CustomCommand";    
}
0

When i tried to run the script it doesn't change anything and I get the following as a result. Results Does it matter that we have change the display of adm-CustomAttributeText1 to Department?

0

Hello,

No, the display name does not take effect in this case. Could you, please, provide the exact command you used to run the script?

Also, the script uses the credentials of the currently logged on user to perform the updates. To make sure that all the Custom Commands and Business Rules are found and updated, sign in with the credentials of the Adaxes service account (specified during Adaxes installation).

0

The command i'm using to run the script is .\Update.ps1 -textToSearch "%adm-CustomAttributeText1%" -replaceWith "%department%" I have also tried logged in as the service account

0

Hello,

Thank you for the provided details.

Did you make any changes in the script? If so, please, post here the script with all your modifications or send it to us (support[at]adaxes.com) in TXT format.

Also, could you, please, post here or send us a screenshot of a Custom Command or Business Rule that should be updated by the script?

0

I made no changes to the script.

conditions to update

I have highlighted adm-CustomAttributeText1

0

Hello,

Thank you for the provided details. The thing is that the script searches for the exact text to replace and value references (e.g. %adm-CustomAttributeText1%) can only be used in the value part of the condition. We updated the script according to your requirements, find it below.

param(
    [Parameter(Mandatory=$true,Position=1)]
    [String]$textToSearch,
    [Parameter(Position=2)]
    [String]$replaceWith
)

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

# A hash table with types of configuration objects and their aliases
$configurationObjectInfos = @{
    "CustomCommands" = "adm-CustomCommand";
    "BusinessRules" = "adm-BusinessRule"
}

function CheckConditions($conditions, $textToSearch, $replaceWith, $objectType, $actionConditionList)
{
    $scriptConditionCount = 0
    foreach ($condition in $conditions)
    {            
        if ($condition.Class -ne "adm-AttributeOperatorValueCondition")
        {
            continue
        }

        # Check whether AttributeName contains the specified string
        $objectCondition = $condition.GetCondition()
        $attributeName = $objectCondition.AttributeName

        if($null -eq $attributeName)
        {
            continue
        }

        if (!($attributeName.ToLower() -eq $textToSearch.ToLower()))
        {
            continue
        }

        # Get condition description
        $conditionDescription = $objectCondition.GetDescription($NULL)
        [void]$actionConditionList.AppendLine("`tCondition: $conditionDescription")

        $scriptConditionCount++

        if ([System.String]::IsNullOrEmpty($replaceWith))
        {
            continue
        }

        # Update condition
        $attributeName = $replaceWith
        $objectCondition.AttributeName = $attributeName
        $condition.SetCondition($objectCondition)
        $condition.SetInfo()
    }
    return ,$scriptConditionCount
}

function CheckActionAndConditionSets($actionsAndConditionsSets, $textToSearch, $replaceWith, $objectType, $actionConditionList)
{
    $scriptConditionCount = 0
    foreach ($actionsAndConditionsSet in $actionsAndConditionsSets)
    {
        # Check conditions
        $result = CheckConditions $actionsAndConditionsSet.Conditions $textToSearch $replaceWith $objectType $actionConditionList
        $scriptConditionCount += $result[0]

        foreach ($elseIf in $actionsAndConditionsSet.ElseIfConditionedActions)
        {
            $result = CheckConditions $elseIf.Conditions $textToSearch $replaceWith $objectType $actionConditionList
            $scriptConditionCount += $result[0]
        }
    }
    return ,$scriptConditionCount
}

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

$totalConditions = 0

foreach ($alias in $configurationObjectInfos.Keys)
{
    # Bind to the configuration container for objects of the current type
    $configurationContainerPath = $admService.Backend.GetConfigurationContainerPath($alias)
    $configurationContainer = $admService.OpenObject($configurationContainerPath, $NULL, $NULL, 0)

    # Find configuration objects of the current type
    $type = $configurationObjectInfos[$alias]
    $configurationContainer.SearchFilter =  "(objectCategory=$type)"
    $configurationContainer.PageSize = 500
    $configurationContainer.SearchScope = "ADS_SCOPE_SUBTREE"
    $configurationContainer.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"

    $searcherResult = $configurationContainer.ExecuteSearch()
    $objects = $searcherResult.FetchAll()
    $searcherResult.Dispose()

    # Search conditions for the specified string
    foreach ($objectID in $objects)
    {
        # Bind to the Business Rule or Custom Command
        $object = $admService.OpenObject($objectID.AdsPath, $NULL, $NULL, 0)

        $actionConditionList = New-Object "System.Text.StringBuilder"

        # Perform search in conditions
        $result = CheckActionAndConditionSets $object.ConditionedActions $textToSearch $replaceWith $object.ObjectType $actionConditionList

        if ($result[0] -eq 0)
        {
            continue
        }

        # Output conditions found
        $objectDisplayName = [Softerra.Adaxes.Utils.ObjectNameHelper]::GetObjectName($objectID.AdsPath, "IncludeParentPath")
        Write-Host "Object name: $objectDisplayName" 

        Write-Host "Action/Condition description:"
        Write-Host $actionConditionList.ToString()

        $totalConditions = $totalConditions + $result[0]
        Write-Host "Search text found in" $result[0] "condition(s)"
        Write-Host
    }
}

if ([System.String]::IsNullOrEmpty($replaceWith))
{
    Write-Host "Total conditions found:" $totalConditions
}
else
{
    Write-Host "Search text replaced in $totalConditions conditions."
}

To run the script, you need to use the very same command, but the parameters should contain LDAP names of the property to replace (e.g. adm-CustomAttributeText1) and the property to set (e.g. department). Finally, the command should look like the following:

.\MyScript.ps1 -textToSearch "adm-CustomAttributeText1" -replaceWith "department"
0

Thank you new script worked like a charm.

Related questions

0 votes
0 answers

Using the Adaxes Administration Console, you can perform bulk update of AD users in several ways: Using the Add or Modify Property Wizard: Select the AD users you need in ... multiple user and computer accounts, add users or contacts to a group in bulk, etc.

asked Apr 28, 2009 by Support (215k points)
0 votes
1 answer

Hi, I would like to use the custom commands to deprovision an AD user. Is there a way to automatically remove all groups (besides Domain Users which cannot be removed) from a user? Thanks...

asked May 1, 2014 by decop (250 points)
0 votes
1 answer

HI *, I would kindly ask if there is a way to pass value to running custom script trough web interface. I see some old discussions on the forum that this feature should be come ... I cannot find it. Could you please help me to find out how to proceed? Thanks.

asked Jan 26, 2015 by majin (280 points)
0 votes
1 answer

I need a detailed, step-by-step, of the custom commands

asked May 18 by rrtenaro (450 points)
0 votes
1 answer

This is a long shot but is there a way to script out the creation of Custom Commands? Right now when we create a new office (which is almost 2 times a months) we speend a ... is the 1st 3 letters so if the office is in Miami it MIA-Mangers and so on.

asked Jan 14 by hgletifer (5.8k points)
2,181 questions
1,946 answers
5,391 comments
5,200 users