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)