使用Powershell脚本重新启动Apache Tomcat服务

时间:2019-12-30 03:52:18

标签: powershell

我正在使用Powershell脚本执行自动化,以自动启动apache tomcat服务(如果它出现故障)。但是代码无法按预期工作。请在下面找到代码:

$serviceName = "AGTtomcat7w"
$serviceNamePath = "D:/Pipeline_AGT/apache-tomcat-7.0.82/bin/startup.bat"

$service = Get-Service | Where-Object { $_.Name -eq $serviceName }
write-host "Service Status is" $service.status

if ($service.status -eq "Running") {
   $smtpServer = "161.99.65.10"
   $msg = new-object Net.Mail.MailMessage
   $smtp = new-object Net.Mail.SmtpClient($smtpServer)

   $dateString = get-date

   $msg.From = "noreply@example.com"
   $msg.To.Add("abc@xyz.com")
   $msg.subject = "Web services Monitoring"
   $msg.body = ([string]$dateString) + " AGT Maxavera Service" + " is " + $service.status 
   $smtp.send($msg)
}

else {
   write-host "Sending waring email"

   $smtpServer = "161.99.65.10"
   $msg = new-object Net.Mail.MailMessage
   $smtp = new-object Net.Mail.SmtpClient($smtpServer)

   $dateString = get-date

   $msg.From = "noreply@example.com"
   $msg.To.Add("abc@xyz.com")
   $msg.subject = "Service " + $service + " is " + $service.status
   $msg.body = ([string]$dateString) + " AGT Maxavera Service " + " is " + $service.status 
   $smtp.send($msg)

   #Starting the service   
   Restart-Service $serviceNamePath -force

}

有人可以帮我吗

1 个答案:

答案 0 :(得分:0)

尝试将$ serviceNamePath更改为$ serviceName。 Restart-Service需要其应重新启动的服务的名称,而不是文件路径。

Restart-Service $serviceNamePath -force

Restart-Service $serviceName -force