0 votes

Hi all,

I got a script to count up a special AD Attribute which is like a customer ID.
In this script it is called "PhanSachbearbNr" an starts with the prefix 9.
So I want to create a new user, and create a script which looks for the highest ID, and count it up +1.
Write it into the AD Attribute and in my mask of the user creation when the ID is unique.
My script returns everytime only "9" and does not check if its a unique ID.
Getting an Error that:
Exception calling "FetchAll" with "0" argument(s): "Im Verzeichnisdienst ist ein unbekannter Fehler aufgetreten. (Server: xxx)"
You cannot call a method on a null-valued expression.

could you please help me and check the script? I'm not a specialist in powershell.

Thank you

$attributeName = "PhanSachBearbNr"
$idWertname = "PhanSachBearbNr" # anpassbar
$Idstandardlaenge = 5 # anpassbar
$standardwert = "9" # anpassbar

# Abfrage Wert
$idLength = $Idstandardlaenge
$prefix = $standardwert

# Abfrage hoechste ID Nummer
$idPartLength = [int]($idLength - 1)
try
{
    $searcher = $Context.BindToObject("Adaxes://rootDSE")
    $searcher.SearchFilter = "(&(sAMAccountType=805306368)($idWertname=$prefix`*))"
    $searcher.SearchScope = "ADS_SCOPE_SUBTREE"
    $searcher.PageSize = 5500
    $searcher.Sort = $idWertname
    $searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
    $searcher.SetPropertiesToLoad(@($idWertname))
    $searcher.VirtualRoot = $True

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

    if ($searchResults.Length -eq 0)
    {
        $idPart = 0
    }
    else
    {
        $id = $searchResults[$searchResults.Count - 1].Properties[$idWertname].Value
        $idPart = [int]$id.SubString(1, $idPartLength)
        $idPart++
    }
}
finally
{
    # Ausgabe Ergebnis
    $searchResultIterator.Dispose()
}

# Vergabe neuer ID
if ($idPart.ToString().Length -gt $idPartLength)
{
    $Context.Cancel("Erreicht die maximale Grenze fuer die ID")
    return
}

$id = "$prefix$($idPart.ToString('0' * $idPartLength))"

# Setzen der neuen ID fuer den betreffenden Benutzer
$Context.SetModifiedPropertyValue($idWertname, $id)
by (450 points)

Please log in or register to answer this question.

Related questions

0 votes
1 answer

Hi, I'm trying to add a column to a report to retrieve the last login data from AAD/Graph using the script posted here but i'm running into the ... [0].signInActivity.lastSignInDateTime # Assign a column value to $Context.Value $Context.Value = $lastLogonDate

asked Feb 22, 2022 by richarddewis (260 points)
0 votes
1 answer

This is for license purposes and we do not want them visible in the Adaxes portal.

asked Oct 22, 2021 by jfrederickwl (20 points)
0 votes
1 answer

Is it possible to script having users added (or removed) from a Security Group based on another AD Attribute? I have found ways to do this in Powershell (something like): ... just utilize the PS script and just run it through Adaxes on a timed fashion? Thanks!

asked Oct 7, 2014 by PunkinDonuts (360 points)
0 votes
1 answer

Hi, I am making business rule which calls powershell script and inside the script I need to check whether account which is added to group is security group. I am using Get- ... , the same command return, that group type is security So what am I doing wrong?

asked Feb 20, 2020 by KIT (960 points)
0 votes
1 answer

Dear Is it possible to change the values of a custom attribute (adm-CustomAttributeTextMultiValue4) when selecting a value from another custom attribute (adm-CustomAttributeText1). For ... , 2Latijn, 3Latijn, etc... Is this possible? Sincerly Hilmi Emre Bayat

asked Aug 26, 2019 by hilmiemrebayat (120 points)
3,551 questions
3,242 answers
8,241 comments
547,827 users