2016-12-27 14 views
0

我试图在服务崩溃时运行perl脚本。 perl脚本打算重新启动服务并向所有开发者发送邮件。在Windows服务pannel中运行程序选项以进行故障恢复

我已经使用Windows恢复选项,它有一个选项运行程序。我已经在命令行选项中填写了所需的详细信息,但脚本似乎没有得到执行。你能帮我分享一下你的知识吗?

Recovery tab configuration

我试图与重新启动服务选项,并且工作正常,但运行的程序没有执行该脚本。我错过了什么吗? 对此有任何评论将有所帮助。

回答

0

我最近实现了一个恢复选项来运行powershell脚本,该脚本尝试重新启动该服务定义的次数,并在结束时发送电子邮件通知,它还将最近的相关日志附加到txt文件。

经过多次尝试之后(尽管我已经看到了所有其他的事情)在服务恢复选项卡上域的配置如下:

方案:Powershell.exe
**不C:\ WINDOWS \ System32 \ WindowsPowerShell \ v1.0 \ Powershell.exe

命令行参数:-command“& {SomePath \ YourScript.ps1'$ args [0]''$ args [1]''$ args [n] '}“

例如:-command”& {C:\ PowershellScripts \ ServicesRecovery.ps 1'Service Name'}“

** $ args是将传递给脚本的参数。这些不是必需的。

这里是PowerShell脚本:

cd $PSScriptRoot 

$n = $args[0] 

function CreateLogFile { 
$events = Get-EventLog -LogName Application -Source SomeSource -Newest 40 
if (!(Test-Path "c:\temp")) { 
    New-Item -Path "c:\temp" -Type directory} 
if (!(Test-Path "c:\temp\ServicesLogs.txt")) { 
    New-Item -Path "c:\temp" -Type File -Name "ServicesLogs.txt"} 
    $events | Out-File -width 600 c:\temp\ServicesLogs.txt 
} 

function SendEmail { 
$EmailServer = "SMTP Server" 
$ToAddress = "[email protected]" 
$FromAddress = "[email protected]" 

CreateLogFile 

$Retrycount = $Retrycount + 1 
send-mailmessage -SmtpServer $EmailServer -Priority High -To $ToAddress -From $FromAddress -Subject "$n Service failure" ` 
-Body "The $n service on server $env:COMPUTERNAME has stopped and was unable to be restarted after $Retrycount attempts." -Attachments c:\temp\ServicesLogs.txt 

Remove-Item "c:\temp\ServicesLogs.txt" 
} 

function SendEmailFail { 
$EmailServer = "SMTP Server" 
$ToAddress = "[email protected]" 
$FromAddress = "[email protected]" 

CreateLogFile 

$Retrycount = $Retrycount + 1 
send-mailmessage -SmtpServer $EmailServer -Priority High -To $ToAddress -From $FromAddress -Subject "$n Service Restarted" ` 
-Body "The $n service on server $env:COMPUTERNAME stopped and was successfully restarted after $Retrycount attempts. The relevant system logs are attached." -Attachments c:\temp\ServicesLogs.txt 

Remove-Item "c:\temp\ServicesLogs.txt" 
} 

function StartService { 

$Stoploop = $false 

do { 
    if ($Retrycount -gt 3){ 
    $Stoploop = $true 
    SendEmail 
    Break 
    } 

    $i = Get-WmiObject win32_service | ?{$_.Name -imatch $n} | select Name, State, StartMode 
    if ($i.State -ne "Running" -and $i.StartMode -ne "Disabled") { 

     sc.exe start $n 
     Start-Sleep -Seconds 35 

     $i = Get-WmiObject win32_service | ?{$_.Name -imatch $n} | select State 
      if ($i.state -eq "Running"){ 
       $Stoploop = $true 
       SendEmailFail} 
      else {$Retrycount = $Retrycount + 1} 
    }   
} 
While ($Stoploop -eq $false) 
} 

[int]$Retrycount = "0" 
StartService 
相关问题