0

I'm trying to create a generic connection (and disconnection) function for my Lotus Notes COM object scripts, so I don't need to repeat code.

To avoid using global variables I wanted to pass the reference to the "would-be" COM object variable, and utilize it, however I'm finding that it stays as null, despite connecting correctly inside the function.

I'm sure my understanding of how [REF] works in PoSh is what's causing this issue.

[EDIT] - I assume it's the New-Object call causing the variable within the function to no longer be referencing the input variable as it now requires a different address in memory? Is there a way to achieve this / a better practise ?

Below is a simplified snippet of my code:

function Connect-NotesSession {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        $obj,
        [Parameter(Mandatory=$false)]
        [System.Security.SecureString]$pw
    )

    try {
        Write-Host "Initializing Lotus Notes COM Object... " -NoNewline
        $obj = New-Object -ComObject Lotus.NotesSession
        if($pw) {
            $obj.Initialize([Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pw)))
        } else {
            $obj.Initialize()
        }
        Write-Host "Connected." -ForegroundColor Green
    } catch {
        Write-Host "Error! Failed to connect" -ForegroundColor Red
    }
}

## Main

$notes = $null
$p = Read-Host -AsSecureString

Connect-NotesSession [REF]$notes $p

$notes.GetType()

Output:

Initializing Lotus Notes COM Object... Connected. 

You cannot call a method on a null-valued expression.
At line:31 char:1
+ $notes.GetType() 
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Expected output:

PS C:\> $notes.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    __ComObject                              System.MarshalByRefObject

1 Answer 1

1

I'm not 100% familiar with the lotus notes stuff but it appears your NUlling out (1) the $Notes object before running the function. And then you're calling it in the function (2).

  1. $notes = $null

    $p = Read-Host -AsSecureString

  2. Connect-NotesSession [REF]$notes $p

    $notes.GetType()

Try moving the object initialization to just above the function call.

## Main
$notes = $null
$notes = New-Object -ComObject Lotus.NotesSession
$p = Read-Host -AsSecureString

Connect-NotesSession [REF]$notes $p

$notes.GetType()

And then remove that from the try statement of course. Also, your nulling may not be required cause when you create the object it overrides anything in that previous value.

Cheers!

Sign up to request clarification or add additional context in comments.

1 Comment

I was nulling out the variable as I wanted a blank slate and will be nulling it after disconnection for reuse later. What I didn't realise was that if it's NULL, there's nothing to reference, which is why it's not being used as expected. I was hoping not to have to declare the type of $notes before passing it in, but I assume that's the only way. Upvoted because it helps without being the specific answer I was hoping for. But will tick if there are no more appropriate answers :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.