Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Right Click in ADUC To Get LDAP Path

Scripting for the Enterprise

Adding Right Click in ADUC To Get LDAP Path

by  markdmac  Posted    (Edited  )
markdmac's Enterprise Ready Scripts

By Mark D. MacLachlan, The Spiders Parlor
http://www.thespidersparlor.com/vbscript



I came up with a simple but very useful script that I thought others might like. Though the code is extemely simple, this is one of those scripts that I consider to have a large WOW factor.

Often when scripting something I will need to grab an LDAP path, I typically use ADSIEdit, but navigating ADSIEdit, and then grabbing an LDAP path can be time consuming.

The following script should be placed on each DC in the same location. Add it to the display specifiers list. It will give you the ability to right click an OU, computer, user or group object etc and be prompted with the LDAP path of the object and allow you to use Ctrl + C to copy it to the clipboard.

Here is the code:
[code Display Specifiers]
On Error Resume Next
strDN = Wscript.Arguments(0)
InputBox vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & _
vbCrLf & "Use Ctrl + C to copy","LDAP Path", strDN
[/code]

For the purposes of this example, save the above code in a vbs file with the following name:
C:\EnterpriseScripts\ShowLDAP.vbs

To add the code to the display specifiers:
1. Open ADSIEdit.
2. Connect to the Configuration database.
3. Expand CN=DisplaySpecifiers
4. Double click CN=409
5. Edit the properties of the following:
ò CN=computer-Display
ò CN=group-Display
ò CN=organizationalUnit-Display
ò CN=user-Display

6. To each of the above add the following line under AdminContextMenu

[code AdminContextMenu]2,Show LDAP,"C:\EnterpriseScripts\ShowLDAP.vbs"[/code]

Note that the path must match where you saved the script locally. The number indicates the position in the right click menu. The text after the first comma is what will be displayed in the right click menu.

7. Close the MMC. Open ADUC and right click a computer, user, group or OU and you can choose to Show LDAP.

[red]You must close all MMC consoles in order for the new Display Specifier to show up in the list.[/red]

I've found a lot of people are confused by the steps required to modify the display specifiers, so I have scripted a solution. Copy the below code to a text file, name it AddDisplaySpecifiers.vbs and double click it to execute.

[code AddDisplaySpecifiers.vbs][green]
'==========================================================================
'
' NAME: AddDisplaySpecifier.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: http://www.thespidersparlor.com
' DATE : 5/14/2009
' COPYRIGHT ¬ 2009, All Rights Reserved
'
' COMMENT: Adds right menu choices to ADUC.
'
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
' ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED To
' THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
' PARTICULAR PURPOSE.
'
' IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS
' BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
' DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
' WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
' ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
' OF THIS CODE OR INFORMATION.
'
'==========================================================================[/green]
Const ADS_PROPERTY_APPEND = 3
[green]
'Modify the Command code below (step 6 from manual instructions)[/green]
CommandCode = "3,ShowLDAP," & Chr(34)& "C:\EnterpriseScripts\ShowLDAP.vbs" & Chr(34)

Dim arrClass()
intIndex = 0
[red]
'Comment out whichever object types you don't want to be included[/red]
BuildClassArray("CN=computer-Display")
BuildClassArray("CN=contact-Display")
BuildClassArray("CN=container-Display")
BuildClassArray("CN=group-Display")
BuildClassArray("CN=publicFolder-Display")
BuildClassArray("CN=server-Display")
BuildClassArray("CN=user-Display")
BuildClassArray("CN=organizationalUnit-Display")

[green]
'Build our array of classes[/green]
Function BuildClassArray(objClass)
ReDim Preserve arrClass(intIndex)
intIndex = intIndex + 1
arrClass(UBound(arrClass)) = CStr(objClass)
End Function
[green]
'Enumerate the classes and add the display specifier[/green]
For Each oType In arrClass
Call AddDisplaySpecifier(oType,CommandCode)
Next
[green]
'Check for errors and report back[/green]
If Err.Number= 0 Then
WScript.Echo "Script completed successfully!"
Else
WScript.Echo "Sorry something went wrong. Verify AD is accessible." & _
vbCrLf & "Error: " & Err.Number & " Error Description: " & Err.Description
End If
[green]
'Worker function that adds to the adminContextMenu[/green]
Function AddDisplaySpecifier(ByVal oType, CommandCode)

Set oRootDSE = GetObject("LDAP://rootDSE")
strForestRoot = oRootDSE.Get("DefaultNamingContext")

strDN = oType & ",CN=409,CN=DisplaySpecifiers," & _
"CN=Configuration," & strForestRoot
Set objObject = GetObject("LDAP://" & strDN)
objObject.PutEx ADS_PROPERTY_APPEND, "adminContextMenu", Array(CommandCode)
objObject.setInfo
End Function
[/code]

Modifications to the AddDisplaySpecifiers Script:


Edit the CommandCode line to switch the display and script names.
Example:
Code:
CommandCode = "3,ShowLDAP," & Chr(34)& "C:\ShowLDAP.vbs" & Chr(34)
               A     B                        C

A = Position in Right Click menu
B = Text to display in Right Click menu
C = Script to execute


You can specify which object types to target by commenting out (') an object class line
Example:
Code:
[green]
'Comment out whichever class you don't want to be included[/green]
BuildClassArray("CN=computer-Display")
BuildClassArray("CN=contact-Display")
BuildClassArray("CN=container-Display")
BuildClassArray("CN=group-Display")
'BuildClassArray("CN=publicFolder-Display")
BuildClassArray("CN=server-Display")
BuildClassArray("CN=user-Display")
BuildClassArray("CN=organizationalUnit-Display")

In the above example Public Folders will not get the new menu choice.

I strive for nothing less than 10s. Before you vote, if you don't think this FAQ rates a 10 please provide feedback to me first. Also please check out my other FAQs in this same forum.

Happy Scripting
Mark D. MacLachlan
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top