I have created a PowerShell script containing the FileSystemWatcher which creates several file change events. It works perfectly fine when running it within PowerShell ISE, but my end goal is to be able to run this as a background task that's not in PowerShell ISE.

I then started to look into wrapping the code within a GUI which I have done and it kind of works. When I run the code it creates a form containing 2 buttons, when you click button1 I want this to start the code and pressing button2 stops the code or have a handler so when I press a certain key the code stops.

What I'm seeing is when I press button1 the script starts and runs, but I cant cancel it without closing the console window that's behind. Also what I'm noticing, when running through PowerShell ISE I get an email notification of the event that has occurred, when this is running through the GUI I have created, it notices the files changing but I don't get an email notification.

Could anyone assist please?

  • Tony - You say "my end goal is to be able to run this as a background task that's not in PowerShell ISE" but then you start talking about some GUI wrapper and so forth. For your "end goal" I visualize a few different solutions for that but the other stuff just adds confusion. Would you be satisfied with ONLY a solution that gives you what you ask in the first paragraph or your question i.e. "my end goal is to be able to run this as a background task that's not in PowerShell ISE"?? Tag me back with the @ sign and my name in a separate comment to clarify. @Name Typing my message – Pimp Juice IT Jan 23 '17 at 3:44
  • @walmart If you can help me run it as a background task that would be great thank you. – Tony Jan 24 '17 at 9:18
  • An update on this, I can get the script to run in the background, but the email functionality doesn't work. I have created a function which creates the SystemWatchers and at the bottom I have a loop setup calling the function. If I call the function name the files will be watched, but no email is triggered if I run the same code without the function, the SystemFileWatcher creates the Watcher events and when an event is triggered I am emailed. If I can get the email function to work, I should be able to get the script to run in the background from a scheduled task. – Tony Jan 26 '17 at 15:36
  • Yes, it sounds like you're using what I was going to suggest already with Task Scheduler. If you edit your question, add your work that you have working with the PowerShell logic and the function logic, explain that this is really the problem you are having, you will probably have much more luck in getting an answer. There are some great PowerShell people on SU but I think the code, a little clarification, etc. will help you get an answer and probably much much quicker than how your question is posted now. Just edit your question above and add the clarification and code you need help with. – Pimp Juice IT Jan 26 '17 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.