Monday, 4 April 2016

Trigger a PowerShell script from Schedule Task

Hello Folks,

PowerShell gave us so many options and solutions that I could wonder I could write this blog from PowerShell command line. (yay!)
Lets deal with a simple requirement on how to execute a PowerShell script on a specific EventID that happened on OS, like Logon, Logoff , some app error etc,

Launch TaskScheduler (taskschd.msc)
Navigate to the Windows,(This is the place normally I would create my custom schedule jobs)

Right-Click on the empty space and "Create a new Task"

Then on the next Windows that pop's up.
General TAB :
Ensure you are using an appropriate user account.
Select "Run whether user is logged on or not"
and "Run with highest privileges"

Then select
Trigger TAB :
Click "New"
Select "Begin the task" drop down  "On an event"
Input the "Log or Source or Event ID"
Select "Enabled"

Then Click "OK"

Now click on the
Actions TAB:
Click on "New"
Under Actions drop down menu, Select "Start a Program" 
Then we need to updated the "Program/Script"  with the desired powershell script.
NOTE: It has to be like "  powershell -file "C:\somefolder\somescript.ps1"   " with any additional parameters that are needed.
You could get some idea from this URL for the parameters

Then click "OK" 

Do some test runs with some simple scripts , test thoroughly and then you could implement it.
Thats it!

Happy Scripting!!