Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用powershell脚本重新启动apache tomcat服务_Powershell - Fatal编程技术网

使用powershell脚本重新启动apache tomcat服务

使用powershell脚本重新启动apache tomcat服务,powershell,Powershell,我正在使用powershell脚本进行自动化,以便在ApacheTomcat服务出现故障时自动启动它。但是代码并没有像预期的那样工作。请查找以下代码: $serviceName = "AGTtomcat7w" $serviceNamePath = "D:/Pipeline_AGT/apache-tomcat-7.0.82/bin/startup.bat" $service = Get-Service | Where-Object { $_.Name -eq $serviceName } wri

我正在使用powershell脚本进行自动化,以便在ApacheTomcat服务出现故障时自动启动它。但是代码并没有像预期的那样工作。请查找以下代码:

$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

}

有人能帮我把$serviceName路径改成$serviceName吗。重新启动服务需要它应该重新启动的服务的名称,而不是文件路径

Restart-Service $serviceNamePath -force

Restart-Service $serviceName -force

我没有看到
重新启动服务的任何路径参数,只有
-Name
。请尝试重新启动服务-Name$serviceName
。如何定义“代码未按预期工作”?您希望代码做什么?@bluf,如果服务停止,它应该重新启动服务。但事实并非如此restarting@SagePourpre,运行代码时出现以下错误:无法在计算机“”上打开AGTtomcat7w服务。如果有批处理文件,为什么还要使用
重新启动服务?只需运行
和$serviceNamePath