Files
Powershell-Scripts/Services Scripts/MFA Server - Apache Services Restart in Order.ps1
T

51 lines
1.8 KiB
PowerShell

#MFA DB server
$ServerName = ""
#Names of services that need to start and listed in correct order
$ServiceName1 = "Tomcat8_Core"
$ServiceName2 = "Tomcat8_BackOffice"
$ServiceName3 = "Tomcat8_Scheduler"
#Variables for getting info about each service
$Service1 = Get-Service -Name $ServiceName1
$Service2 = Get-Service -Name $ServiceName2
$Service3 = Get-Service -Name $ServiceName3
#Testing the connection to the DB server(must be online before services can start)
If (Test-Connection $ServerName) {
#If DB server is up then stop all services
Stop-Service $ServiceName1 -Force -verbose
Stop-Service $ServiceName2 -Force -verbose
Stop-Service $ServiceName3 -Force -verbose
#Refreshes the status of each service
$Service1.Refresh()
$Service2.Refresh()
$Service3.Refresh()
#Making sure all 3 services are stopped before bringing them up in correct order
If ($Service1.status -and $Service2.status -and $Service3.status -eq "Stopped") {
Start-Service $ServiceName1 -verbose
Start-Sleep -seconds 60
$Service1.Refresh()
If ($Service1.status -eq "Running") {
Start-Service $ServiceName2 -verbose
Start-Sleep -seconds 60
$Service2.Refresh()
}
If ($Service2.status -eq "Running") {
Start-Service $ServiceName3 -verbose
Start-Sleep -seconds 30
$Service3.Refresh()
}
}
If ($Service1.status -and $Service2.status -and $Service3.status -eq "Running") {
Write-Host "Script successfully restarted services in correct order"
}
Else {
Write-Host "Services did not get restarted correctly. Please run script again or manually start services"
}
}
Else {
Write-host "Ping to $ServerName failed, exiting script"
}