我正在尝试在服务崩溃时运行perl脚本。 perl脚本打算重新启动服务并向所有开发人员发送邮件。
我已经使用了Windows恢复选项,它可以选择运行程序。我在命令行选项中填写了所需的详细信息,但脚本似乎没有被执行。你可以通过分享你的知识来帮助我吗?
我尝试过使用重启服务选项,但运行正常,但运行程序并不执行脚本。我错过了什么吗? 对此的任何评论都会有所帮助。
答案 0 :(得分:0)
我最近实现了一个恢复选项来运行一个powershell脚本,该脚本尝试重新启动服务一定次数,并在结束时发送电子邮件通知,它还会附加一个带有最新相关日志的txt文件。
经过多次尝试(尽管我见过所有其他事情)服务中恢复选项卡上的字段配置如下:
程序:Powershell.exe
**不是C:\ Windows \ System32 \ WindowsPowerShell \ v1.0 \ Powershell.exe
命令行参数: - 命令“& {SomePath \ YourScript.ps1'$ args [0]''$ args [1]''$ args [n]'}”
例如:-command“& {C:\ PowershellScripts \ ServicesRecovery.ps1'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 = "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