Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create Event based task to run a custom powershell script on Windows Event
# Create a task to run powershell script on event
$cred = Get-Credential
$password = $cred.GetNetworkCredential().Password
$taskName = "EventMonitoringService"
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-NoProfile -WindowStyle Hidden "C:\CourtAlert\Scripts\TakeAction.ps1""'
#Remove existing tasks
$taskExists = Get-ScheduledTask | Where-Object {$_.TaskName -like $taskName }
if($taskExists) {
write-host "Unregistering $($taskExists)"
Unregister-ScheduledTask -TaskName $taskExists.TaskName -Confirm:$false
}
SCHTASKS /Create /TN $taskName /TR $action /SC ONEVENT /RL Highest /RU $cred.Username /RP $password /EC Application /MO "*[Application]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.