AP Invoice Batch Posting Issue

BOI-Experts,

We are running into an interesting phenomenon. A BOI script creates AP and PO invoices in multiple companies, and then posts the AP and PO batches company by company. A lot of times that works just fine. Every now and then the invoices and the batch disappear, but don't show up in the history. Then at some point the missing invoices show up in the history files. I assume that happens when the next batch gets posted. 

Has anyone seen that behavior and has a way to fix it?

Here is the code I use to post the AP batches. It is in Powershell, but the methods are the same as in vbScript.

foreach ($sCompany in $arrListSageCompany) {
            
    $retVal = $oSS.nSetCompany($sCompany)
                
    # Post AP Invoices
    $retVal = $oSS.nSetDate("A/P","$sModuleDate")
    $retVal = $oSS.nSetModule("A/P")
    $retVal = $oSS.nSetProgram($oSS.nLookupTask("AP_InvoiceRegister_ui")) 
    $oAPInvReg = $oScript.NewObject("AP_InvoiceRegister_upd",  [ref]$oSS)
    # .nBatchEnabled to test if Batch Processing is activated
    $retVal = $oAPInvReg.nSelectBatch("XXXXX")
    if ($retVal -eq 1)
    {
        $retVal = $oAPInvReg.nPDFSilent = 1
        $retVal = $oAPInvReg.nSetPostingDate("$sModuleDate")
        $retVal = $oAPInvReg.nProcessReport("PRINT") #DEFERRED
        $retVal = $oAPInvReg.nEndOfPDFConverterJob()
        $retVal = $oAPInvReg.nUpdate()
    }

    try { $oAPInvReg.DropObject() } catch {}

    # Post PO Invoices
    $retVal = $oSS.nSetDate("P/O","$sModuleDate")
    $retVal = $oSS.nSetModule("P/O")
    $retVal = $oSS.nSetProgram($oSS.nLookupTask("PO_ReceiptRegister_ui"))
    $oPOInvReg = $oScript.NewObject("PO_ReceiptRegister_upd",  [ref]$oSS)
    # .nBatchEnabled to test if Batch Processing is activated
    $retVal = $oPOInvReg.nSelectBatch("XXXXX")
    if ($retVal -eq 1)
    {
        $retVal = $oPOInvReg.nSetPostingDate("$sModuleDate")
        $retVal = $oPOInvReg.nInitReportEngine()
        $oPOInvReg.nReportType = 6
        $sDateTime = Get-Date -format "yyyyMMdd_hhmmss"
        $sRegisterFileName = "$($sSageCompany)_PO_DailyReceiptReg_XXXXX_$($sDateTime).pdf"
        $sRegisterFilePath = "$($sRegisterDirectory)$($sRegisterFileName)"
        $retVal = $oPOInvReg.nSetExportOptions(5,$sRegisterFilePath)
        $retVal = $oPOInvReg.nProcessReport("EXPORT") #DEFERRED
        $retVal = $oPOInvReg.nUpdateInit()
        $retVal = $oPOInvReg.nUpdateMain()
        $retVal = $oPOInvReg.nUpdateCleanup()
    }

    try { $oPOInvReg.DropObject() } catch {}
}

Thanks for your help.

B.