2016-12-27 27 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 
相關問題