none
Start-Process does nothing in event action RRS feed

  • Question

  • I am trying to create a hot watch folder that performs certain actions. It works except for when I add a "start-process" into the action. Is there a way to utilize the "start-process" from within an event action?

    #Script watches a folder for PDFs and then moves the PDF to another location



    #Define locations for where files are copied and where the log file is stored
        $destinationfolder="\\filesystem\dest"
        $logfilelocation="c:\Powershell scripts\Eye Camera Automation\Log\specialist.txt"

    ### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
        $watcher = New-Object System.IO.FileSystemWatcher
        $watcher.Path = "\\filesystem\hotfolder"
        $watcher.Filter = "*.pdf"
        $watcher.IncludeSubdirectories = $false
        $watcher.EnableRaisingEvents = $true  

    ### DEFINE ACTIONS AFTER A EVENT IS DETECTED
        $action = { $path = $Event.SourceEventArgs.FullPath
                    $filename=$event.sourceeventargs.name       
                    $changeType = $Event.SourceEventArgs.ChangeType
                    #$logline = "$(Get-Date), $changeType, $path"
                    $logline = "$(get-date -Format "M/d/yyyy H:mm:ss:ff"), $filename"
                    $argumentlist="-pdf " + '"'+ $path +'"' + ' -AI "C:\Powershell scripts\Signature\stamp.jpg" -P7 -MLR200 -MTB-209'
                    Start-Process -FilePath C:\Powershell scripts\PDFStamp\pdfstamp.exe -ArgumentList "$argumentlist" -Verb runas -passthru
                    Move-Item $path $destinationfolder
                    Add-content $logfilelocation -value $logline
                  }    
    ### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY  
        $created = Register-ObjectEvent $watcher "Created" -Action $action
        while ($true) {sleep 5}


    • Edited by dp4205 Thursday, September 15, 2016 6:28 PM
    Thursday, September 15, 2016 6:27 PM

Answers

  • You need to add -NoNewWindow to the line.

    Start-Process -FilePath 'C:\Powershell scripts\PDFStamp\pdfstamp.exe' -ArgumentList $argumentlist -NoNewWindow


    \_(ツ)_/

    • Marked as answer by dp4205 Thursday, September 15, 2016 8:27 PM
    Thursday, September 15, 2016 8:14 PM

All replies

  • Don't put quotes around things that don't need them:

    $argumentlist = @(
    	'-pdf',
    	$path,
    	'-AI',
    'C:\Powershell scripts\Signature\stamp.jpg', '-P7 -MLR200 -MTB-209' ) Start-Process -FilePath 'C:\Powershell scripts\PDFStamp\pdfstamp.exe' -ArgumentList $argumentlist -NoNewWindow
    You can't easily use RunAs in an Event.


    \_(ツ)_/





    • Edited by jrv Thursday, September 15, 2016 8:15 PM
    Thursday, September 15, 2016 6:41 PM
  • I tried replacing my code with yours and it didn't like it until I put quotes around $argumentlist; however it still failed to run the actions when the event was triggered. I also tried to remove the RunAs command in what I had originally posted with the same results. 

    When I was initially writing this, I couldn't get the start-process to work unless I used the RunAs, I couldn't figure out why. 

    Thursday, September 15, 2016 6:55 PM
  • You are kind of lost in fixes.

    When you say it doesn't work. What does that mean?

    Do not run this in the ISE. Open a CLI prompt to test.


    \_(ツ)_/

    Thursday, September 15, 2016 7:01 PM
  • I tried to run it from the CLI, same results. 

    The script should place a stamp on the PDF and move the file to another directory, it does neither. If I run the commands in powershell to place the stamp outside the event action it works correctly. If I comment out the stamping process (start-process) the move file works correctly. So the script runs and stays running, as it should, but does nothing.

    Thursday, September 15, 2016 8:12 PM
  • You need to add -NoNewWindow to the line.

    Start-Process -FilePath 'C:\Powershell scripts\PDFStamp\pdfstamp.exe' -ArgumentList $argumentlist -NoNewWindow


    \_(ツ)_/

    • Marked as answer by dp4205 Thursday, September 15, 2016 8:27 PM
    Thursday, September 15, 2016 8:14 PM
  • jrv,

    Thank you for all your help and quick responses. I had made a few other typos in moving the code to the server, but it appears the solution -NoNewWindow was key. 

    Thanks again! Really appreciate it!

    Thursday, September 15, 2016 8:28 PM
  • Good.  Sorry I forgot that in the fist example.

    \_(ツ)_/

    Thursday, September 15, 2016 8:30 PM