title: Restarting the apache tomcat services using powershell script
body: <p>I am doing an automation using powershell script to automaticlly start apache tomcat services if it goes down. But the code is not working as expected. Please find the code below :</p>
  
  <pre><code>$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
  
  }
  </code></pre>
  
  <p>Can somebody please help me</p>