'PowerShell Watcher fails to execute .bat and .exe processes that work independently in PowerShell instances

I'm working on a Powershell script that will monitor a folder (using a Watcher) and enact the following workflows:

1.) If the file is a .zpl (Zebra thermal printer shipping label), run a simple batch file to send the .zpl to the thermal printer (using these same instructions here)

2.) If the file is a .pdf, run PDFtoPrinter to send the PDF to the default office printer

These two functions I can get to work perfectly fine on their own. I've been using the label.bat batch file to print labels for years now. I've confirmed that I'm able to run PDFtoPrinter with a PDF argument from a Powershell instance without any problems.

I'm implementing this FileSystemWatcher script to handle the monitoring of a folder that is set up to automatically receive .zpl and .pdf attachments. I've changed the relevant section for the action for the handler to the following:

Write-Host "Found file $Name $FullPath"
if($Name -like '*.zpl'){
    Write-Host "Found .zpl file, printing on thermal printer"
    Start-Process $label -verb RunAs -ArgumentList $FullPath
    Move-Item -Path $FullPath -Destination 'C:\Users\User\Documents\Processed Files\'
}
elseif($Name -like '*.pdf'){
    Write-Host "Found .pdf file, printing on default printer"
    Start-Process $PDFtoPrinter -verb RunAs -ArgumentList $FullPath
    Move-Item -Path $FullPath -Destination 'C:\Users\User\Documents\Processed Files\'
    Stop-Process -Path $PDFtoPrinter
    }
else{
}

Where $label is the full path to the label.bat and $PDFtoPrinter is the full path to PDFtoPrinter.exe. Again, I've confirmed that running the statements

Start-Process $label -verb RunAs -ArgumentList $FullPath

and

Start-Process $PDFtoPrinter -verb RunAs -ArgumentList $FullPath

in an elevated instance of Powershell both work correctly (when pre-seeded with the correct $FullPath, of course).

When the Watcher script is running (again, in an elevated instance for now) and I drop in .zpl or .pdf files, I can see the Write-Host lines that correctly identify the files coming through. The .zpl files even move into the correct Processed Files directory (the .pdfs just stick around) but neither of the printing processes trigger correctly.

I've tried to force everything to run with elevated permissions and everyone runs just fine on their own but as soon as I put everything together in this Watcher, nothing decides to work correctly. I've also tried adding Wait commands to no avail.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source