[英]'Run a program' option in windows service pannel for failure recovery
每当出现服务崩溃时,我都试图运行Perl脚本。 perl脚本旨在重新启动服务并将邮件发送给所有开发人员。
为此,我使用了Windows恢复选项,它可以选择运行程序 。 我已经在命令行选项中填写了所需的详细信息,但是脚本似乎没有执行。 您能否通过分享您的知识来帮助我?
我已经尝试过使用“重新启动服务”选项,并且运行良好,但是运行程序未执行脚本。 我想念什么吗? 任何对此的评论将有所帮助。
我最近实现了一个恢复选项,以运行Powershell脚本,该脚本尝试按规定的次数重新启动服务,并在结束时发送电子邮件通知,它还附加了带有最近相关日志的txt文件。
经过几次尝试(尽管我已经看过所有其他事情),服务中“恢复”选项卡上的字段配置如下:
程式:Powershell.exe
**不是C:\\ Windows \\ System32 \\ WindowsPowerShell \\ v1.0 \\ Powershell.exe
命令行参数:-command“&{SomePath \\ YourScript.ps1'$ args [0]''$ args [1]''$ args [n]'}”
例如:-命令“&{C:\\ PowershellScripts \\ ServicesRecovery.ps1'服务名称'}”
** $ 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 = "Name@domain.com"
$FromAddress = "Name@domain.com"
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 = "Name@domain.com"
$FromAddress = "Name@domain.com"
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.