1

I have this powershell script that trigger some instruction when a file is created in a certain directory. All works well, but I'm not able to close the powershell after that the action is done. How can I do that?

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\Users\Autostrade\CartellaSFTP"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true  
$uscita = 0

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = { $path = $Event.SourceEventArgs.FullPath
            $changeType = $Event.SourceEventArgs.ChangeType
            $logline = "$(Get-Date), $changeType, $path"
            Add-content "C:\Users\Autostrade\PHP Script Dati Telepass\log.txt" -value $logline
            # Set up references to executable and script
            $PhpExe  = "C:\PHP\php.exe"
            $PhpFile = "C:\Users\Autostrade\PHP Script Dati Telepass\script.php"

            # Create arguments from Script location
            # usually php.exe is invoked from console like: 
            # php.exe -f "C:\path\myscript.php"
            #$PhpArgs = '-f "{0}"' -f $PhpFile

            # Invoke, using the call operator
            $PhpOutput = & $PhpExe $PhpFile
            PhpOutput
            $uscita = 1
          }    
### DECIDE WHICH EVENTS SHOULD BE WATCHED 
Register-ObjectEvent $watcher "Created" -Action $action
#Register-ObjectEvent $watcher "Changed" -Action $action
#Register-ObjectEvent $watcher "Deleted" -Action $action
#Register-ObjectEvent $watcher "Renamed" -Action $action
while (!$uscita) {sleep 5}
exit

I tried using that "$uscita" variable to stop the while and exit, but it's not working. I tried also to put at the end of the action the exit command, but that is not working too

8
  • 1
    $uscita = 1 -> $using:uscita = 1 Commented Apr 11, 2017 at 15:27
  • I tried.. The powershell give some errors and immediatly close.. I'm not able to read what is the error Commented Apr 11, 2017 at 15:32
  • Remove the exit and run the script from a PowerShell prompt. Commented Apr 11, 2017 at 15:59
  • It gives me this exception: CategoryInfo: ParseError (:) [], ParseException FullyQualifiedErrorId: InvalidLeftHandSide Commented Apr 12, 2017 at 7:55
  • Please edit your question and show the complete error. Don't post a screenshot; copy/paste it as text. Commented Apr 12, 2017 at 8:04

2 Answers 2

3

I edited the last part of my code in this way and now it works. So after the action, the script ends itself. Here's the code:

while (!$uscita) {
if($? -eq $false) {
    exit
    }
sleep 5}
Sign up to request clarification or add additional context in comments.

Comments

0

Offhand it looks like your 2nd to last line with the while is probably returning true, so it just sits in the sleep command.

Try this to test, it will return true if the last command completed successfully.

while (!$uscita) {write-output $?; sleep 5}

3 Comments

Ok, I tried. When I insert a file in he directory that I'm monitoring, it gives me "false".. But the loop dpesn't stop. It continues and returns true
That makes sense, we're getting closer. $? will tell you (true/false) if the previous command succeeded and it did. Now I'm wondering: you said "it give me 'false'", do you mean that the statement !$uscita returns $true? (meaning $uscita -eq $false).
I'm not sure the answer you posted is the best way to do it. Instead of setting $uscita = 1 consider setting $uscita = $true. Then to troubleshoot use - while (!$uscita) {write-output $uscita; sleep 5}. If this still loops infinitely it means that the $uscita = $true line isn't getting hit. Start putting other write-outputs in the $action block or higher up so that you can see where the issue is. Don't forget to troubleshoot the function PhpOutput. Unexpected behavior there could prevent $uscita from being set.

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.