Powershell and Lotus Notes pt2

To continue my journey (PS and LN pt1) in PS scripting on Lotus Notes, I wanted to see if it is possible to manipulate groups members in LN. Well, it is possible, and I will show you how I did it.

As you remeber from my last post, 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

We are going to try this on a test group called #LN_TEST_GRP and the user we are going to add/remove from this group is CN=User TEST/O=LNDS

#Parameters
$strUserView = '$VIMGroups'  #System name for groups view in names.nsf
$DomServer = "SERVER/LN" 
$DomDBPath = "names.nsf" #mailbox that contains users, groups.. etc..
$pwd4NotesDB = "Passw0rd" 

$User = "CN=User TEST/O=LNDS"
$LNGroup = "#LN_TEST_GRP"
$Insert = 0 #1 for insert user
$Delete = 0 #1 for delete user
$Array = "" #Set Array to empty

#This part is for testing insert/delete - manually set values
$delete=0
$Insert=0
#

$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
$DomGroupView  = $DomDatabase.GetView($strGroupView) #Initialize View

$DomGrp = $DomGroupView.GetDocumentByKey("$LNGroup") #Get group from Group List
#Now we save members to Array and prepare MaxArry to handle For loop
$userGrp = $DomGrp.GetFirstItem("members") #For text Append
$Array = @($userGrp.Values) #GetValues and save to array
$MaxArray = $Array | measure -Maximum #GetMaxOf Array
$MaxArray = $MaxArray.Count #GetMaxOf Array

Now, when we got this sorted, the next step is manipulating group members. First we will add the user, then we are going to remove it from group.

The process of insert is very straightforward, If user is not on the member list, add the user to the end of that same list.

#INSERT USER INTO GROUP DOCUMENT
IF ($Insert -eq 1) # IF INSERT IS 1 
{
        IF (($userGrp.values -contains $User))
            {
                "******************User Exists - EXIT****************"
            }
        else
            {
                "******************User NotExist - INS****************"
                IF (($User -ne $nul) -and ($LNGroup -ne $nul))
                    {
                    $userGrp.AppendToTextList($User) #Add user to end of members list
                    $DomGrp.Save('False','True') #Save Group
                    }
            }
}

The process of deleting user from members list is little more complicated than insert. We input members to array, find user that needs to be deleted and replace the user with “”, then we cleanup the array, and finally we replace members value in LN group document with that new array.

#REMOVE USER FROM GROUP DOCUMENT
IF ($Delete-eq 1) # IF DELETE IS 1 
{    
        
        IF (($userGrp.values -contains $User))
            {
                "*******************User exists - DEL****************"
               IF (($User -ne $nul) -and ($LNGroup -ne $nul))
                    {

                            for ($i=0; $i -lt $MaxArray; $i++) #FOR Loop
                            {
                                IF ($array[$i] -eq $User) #IF user is in the list
                                {
                                $array[$i] = "" #Set "" for that user
                                $Array = $Array | Where { -not [string]::IsNullOrWhiteSpace($_) }     #Clean Array of blank spaces
                                }
                            }
                            #$Array
                            $Array=$Array.Trim() #Array Trim
                            $DomGrp.ReplaceItemValue("Members",$Array) #Replace Members list with our new array without the specific user
                            $DomGrp.Save('False','True') #Save Group document
                    } 
            }
        else
            {
                
                   "*******************User NotExists - EXIT****************"
   
            }
  }

So, this should cover the basics in adding and removing users from Lotus Notes.

Good Luck