PowerShell How can I restart service automatically?

Viewed 772

I am trying to restart specific service when cpu usage come to 80%. I am using job trigger and try to get logs but I could not restart to service.

$peridiocallyChecks = {
    # Check if logging source is available, if not add it
    if (!(Get-EventLog -Source "CPU supervision" -LogNameApplication)){    
        New-EventLog -LogName Application -Source "CPU supervision" 
    }

    #Getting CPU usage e.g 45.3434242423
    $cpuUsage = [double] (Get-Counter '\Processor(_Total)\% Processor Time' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty CounterSamples | Select-Object -ExpandProperty CookedValue)

    if ($cpuUsage -gt 80) {
       Write-EventLog -LogName "Application" -Source "CPU supervision" -EntryType Information -EventId 1 -Message "CPU usage is $cpuUsage. Going to stop service"
        Stop-Service "service name"
        # Some cooldown time for the service to shutdown
        Start-Sleep -Seconds 10
        Start-Service "service name"
        Write-EventLog -LogName "Application" -Source "CPU supervision" -EntryType Information -EventId 1 -Message "Restarted service"
    }
    Get-EventLog -Source "CPU supervision" -LogName Application
}

# Trigger every hour

$trigger = New-JobTrigger -Once -At "17/05/2019 0am" -RepetitionInterval (New-TimeSpan -Hour 12) -RepetitionDuration ([TimeSpan]::MaxValue)

Register-ScheduledJob -Name "CPUCheck23" -Trigger $trigger -Scriptblock $peridiocallyChecks
1 Answers

Write-EventLogis writing to the Windows Event log. I would run the code that creates the log source outside of the $peridiocallyChecks block that is run with the scheduled task. You need admin rights to create the log source, and what could be happening is that it fails during the scheduled task, and you are not getting any logging/the rest of the script block does not run.

So first step, get that logging working outside of a scheduled task, and build a custom view in the Event Viewer so that you can execute Write-EventLog in a interactive prompt, refresh your view and see your log message.

After you can review the logs without the complexity of the schedule task on top of it, start working through the script to find what area is not running. Add more Write-EventLog calls as needed to narrow down what is called and what is not.

the Get-EventLog -Source "CPU supervision" -LogName Application is redundant. This will show you the event log messages, but when run in a scheduled task you are not going to see anything.

I also make use of the sampling functionality of Get-Counter to calculate the mean value over a few seconds. This way gives you a better idea of the actual load, because a single observation could be lower than you would expect even if the system is stressed.

$i = 0
$runTotal = 0.0;
$counters = Get-Counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 3 -MaxSamples 5 | Select-Object -ExpandProperty CounterSamples | Select-Object -ExpandProperty CookedValue 
$counters | foreach { $runTotal = $_ + $runTotal; $i ++} 

#Raw Counter
$counters

# Mean value over 3 seconds will give you a better estimate 
$runTotal/ $i
Related