PowerShell How can I restart service automatically? PowerShell How can I restart service automatically? powershell powershell

PowerShell How can I restart service automatically?


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