ncsuTwoPartName

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

[powershell]

<#
.SYNOPSIS
Queries ldap.ncsu.edu for the two part name associated with a given Unity ID
.PARAMETER unityId
NCSU Assigned Username
.EXAMPLE
get_ncsutwopartname.ps1 -unityId srleap
.LINK
http://msdn.microsoft.com/en-us/library/bb332056.aspx
http://www.dougfinke.com/blog/index.php/2010/08/29/how-to-load-net-assemblies-in-a-powershell-session/
http://gallery.technet.microsoft.com/scriptcenter/Using-SystemDirectoryServic-0adf7ef5
.NOTES
Author: Ryan Leap – srleap@ncsu.edu
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 = “ldap.ncsu.edu”

# 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
Write-Host $ncsuTwoPartName
Write-Host

$ldapConn.Dispose()

[/powershell]