Task Scheduler: Unterschied zwischen den Versionen

Aus Wiki-WebPerfect
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „== 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 af…“)
 
 
(Eine dazwischenliegende Version des gleichen Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
 
== Create a Task in Windows Task Scheduler ==
 
== 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.
+
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.
 
<source lang="powershell">
 
<source lang="powershell">
 
$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}"'
+
$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