0 votes

Good Morning,

I was hoping to get some assistance in creating a powershell script that I could run daily that would do the following.

1. Gather a list of all AD users with the "Description" of Manager or Supervisor (or any other titles I choose)
2. Populate the "Manager" field in a property pattern with found users

Any assistance would be great.

Thanks!

by (520 points)

1 Answer

0 votes
by (223k points)
selected by
Best answer

Hello,

Have a look at the following script from our repository: https://www.adaxes.com/script-repositor ... h-s516.htm.

0

That was exactly what I needed!

Thanks!

0

So I ended up getting this to work, but I came across a small issue.

We have quite a few manager/supervisors that are in this list. The drop down in the "Create Users" form does not list these users in any sort of order. This makes scrolling through trying to find the specific user incredibly difficult.

Any ideas what I could change?

Thanks

0

Hello,

We have updated the script to meet your needs, Find it below.

# Search settings
$propertyForList = "distinguishedName" # TOOD: modify me
$propertyForSearch = "description" # TOOD: modify me
$valuesForSearch = @("Value1", "Value2") # TOOD: modify me
$sortBy = "name" # TODO: modify me
$sortDirection = "Ascending" # TODO: modify me. Possible values: Ascending, Descending

# Property Pattern settings
$patternName = "User Pattern" # TODO: modify me
$propertyToUpdate = "manager" # TODO: modify me
$isPropertyRequired = $True # TODO: modify me

function SearchObjects($filter, $properties, $baseObjectPath, $virtualRoot, $sortBy, $sortDirection)
{
    $searcher = $Context.BindToObject($baseObjectPath)
    $searcher.SearchFilter = $filter
    $searcher.SearchScope = "ADS_SCOPE_SUBTREE"
    $searcher.PageSize = 500
    $searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
    $searcher.SetPropertiesToLoad($properties)
    $searcher.VirtualRoot = $virtualRoot
    if ($sortBy -ne $NULL)
    {
        $sortOption = New-Object "Softerra.Adaxes.Adsi.AdmSortOption"
        $sortOption.PropertyName = $sortBy
        $sortOption.Direction = $sortDirection
        $searcher.Sort = $sortOption
    }

    try
    {
        $searchResultIterator = $searcher.ExecuteSearch()
        $searchResults = $searchResultIterator.FetchAll()

        return ,$searchResults
    }
    finally
    {
        # Release resources
        if ($searchResultIterator){ $searchResultIterator.Dispose() }
    }
}

# Search Property Pattern
$propertyPatternsPath = $Context.GetWellKnownContainerPath("PropertyPatterns")

$searchResults = SearchObjects "(&(objectClass=adm-PropertyPattern)(name=$patternName))" @() $propertyPatternsPath $False $NULL $NULL
if ($searchResults.Length -eq 0)
{
    $Context.LogMessage("Property Pattern '$patternName' not found.", "Warning")
    return
}
elseif ($searchResults.Length -gt 1)
{
    $Context.LogMessage("Found more than one Property Pattern with the following name: '$patternName'.", "Warning")
    return
}
$propertyPatternPath = $searchResults[0].AdsPath

# Build search filter for users
$filter = New-Object "System.Text.StringBuilder"
$filter.Append("(&(sAMAccountType=805306368)($propertyForList=*)")
$filter.Append([Softerra.Adaxes.Ldap.FilterBuilder]::CreateOrred($propertyForSearch, [System.String[]]$valuesForSearch))
$filter.Append(")")

# Search users
$searchResults = SearchObjects $filter.ToString() @($propertyForList) "Adaxes://RootDSE" $True $sortBy $sortDirection

$values = New-Object "System.Collections.ArrayList"
foreach ($searchResult in $searchResults)
{
    [void]$values.Add($searchResult.Properties[$propertyForList].Value)
}

# Update Property Pattern
$pattern = $Context.BindToObject($propertyPatternPath)

foreach ($item in $pattern.Items)
{
    if ($item.PropertyName -ieq $propertyToUpdate)
    {
        $pattern.Items.Remove($item)
        break
    }
}

if ($values.Count -eq 0)
{
    return
}

# Create a new item
$item = $pattern.Items.Create()
$item.PropertyName = $propertyToUpdate
$item.IsPropertyRequired = $isPropertyRequired

$constraints = $item.GetConstraints()
$constraint = $constraints.Create("ADM_PROPERTYCONSTRAINTTYPE_VALUERANGE")
$constraint.AreValuesDenied = $False
$constraint.Values = $values.ToArray()
$constraints.Add($constraint)
$item.SetConstraints($constraints)

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

Worked perfectly, thanks for the help!

Related questions

0 votes
1 answer

We are using the dynamic dist list script found below. The issue is we also have to be able to provide overrides, which we are achieving through secondary static ... ADS_PROPERTY_UPDATE", "member", $userDNs) } # Save the changes $Context.TargetObject.SetInfo()

asked Jun 6, 2018 by willy-wally (3.2k points)
0 votes
1 answer

How do I go about getting an export of users that are assigned to a particular dynamic group? The existing export rules don't seem to do the trick. It only pulls info ... the dynamic group, i would like to also report on those that have been licensed for O365

asked Mar 20, 2017 by cubedit (50 points)
0 votes
0 answers

Hello. Is there a way to get a drop down list with custom and dynamic content in Adaxes administration console (or web)? 1. Get list of available number in dailplan from a ... or disable for Lync' from a temp variable populated in #2. How can we address #2?

asked Sep 27, 2016 by Klas (460 points)
0 votes
1 answer

Hi! I want to auto populate the Manager dropdown list (when HR is creating a new user) with the members of two different groups in our AD. Can this be done? Best regards, Kaj Lehtinen

asked Sep 7, 2017 by KajLehtinen (650 points)
0 votes
1 answer

In the Reports section, there is an option to view "All Users" and then export the view. There is also an option to include additional columns. If I include "Manager", ... along with the complete AD path gets exported. Is it possible to export only the name?

asked Jan 14, 2014 by sdavidson (730 points)
2,761 questions
2,495 answers
6,538 comments
1,483,392 users