2

I was trying to execute powershell in parallel mode. I was advised to create an event which will trigger whenever there is a change in folder.

For ex: If a folder name called "test1" is created in a particular folder then my powershell script test1.ps1 should be triggered.

I was trying to get example for this with Register-Objevent , but i am not getting any good clue.

If you have implemented some thing like this , can you please guide me?


Updated:

When i create a file named test1.ps1 in the "D:\ParallelEvent\temp" folder, it should execute test1.ps1 powershell file. Following is the code for that

 $ScriptLoc="D:\My_Scripts"
    $watcher = New-Object System.IO.FileSystemWatcher -Property @{Path = 'D:\ParallelEvent\temp';Filter = '*.txt';NotifyFilter = [System.IO.NotifyFilters]'FileName,LastWrite'}
    $CreatedAction =
    {
    $FilenamewithoutExtn=$(($event.sourceEventArgs.Name).Split('.')[0])
    Start-Job $ScriptLoc\$FilenamewithoutExtn.ps1
    Get-Job -State Running | Wait-Job
    } 

Register-ObjectEvent -InputObject $watcher -EventName Created -SourceIdentifier FileCreated -Action $CreatedAction

My test1.ps1 has following line

[system.windows.forms.messagebox]::show("Hello, Test1!")

But still it doesn't execute the test1.ps1 it seems. I did not get any messagebox. Am i missing any thing?

1 Answer 1

1

you are not getting messagebox because powershell jobs run as background process and does not show ui.

if you modify the test1.ps1 to a file redirection

"test " > C:\AnywhereYouWantToRedirectData\testagain.txt

you will be able to see that the test1.ps1 runs and redirected file gets created (testagain.txt).

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.