Skip to content

Instantly share code, notes, and snippets.

@XPlantefeve
Last active February 3, 2019 19:43
Show Gist options
  • Save XPlantefeve/065a35329de55792f529cf56c507347e to your computer and use it in GitHub Desktop.
Save XPlantefeve/065a35329de55792f529cf56c507347e to your computer and use it in GitHub Desktop.
# The following is an example, it's not a function, it's not meant to be run
# as is, I just wrote it to show how creating a scheduled tak on an event
# can be done in PowerShell. I wrote a small post about how it came to be, that
# can be read at https://xavier.plantefeve.fr/posts/SchdTskOnEvent
$class = Get-cimclass -ClassName MSFT_TaskEventTrigger -Namespace root/Microsoft/Windows/TaskScheduler
$trigger = $class | New-CimInstance -ClientOnly
$trigger.Enabled = $true
$trigger.Subscription = '<QueryList><Query Id="0" Path="Microsoft-Windows-NetworkProfile/Operational"><Select `
Path="Microsoft-Windows-NetworkProfile/Operational">*[System[Provider[@Name=''Microsoft-Windows-NetworkProfile''] `
and EventID=10000]]</Select></Query></QueryList>'
$ActionParameters = @{
Execute = 'C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe'
Argument = '-NoProfile -File C:\scripts\NetworkConnectionCheck.ps1'
}
$Action = New-ScheduledTaskAction @ActionParameters
$Principal = New-ScheduledTaskPrincipal -UserId 'NT AUTHORITY\SYSTEM' -LogonType ServiceAccount
$Settings = New-ScheduledTaskSettingsSet
$RegSchTaskParameters = @{
TaskName = 'Network checks'
Description = 'runs at network connection'
TaskPath = '\Event Viewer Tasks\'
Action = $Action
Principal = $Principal
Settings = $Settings
Trigger = $Trigger
}
Register-ScheduledTask @RegSchTaskParameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment