Skip to content

Instantly share code, notes, and snippets.

@vendettamit
Last active January 13, 2017 22:09
Show Gist options
  • Save vendettamit/c7a011e935634a3a6e7574893ce2e322 to your computer and use it in GitHub Desktop.
Save vendettamit/c7a011e935634a3a6e7574893ce2e322 to your computer and use it in GitHub Desktop.
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