26 March 2008 0 Comments

Scripting the creation of a Lotus Notes DB replica

In my company, we have a lot of laptops as our staff mostly work out of the office. They constantly VPN in to get their mail, and our recommended best practice is to replicate their Lotus Notes mail rather than working online. However, when setting up a laptop for a new staff member, that meant we’d manually have to create a Mail file replica for them which was a bit time consuming.

After mucking around with the Notes COM objects, I discovered we can actually automate this fairly easily. Attached below is the VBScript that is called as part of a New Laptop setup. In a seperate tool, the staff member is prompted for their Lotus Notes password, which gets passed as an argument to this script. It could probably be more automated, but this’ll do us for now.

By the way, I’ve tested this on Lotus Notes 6.x and 7.x

' Variable declaration
Dim objShell, objLNSession, strSessionFolder, strSessionDB, blnErrorOccurred
' --------------------------------------------------------------------------------------------
' Create Objects for use
Set objShell        =        CreateObject("WScript.Shell")
Set objLNSession    =        CreateObject("Lotus.NotesSession")
' --------------------------------------------------------------------------------------------
' Initialise a Lotus Notes session using the argument as the password
Err.Clear
Call objLNSession.Initialize(WSH.Arguments(0))

Select Case Err.Number
   Case 0
   Case 438
      strError = "An incorrect password was entered."
      blnErrorOccurred = True
   Case Else
      strError = "An error occured while initialising Lotus Notes"
      blnErrorOccurred = True
End Select

If blnErrorOccurred Then
   WScript.Echo strError & " Errorcode " & Err.Number & ". Exiting..."
   WScript.Quit
End If
' --------------------------------------------------------------------------------------------
' Discover the users mail server
Err.Clear
strMailServer = objLNSession.GetEnvironmentString("MailServer", True)

Select Case Err.Number
   Case 0
   Case Else
      strError = "There was a problem connecting to the mailserver."
      blnErrorOccurred = True
End Select

If blnErrorOccurred Then
   WScript.Echo strError & " Errorcode " & Err.Number & ". Exiting..."
   WScript.Quit
End If
' --------------------------------------------------------------------------------------------
' Open the data folder on the mailserver
Err.Clear
Set strSessionFolder = objLNSession.GetDbDirectory(strMailServer)

Select Case Err.Number
   Case 0
   Case Else
      strError = "There was a problem connecting to the mailserver data directory."
      blnErrorOccurred = True
End Select

If blnErrorOccurred Then
   WScript.Echo strError & " Errorcode " & Err.Number & ". Exiting..."
   WScript.Quit
End If

' --------------------------------------------------------------------------------------------
' Open the users mail database
Err.Clear
Set strSessionDB = strSessionFolder.OpenMailDatabase

Select Case Err.Number
   Case 0
   Case Else
      strError = "There was a problem opening the user mail database."
      blnErrorOccurred = True
End Select

If blnErrorOccurred Then
   WScript.Echo strError & " Errorcode " & Err.Number & ". Exiting..."
   WScript.Quit
End If

' --------------------------------------------------------------------------------------------
' Create a new local replica of the mailfile using the same name as the server copy)
WScript.Echo "Creating replica: " & strMailServer & "\" & strSessionDB & ". Please wait..."
Err.Clear
Call strSessionDB.CreateReplica("", strSessionDB)

Select Case Err.Number
   Case 0
   Case -2147217499
      strError = "A local replica already exists."
   Case Else
      strError = "There was a problem creating a new replica."
      blnErrorOccurred = True
End Select

If blnErrorOccurred Then
   WScript.Echo strError & " Errorcode " & Err.Number & ". Exiting..."
   WScript.Quit
End If

WScript.Echo "Replication Successful. Exiting..."

Leave a Reply