I modified some of your code, but I left most of it "as is". I wasn't sure if you were supposed to run your web request for every service, or only once after all services were started.
[Array] $Services = 'LAS_LMB', 'LAS_LWS', 'LAS_CORE';
foreach ($ServiceName in $Services) {
$MustForce = $false # kill process?
$loopcount = 0
$arrService = Get-Service -Name $ServiceName
Write-Host $ServiceName
while ($arrService.Status -eq 'Running') { # what about other states? E.g., ContinuePending, Paused, StopPending, etc? Maybe change to "-ne "Stopped"?
# consider using "-Force if this tries to stop the service more than "X" times, as below
# and then try killing the service if it hasn't stopped on its own
$loopcount++
if ($loopcount -gt 3){
$MustForce = $true
}
elseif ($loopcount -gt 5) {
Write-Host "Cannot stop or kill service $ServiceName! HELP!!!!"
Exit
}
Stop-Service $ServiceName -Force:$MustForce
Write-Host $arrService.status
Write-Host $ServiceName stopping
Start-Sleep -seconds 60
$arrService.Refresh()
if ($arrService.Status -eq 'Stopped') {
Write-Host $ServiceName is now Stopped
}
}
}
$las_path = "D:\LAS\"
$tomcat_core = "tomcat_core"
$tomcat_web = "tomcat_web"
$tomcat_lmb = "tomcat_lmb"
$log_folder = "logs"
$temp_folder = "temp\*"
$localhost_folder = "work\Catalina\localhost"
$PATH = Join-Path $las_path -ChildPath $tomcat_core | Join-Path -ChildPath $localhost_folder
$PATH1 = Join-Path $las_path -ChildPath $tomcat_web | Join-Path -ChildPath $localhost_folder
$PATH2 = Join-Path $las_path -ChildPath $tomcat_lmb | Join-Path -ChildPath $localhost_folder
$TEMPPATH = Join-Path $las_path -ChildPath $temp_folder
$LOGDIR = Join-Path $las_path -ChildPath $log_folder
if ((Get-Service $Services | Where-Object {$_.Status -eq "Stopped"}).count -eq $Services.count){ # all services are stopped?
Start-Sleep 5
Remove-Item -Path $PATH, $PATH1, $PATH2, $TEMPPATH -Force -Recurse -Verbose
Get-ChildItem -Path $LOGDIR -Recurse -include *.log -File | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } | Remove-Item -Verbose
Write-Host 'Folders was deleted'
Break
}
else{
Write-Host "Something has started one of the services! HELP!!!" -ForegroundColor RED
exit
}
foreach ($ServiceName in $Services) {
$arrService = Get-Service -Name $ServiceName
Write-Host $ServiceName
while ($arrService.Status -ne 'Running') {
Start-Service $ServiceName
Write-Host $arrService.status
Write-Host $ServiceName starting
Start-Sleep -seconds 30
$arrService.Refresh()
}
# don't do any of this unless the service is actually running
$CORESt = Get-Service -DisplayName "LAS_CORE" | Where-Object { $_.Status -eq "Running" }
$WEBSt = Get-Service -DisplayName "LAS_LWS" | Where-Object { $_.Status -eq "Running" }
$LMBSt = Get-Service -DisplayName "LAS_LMB" | Where-Object { $_.Status -eq "Running" }
#$servicesToStartLASCORE = { Write-Host "Starting service LASCORE"; Start-Service -Name "LASCORE" -Verbose; Start-Sleep -Seconds $startBreak }
#$WebResponse = (Invoke-WebRequest "http://.........../" | Select-Object -Property Content).content
$WebResponse1 = (Invoke-WebRequest "http://........................")
$uri1 = 'http://.................'
$user = '..........'
$pass = '.........' | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential ($user, $pass)
$REMOTEDB = Invoke-WebRequest -Uri $uri1 -Credential $cred
$uri2 = '............'
$cred = New-Object Management.Automation.PSCredential ($user, $pass)
$LAGOFS = Invoke-WebRequest -Uri $uri2 -Credential $cred
$uri3 = 'http://.................'
$cred = New-Object Management.Automation.PSCredential ($user, $pass)
$Message = Invoke-WebRequest -Uri $uri3 -Credential $cred
if ($WebResponse1.StatusCode -eq 200 -and $REMOTEDB.StatusCode -eq 200 -and $LAGOFS.StatusCode -eq 200) {
Write-Host $ServiceName is now Running
}
}