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
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.
Great 🙂 glad I could be of some help 🙂
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.
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()
}
Wow James, this is great! thanks for sharing!
🙂
I’m very limit to powershell and lotus script. I’m new in lotus notes. I’ve bee tasked to migrate lotus notes data, including attachment to sharepoint. Can you please help me to get the Powershell code?I’m running lotus notes domino 12.0 version. Do I need to run powershell 32bit even if domino is 64bit.
I’m ready to compensate for getting for getting the code? Thank you.
hi Jose,
you can always try what James below wrote. I haven’t had the time to play with LN and Sharepoint.
Thank you will do.
Luka Gros,
I was able to run your script for email database template and it works GREAT!! Excellent script!!
I’ve other types of .NSF databases which I believe use a different type of template, not email. These databases holds records or items and attachment files as well. When I run the script, it acknowledge there is attachment on the If statement (IF ($DomDoc.HasEmbedded -eq “True”){), but when it reached the Foreach($A in $attachItem.EmbeddedObjects){ it bypassed it and don’t write anything. It seems I’m having problems with attachments. I used different Views and still cannot pass Foreach($A in $attachItem.EmbeddedObjects){. Anything you can help, I’d appreciate it.
thank you.
When you write the output for $A in foreach loop? And then try $A | Get-Member
to see what properties or methods the $A has