Platform SDK: Active Directory, ADSI, and Directory Services

Example Code for Creating an External crossRef Object

The following VBScript examples create an external crossRef object.

'''''''''''''''''''''''''''''''''''''''
'Parse the arguments
'''''''''''''''''''''''''''''''''''''''
On Error Resume Next
Set oArgs = WScript.Arguments
 
If oArgs.Count = 3 Then
  sCrossRefCN = oArgs.item(0)
  sCrossRefDNSRoot = oArgs.item(1)
  sCrossRefNCName = oArgs.item(2)
Else
  sCrossRefCN = InputBox("This script creates a cross reference object." & vbCrLf & vbCrLf &"Specify the name (cn) of the new crossRef object:")
  sCrossRefDNSRoot = InputBox("Specify the DNS root of the server or domain containing the external naming context:")
  sCrossRefNCName = InputBox("Specify the distinguished name (DN) of the external naming context:")
End If
 
If sCrossRefCN = "" Then
  WScript.Echo "No CN was specified. You must specify a CN."
  WScript.Quit(1)
End If
 
If sCrossRefNCName = "" Then
  WScript.Echo "No DN was specified. You must specify a DN."
  WScript.Quit(1)
End If
 
If sCrossRefDNSRoot = "" Then
  WScript.Echo "No DNS root was specified. You must specify a DNS root."
  WScript.Quit(1)
End If
 
sPrefix = "LDAP://"
 
'Get distinguished name for config container and build ADsPath to partitions container.
  Set root= GetObject(sPrefix & "rootDSE")
  If (Err.Number <> 0) Then
     BailOnFailure Err.Number, "on GetObject method"
  End If
  sConfigDN = root.Get("configurationNamingContext")
  If (Err.Number <> 0) Then
     BailOnFailure Err.Number, "on Get method"
  End If
  sContainerDN = "cn=Partitions," & sConfigDN
 
'''''''''''''''''''''''''''''''''''''''
'Bind to the container
'''''''''''''''''''''''''''''''''''''''
Set cont= GetObject(sPrefix & sContainerDN)
If (Err.Number <> 0) Then
  BailOnFailure Err.Number, "on GetObject method for partitions container"
End If
'''''''''''''''''''''''''''''''''''''''
'Add the user
'''''''''''''''''''''''''''''''''''''''
Set newCrossRef  = cont.Create("crossRef", "cn=" & sCrossRefCN)
If (Err.Number <> 0) Then
  BailOnFailure Err.Number, "on Create method"
End If
newCrossRef.put "dnsRoot", sCrossRefDNSRoot
If (Err.Number <> 0) Then
  BailOnFailure Err.Number, "on Put method call for dnsRoot"
End If
newCrossRef.put "nCName", sCrossRefNCName
If (Err.Number <> 0) Then
  BailOnFailure Err.Number, "on Put method call for nCName"
End If
newCrossRef.SetInfo
If (Err.Number <> 0) Then
  BailOnFailure Err.Number, "on SetInfo method"
End If
strText = "The crossRef " & sCrossRefCM & " was successfully added."
strText = strText & vbCrLf & "The crossRef has the following properties:"
'Refresh the property cache
newCrossRef.GetInfo
count = newCrossRef.PropertyCount
If (Err.Number <> 0) Then
  BailOnFailure Err.Number, "on PropertyCount method"
End If
strText = strText & "Number of properties: " & count 
 
'Set v = user.Next()
'cprop = 1
'If (Err.Number <> 0) Then
'  BailOnFailure Err.Number, "on Next method"
'End If
 
'While (Not (IsNull(v)) And Err.Number = 0)
'  cprop = cprop+1
'  Set v = user.Next()
'If (Err.Number <> 0) Then
'  BailOnFailure Err.Number, "on Next method"
'End If
'Wend
For cprop=1 to count
  Set v = newCrossRef.Next()
  If IsNull(v) Then
    Exit For
  End If
  strText = strText & vbCrLf & cprop & ") " & v.Name & " "
  cattr = 0
  attr = v.Values
  For Each attrval In attr
    cattr = cattr+1
    if (v.adstype=3) then
      strText = strText & vbCrLf & "      " & attrval.caseignorestring
    end if
    if (v.adstype=1) then
      strText = strText & vbCrLf & "      " & attrval.DNstring
    end if
    if (v.adstype=7) then
      strText = strText & vbCrLf & "      " & attrval.Integer
    end if
    if (v.adstype=6) then
      strText = strText & vbCrLf & "      " & attrval.Boolean
    end if
    if (v.adstype=9) then
      strText = strText & vbCrLf & "      " & attrval.UTCTime
    end if
    if (v.adstype=10) then
      strText = strText & vbCrLf & "      " &  "Type: LargeInteger"
    end if
    if (v.adstype=8) then
      strText = strText & vbCrLf & "      " &  "Type: OctetString"
    end if
    if (v.adstype=25) then
      strText = strText & vbCrLf & "      " &  "Type: NTSecurityDescriptor"
    end if
  Next
 
Next
show_items strText, "Create crossRef"
 
'''''''''''''''''''''''''''''''''''''''
'Display subroutines
'''''''''''''''''''''''''''''''''''''''
Sub show_items(strText, strName)
    MsgBox strText, vbInformation, "Create CrossRef"
End Sub
 
Sub BailOnFailure(ErrNum, ErrText)    strText = "Error 0x" & Hex(ErrNum) & " " & ErrText
    MsgBox strText, vbInformation, "ADSI Error"
    WScript.Quit
End Sub