diff --git a/Services Scripts/Service - SAF Restart Services if Stopped.ps1 b/Services Scripts/Service - SAF Restart Services if Stopped.ps1 new file mode 100644 index 0000000..9da1a2b --- /dev/null +++ b/Services Scripts/Service - SAF Restart Services if Stopped.ps1 @@ -0,0 +1,97 @@ +<# +Author: Gabe Kerntke +Company: UFS LLC +Date: 02-05-2024 + +.Synopsis +Script checks if any of the Store and Forward (SAF) machine services are stopped. If any(or all) of the services are not running, it will manually stop, then start all the services and check the services again to see if they are running. +The script loops through this check 3 times before exiting. + +.Modified +2024-02-05 (GabeK) - Original script created +2024-02-05 (CraigK) - Removed code in start \ stop functions that wasn't needed +2024-02-06 (GabeK) - Updated script with loop function +2024-02-07 (GabeK) - updated script to remove verbose and added output of stopped services +#> +[CmdletBinding()] +Param( + [Parameter(Mandatory = $false)] + $ServiceName_0 = "Apache-SF", + [Parameter(Mandatory = $false)] + $ServiceName_1 = "Apache-IWS", + [Parameter(Mandatory = $false)] + $ServiceName_2 = "FiservIWSImageService1", + [Parameter(Mandatory = $false)] + $ServiceName_3 = "FiservIWSImageService2", + [Parameter(Mandatory = $false)] + $ServiceName_4 = "FiservIWSImageService3", + [Parameter(Mandatory = $false)] + $ServiceName_5 = "FiservIWSImageService4", + [Parameter(Mandatory = $false)] + $ServiceName_6 = "FiservIWSImageService5", + [Parameter(Mandatory = $false)] + $ServiceName_7 = "FiservIWSImageService6", + [Parameter(Mandatory = $false)] + $ServiceName_8 = "FiservIWSImageService7", + [Parameter(Mandatory = $false)] + $ServiceName_9 = "FiservIWSImageService8" +) +$SAFservices = Get-Variable -Name "ServiceName_[0-9]*" | Select-Object -ExpandProperty Value + +#Region Functions + +#Stop SAF Services +Function StopSAFservices { + Stop-Service "Apache-SF" -Force | Out-Null + Stop-Service "Apache-IWS" -Force | Out-Null + Stop-Service "FiservIWSImageService1" -Force | Out-Null + Stop-Service "FiservIWSImageService2" -Force | Out-Null + Stop-Service "FiservIWSImageService3" -Force | Out-Null + Stop-Service "FiservIWSImageService4" -Force | Out-Null + Stop-Service "FiservIWSImageService5" -Force | Out-Null + Stop-Service "FiservIWSImageService6" -Force | Out-Null + Stop-Service "FiservIWSImageService7" -Force | Out-Null + Stop-Service "FiservIWSImageService8" -Force | Out-Null + Start-Sleep 15 + StartSAFservices +} + +#Start SAF Services +Function StartSAFservices { + foreach ($Service in $SAFservices) { + Start-Service $Service | Out-Null + Start-Sleep 5 + } +} + +#Check for stopped SAF services +Function CheckSAFservices { + $ServicesExist = Get-Service $SAFservices -ErrorAction SilentlyContinue + If ($ServicesExist -ne $null) { + #Gets just the status of the services to then see if any are stopped + $ServiceCheck = Get-Service $SAFservices | Select-Object -ExpandProperty Status + If ($ServiceCheck -contains "Stopped") { + Write-Host "One or all services are stopped, proceeding to restart all services" + Get-Service $SAFservices | Select-Object Status, Name | Where-Object { $_.Status -eq "Stopped" } | Format-List | Write-Output + StopSAFservices + } + Else { + Write-Host "All services are running, exiting script" + Break + } + } + Else { + Write-Host "The SAF services don't exist on this machine, exiting script" + Break + } +} + +Function LoopCheckingforServices { + for ($i = 0; $i -lt 3; $i++) { + $SAFservices.Refresh + CheckSAFservices + } +} + +#Region Script Start +LoopCheckingforServices \ No newline at end of file