This PowerShell script will retrieve (from the ncsuTwoPartName attribute associated with the supplied Unity ID.


Queries for the two part name associated with a given Unity ID
NCSU Assigned Username
get_ncsutwopartname.ps1 -unityId srleap
Author: Ryan Leap –
Requires: .NET Assembly System.DirectoryServices.Protocols
Param (
[parameter(Mandatory=$true)] [string] $unityId

# Loads the S.DS.P namespace, required for LDAP interaction
Add-Type -AssemblyName System.DirectoryServices.Protocols

# LDAP Server
$ldapServer = “”

# Make a connection object. This does not bind to the ldap store.
# This line of code includes creating the LDAP Directory Identifier.
$ldapConn = New-Object System.DirectoryServices.Protocols.LdapConnection(new-object System.DirectoryServices.Protocols.LdapDirectoryIdentifier($ldapServer, 389))

# Default authentication is negotiate. Change to basic.
$ldapConn.AuthType = [System.DirectoryServices.Protocols.AuthType] “Basic”

# Build the Directory Request
$ldapRequest = New-Object System.DirectoryServices.Protocols.SearchRequest

# Set the search base
$ldapRequest.DistinguishedName = “uid=$unityId,ou=accounts,dc=ncsu,dc=edu”

# Set the search filter
$ldapRequest.Filter = “(objectClass=*)”

# Set the search scope
$ldapRequest.Scope = [System.DirectoryServices.Protocols.SearchScope] “Subtree”

# Set the server side timeout
$ldapRequest.TimeLimit = (New-Object System.TimeSpan(0,0,30))

# Add Attributes for retrieval
$ldapRequest.Attributes.Add(“ncsuTwoPartName”) | Out-Null

# Send the request
$ldapResponse = $ldapConn.SendRequest($ldapRequest, (New-Object System.TimeSpan(0,0,30))) -as [System.DirectoryServices.Protocols.SearchResponse];

# Get the Value for the two part name attribute
$ncsuTwoPartName = $ldapResponse.Entries[0].Attributes[“ncsuTwoPartName”].GetValues([string])

[string] $dn = $ldapRequest.DistinguishedName

Write-Verbose “Distinguished Name: $dn”
Write-Host $ncsuTwoPartName