Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell如何自动重新启动服务?_Powershell_Service_Restart - Fatal编程技术网

PowerShell如何自动重新启动服务?

PowerShell如何自动重新启动服务?,powershell,service,restart,Powershell,Service,Restart,当cpu使用率达到80%时,我尝试重新启动特定的服务。 我正在使用作业触发器并尝试获取日志,但无法重新启动服务 $peridiocallyChecks = { # Check if logging source is available, if not add it if (!(Get-EventLog -Source "CPU supervision" -LogNameApplication)){ New-EventLog -LogName Applic

当cpu使用率达到80%时,我尝试重新启动特定的服务。 我正在使用作业触发器并尝试获取日志,但无法重新启动服务

$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

Write EventLog
正在写入事件日志。我将在与计划任务一起运行的
$peridiocallyChecks
块之外运行创建日志源的代码。您需要管理员权限来创建日志源,可能发生的情况是,它在计划任务期间失败,并且您没有获得任何日志记录/脚本块的其余部分没有运行

因此,第一步,让日志记录在计划任务之外工作,并在事件查看器中构建一个自定义视图,以便您可以在交互式提示中执行
Write EventLog
,刷新视图并查看日志消息

在您可以查看日志而不必考虑日程任务的复杂性之后,开始通过脚本查找哪个区域没有运行。根据需要添加更多的
Write EventLog
调用,以缩小调用内容和未调用内容的范围

Get EventLog-Source“CPU监控”-LogName应用程序是冗余的。这将向您显示事件日志消息,但在计划任务中运行时,您不会看到任何内容

我还利用
Get Counter
的采样功能计算几秒钟内的平均值。通过这种方式,您可以更好地了解实际负载,因为即使系统受到压力,单个观察值也可能低于您预期的值

$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

您/脚本是否具有停止和启动服务的权限?您需要在计划作业中使用任何凭据吗?是的,我有权限和凭据。我使用从管理员帐户,但它仍然不工作@I.tDelinkunt可以停止服务吗?您的自定义消息是否写入事件日志?该服务正在工作,并且由于该服务,CPU使用率大部分时间达到95%。我想在服务的cpu使用率达到80%时自动重启。我也到处查看日志,但找不到日志@I.T有说服力