0

I am trying to call an outside ps script to run every time files are created. The monitoring and logging of files work well, but the Invoke-Expression of running the external script only runs one time even if more files are created. How do I run the external script with every time a new file is created.

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "c:\mypath"
    $watcher.Filter = "*.txt*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action = { $path = $Event.SourceEventArgs.FullPath                
                $changeType = $Event.SourceEventArgs.ChangeType
                $logline = "$(Get-Date), $changeType, $path"
                Add-content "C:\mypath\log.txt" -value $logline
                Invoke-Expression (start powershell ("C:\MyOtherScript.ps1")) ###### This only runs one time even if file is changes and logged
              }

### DECIDE WHICH EVENTS SHOULD BE WATCHED

    Register-ObjectEvent $watcher "Created" -Action $action
    while ($true) {sleep 5}

EDIT: This got it working incase anyone find themselves here looking to solve the porblem

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "c:\mypath"
    $watcher.Filter = "*.txt*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action = { $path = $Event.SourceEventArgs.FullPath                
                $changeType = $Event.SourceEventArgs.ChangeType
                $logline = "$(Get-Date), $changeType, $path"
                Add-content "C:\mypath\log.txt" -value $logline
                Start-Process powershell -argument "C:\MyOtherScript.ps1"
              }

### DECIDE WHICH EVENTS SHOULD BE WATCHED

    Register-ObjectEvent $watcher "Created" -Action $action
    #while ($true) {sleep 5}
5
  • What's the purpose of your loop at the end? Also, why are you using Invoke-Expression instead of just Start-Process Commented Nov 15, 2017 at 18:49
  • The loop at the end was in to original code to monitor files her superuser.com/questions/226828/… Commented Nov 15, 2017 at 19:00
  • In searching for how to do this I found 'Invoke-expression' or 'invoke-item' were the methods of running other scripts. I am new to powershell. Commented Nov 15, 2017 at 19:01
  • I was able to get it working with using Start-Process powershell -argument "C:\MyOtherScript.ps1" and commenting out the while ($true) {sleep 5} Commented Nov 15, 2017 at 19:56
  • Great, I'll contribute that as the answer then. Commented Nov 15, 2017 at 20:36

1 Answer 1

1

Invoke-Expression is not recommend over using & due to security concerns. In your example, however, I'd suggest Start-Process:

Start-Process -FilePath 'powershell' -ArgumentList @('-File','"C:\MyOtherScript.ps1"')
Sign up to request clarification or add additional context in comments.

Comments

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.