Hello,
There is one small issue with your version of the script. The thing is that it adds a new department to the list of departments when a new OU is created, but if an OU is deleted, your version of the script will not update the Property Pattern. We suggest a bit different approach.
You can create two Business Rules, one of which will be triggered after creating an OU, the other one will be triggered after deleting an OU. Both the Business Rules will launch a Custom Command that will run a PowerShell script. The script will update the list of departments based on the names of OUs under the Departments OU. Running script from a Custom Command (and not directly in the Business Rules) will allow you to keep the script in a single place. If you need to modify the script in the future, you will need to update only the Custom Command. Also, you can create a Scheduled Task that will run periodically and and launch the same Custom Command to update the list of departments. This is useful if some OUs will be created or deleted outside of Adaxes. To implement this functionality:
I. Create the Custom Command that will launch the script
-
Create a new Custom Command.
-
On the 1st step of the Create Custom Command wizard, you can disable the Custom Command. Disabled Custom Commands are not visible for users in the UI and cannot be executed manually, but can be executed from Business Rules, Custom Commands or Scheduled Tasks. To disable the Custom Command, uncheck the Enabled option.
-
On the 2nd step, select the Organizational-Unit object type.
-
On the 3rd step, add the Run a program or PowerShell script action and paste the following script:
# Get a list of department names
$Context.TargetObject.Filter = @("organizationalUnit")
$departmentNames = @()
foreach ($department in $Context.TargetObject)
{
$departmentNames += $department.Get("name")
}
if ($departmentNames.Length -eq 0)
{
return
}
# Build the ADS path of the built-in Property Pattern called 'User Pattern'
$propertyPatternsPath = $Context.GetWellKnownContainerPath("PropertyPatterns")
$propertyPatternsPathObj = New-Object "Softerra.Adaxes.Adsi.AdsPath" $propertyPatternsPath
$builtinPathObj = $propertyPatternsPathObj.CreateChildPath("CN=Builtin")
$patternPath = $builtinPathObj.CreateChildPath("CN=User Pattern")
# Bind to the Property Pattern
$pattern = $Context.BindToObject($patternPath)
# Delete the item for the 'Department' property
foreach ($item in $pattern.Items)
{
if ($item.PropertyName -ieq "department")
{
$pattern.Items.Remove($item)
break
}
}
# Create a new item for the 'Department' property
$item = $pattern.Items.Create()
$item.PropertyName = "department"
$item.IsPropertyRequired = $False # Set to $True to make the 'Department' property required
$constraints = $item.GetConstraints()
$constraint = $constraints.Create("ADM_PROPERTYCONSTRAINTTYPE_VALUERANGE")
$constraint.AreValuesDenied = $False
$constraint.Values = $departmentNames
$constraints.Add($constraint)
$item.SetConstraints($constraints)
# Save the changes
$item.SetInfo()
$pattern.Items.Add($item)
-
Add a short description for the script and click OK.
-
Finish creation of the Custom Command.
II. Create Business Rules to update the list of departments after creating or deleting a department OU
-
Create a new Business Rule.
-
On the 2nd step:
- for the Business Rule that will be executed after creating an OU, select Organizational-Unit and After Creating an Organizational-Unit;
- for the Business Rule that will be executed after deleting an OU, select Organizational-Unit and After Deleting an Organizational-Unit.
-
On the 3rd step, add the Execute a Custom Command action and click Select.
-
In the dialog box that appears, select the Custom Command that you created on step I.
-
Click OK two times.
-
On the 4th step, assign the Business Rule over your Departments OU and specify the Child objects of this Organizational-Unit and Immediate child objects only in the Assignment Options dialog.
-
Finish creation of the Business Rule.
III. Create a Scheduled Task to update the list of departments on schedule
- Create a new Scheduled Task.
- On the 3rd step, select the Organizational-Unit object type.
- On the 4th step, add the Execute a Custom Command action and click Select.
- In the dialog box that appears, select the Custom Command that you created on step I.
- Click OK two times.
- On the 4th step, assign theScheduled Task over your Departments OU and specify the Child objects of this Organizational-Unit and Immediate child objects only in the Assignment Options dialog.
- Finish creation of the Scheduled Task.