Task Scheduler: Unterschied zwischen den Versionen
Aus Wiki-WebPerfect
Admin (Diskussion | Beiträge) |
Admin (Diskussion | Beiträge) |
||
Zeile 4: | Zeile 4: | ||
$TaskName = "Splunk Workaround" | $TaskName = "Splunk Workaround" | ||
$TaskDescription = "Restart Splunk Service if splunk-wineventlog.exe is not running." | $TaskDescription = "Restart Splunk Service if splunk-wineventlog.exe is not running." | ||
− | $TaskAction = New-ScheduledTaskAction -Execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument '-NoProfile -WindowStyle Hidden - | + | $Command = "& {If (!(Get-Process splunk-winevtlog)) {Restart-Service SplunkForwarder}}" |
+ | $TaskAction = New-ScheduledTaskAction -Execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument '-NoProfile -WindowStyle Hidden -Command `"$Command`"" | ||
$TaskTriggers = (New-ScheduledTaskTrigger -AtStartup -RandomDelay (New-TimeSpan -Minutes 5)), (New-ScheduledTaskTrigger -Daily -At '5:00 AM') | $TaskTriggers = (New-ScheduledTaskTrigger -AtStartup -RandomDelay (New-TimeSpan -Minutes 5)), (New-ScheduledTaskTrigger -Daily -At '5:00 AM') | ||
$TaskSettings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 5) -RestartCount 3 -RestartInterval (New-TimeSpan -Minutes 1) | $TaskSettings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 5) -RestartCount 3 -RestartInterval (New-TimeSpan -Minutes 1) |
Aktuelle Version vom 27. Oktober 2021, 08:56 Uhr
Create a Task in Windows Task Scheduler
Because of an Splunk Universal Forwarder issue, we had to create a task to check if the subprocess are running after a reboot. If the subprocess is not running, restart the service.
$TaskName = "Splunk Workaround" $TaskDescription = "Restart Splunk Service if splunk-wineventlog.exe is not running." $Command = "& {If (!(Get-Process splunk-winevtlog)) {Restart-Service SplunkForwarder}}" $TaskAction = New-ScheduledTaskAction -Execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument '-NoProfile -WindowStyle Hidden -Command `"$Command`"" $TaskTriggers = (New-ScheduledTaskTrigger -AtStartup -RandomDelay (New-TimeSpan -Minutes 5)), (New-ScheduledTaskTrigger -Daily -At '5:00 AM') $TaskSettings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 5) -RestartCount 3 -RestartInterval (New-TimeSpan -Minutes 1) Register-ScheduledTask -TaskName $TaskName -Description $TaskDescription -Action $TaskAction -Trigger $TaskTriggers -Settings $TaskSettings -User "System" -RunLevel Highest -Force