Creating an Offline Address List in VBScript

Originally published April 30, 2005

 

Given the techniques we learned in this article about working with binary data in VBScript, plus a simple technique for creating GUIDs in VBScript, we are now prepared to use those capabilities in creating Exchange Server objects.

A GUID is a Globally Unique Identifier. It's a long binary string, unique in a forest, that identifies an object.

One of the objects in Exchange that requires a GUID to be created is the Offline Address List (OAL)/Offline Address Book (OAB).

While this routine will require some modification to fit your environment, those mods are small and the techniques shown are generally applicable to Exchange in specific, and dealing with GUIDs in VBScript in general.

Enjoy!

Function CreateOAL (strDomain, strName)
 Dim strDefault, strContainer, strGUID, strOABName, strAALName
 Dim objDefault, objContainer, objNew
 Dim arrGUID, arrAAL, strLEDN

 CreateOAL = False

 On Error Resume Next
 Err.Clear

 strContainer = "CN=Offline Address Lists,CN=Address Lists Container," & strOrgDN

 ' this is ANY OAL that has reasonable defaults
 strDefault = "CN=Offline Address List - Blue Ridge Internetworks," & strContainer

 Set objDefault = GetObject ("LDAP://" & strDefault)
 If Err Then
  ErrorReport
  e "Couldn't GetObject: " & strDefault
  CreateOAL = True
  Exit Function
 End If

 Set objContainer = GetObject ("LDAP://" & strContainer)
 If Err Then
  ErrorReport
  e "Couldn't GetObject: " & strContainer
  Set objDefault = Nothing
  CreateOAL = True
  Exit Function
 End If

 strGUID = CreateGUID (32)
 Call ConvertHexStringToByteArray (strGUID, arrGUID)

 strAALName = "CN=Addresses - " & strName & _
   ",CN=All Address Lists,CN=Address Lists Container," & strOrgDN
 arrAAL = Array (strAALName)

 ' use objNew as a temporary to verify AAL exists
 Set objNew = GetObject ("LDAP://" & strAALName)
 If Err Then
  ' oops!
  ErrorReport
  e "Couldn't GetObject: " & strAALName
  Set objContainer = Nothing
  Set objDefault   = Nothing
  CreateOAL = True
  Exit Function
 End If
 Set objNew = Nothing

 strOABName = "Offline Address List - " & strName
 strLEDN    = "/O=" & strOrg & "/CN=addrlists/CN=oabs/CN=" & strOABName

 Set objNew = objContainer.Create ("msExchOAB", "CN=" & strOABName)
 If Err Then
  ErrorReport
  e "Couldn't Create: " & strOABName
  Set objCOntainer = Nothing
  Set objDefault   = Nothing
  CreateOAL = True
  Exit Function
 End If

 ' derived or calculated values
 objNew.siteFolderGUID      = arrGUID
 objNew.offlineABContainers = arrAAL
 objNew.legacyExchangeDN    = strLEDN

 ' copied values - most are simple, some aren't
 ' all unique to OAB's
 objNew.doOABVersion        = objDefault.doOABVersion
 objNew.msExchOABFolder     = objDefault.msExchOABFolder
 objNew.offlineABSchedule   = objDefault.offlineABSchedule
 objNew.offlineABServer     = objDefault.offlineABServer
 objNew.offlineABStyle      = objDefault.offlineABStyle
 objNew.siteFolderServer    = objDefault.siteFolderServer

 ' copied values - generic
 objNew.showInAdvancedViewOnly = objDefault.showInAdvancedViewOnly
 objNew.systemFlags            = objDefault.systemFlags

If 1 Then
 wscript.echo "siteFolderGUID = " & OctetToHexStr (objNew.SiteFolderGUID)
 wscript.echo "offlineABContainers = " & objNew.offlineABContainers (0)
 wscript.echo "legacyExchangeDN = " & objNew.legacyExchangeDN
 wscript.echo "doOABVersion = " & objNew.doOABVersion
 wscript.echo "msExchOABFolder = " & octetToHexStr (objNew.msExchOABFolder)
 wscript.echo "offlineABSchedule = " & octetToHexStr (objNew.offlineABSchedule)
 wscript.echo "offlineABServer = " & objNew.offlineABServer
 wscript.echo "offlineABStyle = " & objNew.offlineABStyle
 wscript.echo "siteFOlderServer = " & objNew.siteFolderServer
 wscript.echo "showInAdvancedViewOnly = " & objNew.ShowInAdvancedViewOnly
 wscript.echo "systemFlags = " & objNew.SystemFlags
End If

 objNew.SetInfo
 If Err Then
  ErrorReport
  e "Couldn't SetInfo"
  Set objNew       = Nothing
  Set objCOntainer = Nothing
  Set objDefault   = Nothing
  CreateOAL = True
  Exit Function
 End If

 Set objNew       = Nothing
 Set objDefault   = Nothing
 Set objContainer = Nothing

 wscript.echo "Created new OAB: " & strName
End Function

Function OctetToHexStr (arrbytOctet)
 ' Function to convert OctetString (byte array) to Hex string.

 ' Code from Richard Mueller, a MS MVP in Scripting and ADSI

 Dim k

 OctetToHexStr = ""

 For k = 1 To Lenb (arrbytOctet)
  OctetToHexStr = OctetToHexStr _
        & Right("0" & Hex(Ascb(Midb(arrbytOctet, k, 1))), 2)
 Next
End Function

Sub ConvertHexStringToByteArray (ByVal strHexString, ByRef pByteArray)
 Dim fso, stream, temp, ts, n
 ' This is an elegant way to convert a hex string to a Byte
 ' array. Typename(pByteArray) will return Byte(). pByteArray
 ' should be a null variant upon entry. strHexString should be
 ' an ASCII string containing nothing but hex characters, e.g.,
 ' FD70C1BC2206240B828F7AE31FEB55BE

 ' Code from Michael Harris, a MS MVP in Scripting

 Set fso = CreateObject ("scripting.filesystemobject")
 Set stream = CreateObject ("adodb.stream")

 temp = fso.gettempname ()
 Set ts = fso.createtextfile (temp)

 For n = 1 To (Len (strHexString) - 1) step 2
  ts.write Chr ("&h" & Mid (strHexString, n, 2))
 Next

 ts.close

 stream.type = 1
 stream.open
 stream.loadfromfile temp

 pByteArray = stream.read

 stream.close
 fso.deletefile temp

 Set stream = Nothing
 Set fso = Nothing
End Sub

Function CreateGUID (tmpLength)
 Randomize Timer
 Dim tmpCounter,tmpGUID
 Const strValid = "0123456789ABCDEF"

 For tmpCounter = 1 To tmpLength
  tmpGUID = tmpGUID & Mid (strValid, Int (Rnd (1) * Len (strValid)) + 1, 1)
 Next

 CreateGUID = tmpGUID
End Function

Published Tuesday, November 13, 2007 7:49 PM by michael
Filed under: , ,

Comments

No Comments