I have multiple powershell scripts that have to get settings from a MS Access database. I do this inline with this code:
$objConnection = New-Object -combody ADODB.Connection
$objRecordset = New-Object -combody ADODB.Recordset
$objConnection.Open($myDatabase)
$objRecordset.Open($MyQuery, $objConnection , $adOpenStatic, $adLockOptimistic)
# do stuff
$objRecordset.Close()
$objConnection.Close()
The first and the last part is always the same, so for brevity and ease of maintaining I want to put the first in a function:
function GetSettings(myQuery) {
$objConn = New-Object -combody ADODB.Connection
$objRec = New-Object -combody ADODB.Recordset
$objConn.Open($myDatabase)
$objRec.Open($myQuery, $objConn, $adOpenStatic, $adLockOptimistic)
return $objRec
}
And then just call it:
$objRecordset = GetSettings($MyQuery)
But this does not close the recordset or the connection in the function. Is this a problem or will they get closed automatically after exiting the function or even exiting the powershell script? This will be running multiple times per day, I don’t want to risk filling up memory or causing other issues.
I’ve tried assigning $objRec to some other variable $myReturnRec = $objRec
and returning that, but that does not work, $myReturnRec remains null after the assignment.
6