Hi Guys,
I' d like to get user surname. It's easy using PS but if I want to do it using such ADSI script

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

# Connect to the Adaxes service
$admNS = New-Object("Softerra.Adaxes.Adsi.AdmNamespace")
$admService = $admNS.GetServiceDirectly("localhost")
# Bind user
$userDN = "CN=John Smith,OU=Users,DC=contoso,DC=com"
$user = $admService.OpenObject("Adaxes://$userDN", $NULL, $NULL, 0)
$surname = $user.Get("surname")
$surname

I get: Exception calling "Get" with "1" argument(s): "The 'surname' property cannot be found in the cache."

Do you have any idea, why?
Best regards.

by (510 points)

1 Answer

by (1.2k points)
Best answer
0 votes

The AD attribute is sn and Adaxes aliases this as Last Name. Try sn instead of surname

by (216k points)
0

Hello,

As jiambor mentioned, the LDAP name of the attribute is sn. In scripts, you need to use LDAP names for attributes, not the names under which attributes appear in Adaxes. Thus, a correct version of the script would be:

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

# Connect to the Adaxes service
$admNS = New-Object("Softerra.Adaxes.Adsi.AdmNamespace")
$admService = $admNS.GetServiceDirectly("localhost")
# Bind to user
$userDN = "CN=John Smith,OU=Users,DC=contoso,DC=com"
$user = $admService.OpenObject("Adaxes://$userDN", $NULL, $NULL, 0)
try
{
    $surname = $user.Get("sn")
}
catch
{
    # What to do if the attribute is empty
}

If you are trying to run the script as a part of a Business Rule, Custom Command or Scheduled Task, the script can be simplified by removing unnecessary calls:

# Bind to user
$user = $Context.BindToObjectByDN("CN=John Smith,OU=Users,DC=contoso,DC=com")
try
{
    $surname = $user.Get("sn")
}
catch
{
    # What to do if the attribute is empty
}

If the user whose surname you are trying to get is the target object of the operation, this can be made even easier:

try
{
    $surname = $Context.TargetObject.Get("sn")
}
catch
{
    # What to do if the attribute is empty
}

Related questions

I'm not able to retrieve the Description of a user using ADSI. I'm trying user.Get("description").ToString() on IADsUser I get the error, The 'description' property cannot be found in the cache. Is there a different method I need to use to get Description?

asked Feb 6, 2017 by sdavidson (730 points)
0 votes
1 answer

Hi Guys, Short question. In our organisation we have a buch of the security group that have a specific "class". The class is simple number stored in the ExtensionAttribute1. I'd ... tried to do the something like that with ADSI :cry: Could you please help me?

asked Dec 8, 2014 by axmaster (510 points)
0 votes
1 answer

Code is below. But the subject says it all. When I run the command targeted in this function via the Adaxes GUI or the web interface, it runs without issue. When run using this ... = $null } } end { $admNS = $admService = $credUser = $credPwd = $null } }

asked Apr 3, 2024 by jrtolle (20 points)
0 votes
1 answer

I'm working on trying to update a script for updating office addresses on-demand. I'd like to leverage ADSI for this and I see that you can clear all condition sets ... seeing a documentation gap here or I'm running past the method needed for this. Thanks!

asked Jan 30, 2023 by AbbyR (40 points)
0 votes
1 answer

I am trying to do a bulk update of a few custom properties that I added. I read this article Custom Properties in Search & PowerShell?. But I am not able to retrieve ... able to get other AD properties using the Get, e.g, user.Get("samAccountName") works

asked Jun 20, 2013 by sdavidson (730 points)
0 votes
1 answer