Powershell and Lotus Notes

As you might know, Powershell works best with other Microsoft products, like MS Exchange.

Now what happens when you want to connect PS to some non Microsoft products? In my case that was Lotus Notes. The collaboration giant. As you probably know, Lotus has its own LotusScritp language, with whom you may create Lotus databases, agents, users, etc… With it you can do almost anything in Lotus.
Since I like to test and experiment with Powershell, and one of my projects was Powershell script and Exchange, Self-service user portal, I decided to play with Powershell and Lotus Notes. And I must say, I works.

First of all, If you want to connect to Lotus Notes via PS, you must start powershell or PS ISE in 32bit mode.

#open powershell in 32bit mode
#Start-Process $Env:WINDIR\SysWOW64\WindowsPowerShell\v1.0\powershell.exe
#or ISE
#Start-Process $Env:WINDIR\SysWOW64\WindowsPowerShell\v1.0\powershell_ise.exe
if([Environment]::Is64BitProcess -eq $true)
                    {
                    write-output "64bit NO GO" 
                    BREAK
                    } 
            else {
                        write-output "32bit OK"
                        } #because you have a 64-bit PowerShell

Once you open the PS or ISE (I’m more of ISE or Visual Code person), you can start connecting to Lotus Notes.

One of my tasks, was to extract an HTML attachment which was sent everyday by our backup system. But with this code, will go through all mail documents in Inbox, and for those that have attachment, it will create a folder for every sender and create date subfolder with attachments in it.

#Parameters
$strUserView = '$Inbox'  
$DomServer = "SERVER/LN" 
$DomDBPath = "mail\user.nsf" 
$pwd4NotesDB = "Passw0rd" 
$ipPath2Export = "C:\Temp"

$DomSession = New-Object -ComObject Lotus.NotesSession #Use LN COM class
$DomSession.Initialize($pwd4NotesDB) #This is when Lotus asks for your password when you open it
$DomDatabase = $DomSession.GetDatabase($DomServer,$DomDBPath) #Initialize Database
$DomView  = $DomDatabase.GetView($strUserView) #Initialize View

Since the script has to go through all the mail, we need the loop.

$Counterf = $DomView.GetFirstDocument() #Define first document in view
While ($CounterF -ne $nul) {

$DomNexDocument = $DomView.GetNextDocument($CounterF) #Define Next doc

#Define some fields
$DomeLoopSubject = $CounterF.GetItemValue("Subject") #GetSubject
$DomeLoopFrom = $CounterF.GetItemValue("From") #GetFrom
$DomeLoopDate = $Counterf.GetItemValue("DeliveredDate")
$DomeLoopDate = '{0:yyyyMMdd}' -f $DomeLoopDate #GetDateandFormat

#for folder name we need to clean FROM
$DomeLoopFrom=$DomeLoopFrom.Replace("CN=","")
$DomeLoopFrom=$DomeLoopFrom.Replace("/O=LN","")
$DomeLoopFrom=$DomeLoopFrom.Replace("<","")
$DomeLoopFrom=$DomeLoopFrom.Replace(">","")
#$DomeLoopFrom=$DomeLoopFrom.Replace("@","")
$DomeLoopFrom=$DomeLoopFrom.Replace("\","")
$DomeLoopFrom=$DomeLoopFrom.Replace("/","")
$DomeLoopFrom=$DomeLoopFrom.Replace("`"","")

IF ($Counterf.HasEmbedded -eq "True") #IF attachment exists
    {
      $AttachItem = $Counterf.GetFirstItem("Body") #Get all attachments     
        
       Foreach($A in $attachItem.EmbeddedObjects)
        {
            $DomAttachSavePath = "$ipPath2Export"
            $DOmAttachSavePath = $DOmAttachSavePath+$DomeLoopFrom 
            $FILENAME=$A.Name
            $DomAttachSavePath=$DomAttachSavePath+"\"+$DomeLoopDate   
            Write-Output "Possible path  $DomAttachSavePath"
            New-Item -ItemType Directory -Force -Path $DomAttachSavePath >$null
            $Extractto = $DomAttachSavePath+"\"+$FILENAME       
            $A.EXTRACTFILE($Extractto) 
      }

    }
$CounterF = $DomNexDocument #Raise counter
}

So, this is how I managed to export all attachments from my Inbox in Lotus Notes.

Good Luck

6 thoughts on “Powershell and Lotus Notes

  1. This is fantastic.. I have a few notes databases to migrate over to share point, and this has really jump started me. I’m quite to Power shell but proficient in Lotus script. Fooling around with variations of this script right now!

    This morning I had no idea you could connect to Domino via PS.

  2. Hello, I am also quite clever with Lotus script.. Had no idea you could connect to notes with PS. I’m struggling with handling the file attachments. IN my case, rather than using a documentset, I just want to store the attachments from the Notes document into the List Item. struggling to come up with any idea. Can you push me in a direction? Can do this on the fly as I iterate through the Notes docs, or do i have to save the files and upload from there?

    • Hi James, this is all I could get from using PS with LS. I honestly dont know how to store attachment into Sharepoint ListItem. I would go with save files to temp, then upload them and in the end delete them from temp, if no longer needed.

  3. I figured it out. It wasn’t quite straightforward as I assumed I’d be able to transfer one embedded object to an attached object in the listitem but it didn’t quite work that way, or I haven’t found the magic at least….
    So what I did..
    – first I add-PNPlistitem using the metadata from the domino record
    -next I detatch all embedded objects to my local file system, since i’m running this migration from a desktop
    $NEWFILENAME = $DomAttachSavePath+”\”+$DomeLoopMigrationUnid+”\”+$NEWFILENAME
    I’m storing each document set in a separate subdirectory using the domino unid
    -I also created an array with all the file names.
    $MyArray.Add($NEWFILENAME) > $null
    $Att=@($MyArray)
    -then I found a function from another site to attach files
    Lastly, I called these functions: (found on another thread but it was public)

    writeItem -attachments $Att
    function writeItem(
    $attachments
    ) {
    # check if file exists first
    #$items=Add-PnPListItem -List “PRC Documents”
    $items=$AddedItem

    for ($a=0; $a -lt $attachments.length; $a++) {
    #Write-host ” ” $attachments[$a]
    write-host $attachments[$a]
    writeAttachment -item $items -fileWithPath $attachments[$a]

    }

    }

    function writeAttachment($item, $fileWithPath)
    {

    $ctx=Get-PnPContext
    $memoryStream = New-Object IO.FileStream($fileWithPath,[System.IO.FileMode]::Open)
    $fileName = Split-Path $fileWithPath -Leaf
    $attachInfo = New-Object -TypeName Microsoft.SharePoint.Client.AttachmentCreationInformation
    $attachInfo.FileName = $fileName
    $attachInfo.ContentStream = $memoryStream
    $attFile = $item.attachmentFiles.add($attachInfo)
    $ctx.load($attFile)
    $ctx.ExecuteQuery()
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.