Ephesoft.Transact.Service.psm1

<#
.SYNOPSIS
  Restarts the Ephesoft Transact service.
.DESCRIPTION
  Restarts the Ephesoft Transact service.
.EXAMPLE
  Restart-TransactService
#>

Function Restart-TransactService {
    [CMDLetBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')]
    Param()

    $MaxTimeout = New-TimeSpan -Minutes 2

    if ($pscmdlet.ShouldProcess('Restart', 'Transact')) {
        $ServiceName = $null
        if (Get-Service 'EphesoftEnterprise' -ErrorAction SilentlyContinue) {
            $ServiceName = 'EphesoftEnterprise'
        }
        elseif (Get-Service "EphesoftTransact" -ErrorAction SilentlyContinue) {
            $ServiceName = 'EphesoftTransact'
        }
        else {
            Write-Output 'No Transact service found'
        }

        if ($ServiceName) {
            $Service = Get-Service $ServiceName
            if ($Service.Status -eq 'Running') {
                Write-Output "Attempting to stop $($Service.Name)"
                Stop-Service $Service.Name -NoWait

                try {
                    # Wait until the Transact service is stopped or timeout is reached, whichever occurs first
                    $Service.WaitForStatus('Stopped', $MaxTimeout)
                }
                catch [System.ServiceProcess.TimeoutException] {
                    Write-Warning "$($Service.Name) did not stop within the timeout period $($MaxTimeout.TotalMinutes) minutes"
                }

                Write-Output "Stopping tomcat service if running"
                $TomcatProcess = Get-Process tomcat*
                if ($TomcatProcess) {
                    Stop-Process $TomcatProcess -Force
                    try {
                        $ProcessTimeout = 10000

                        # Wait until the tomcat process is stopped or timeout is reach, whichever occurs first
                        $TomcatProcess.WaitForExit($ProcessTimeout) | Out-Null
                    }
                    catch {
                        Write-Error "$($TomcatProcess.ProcessName) did not stop within the timeout period of $($ProcessTimeout/1000) seconds"
                        Exit 1
                    }
                }

                # Now that we've forced tomcat process to stop, check that the Transact service is stopped
                try {
                    # Wait until the Transact service is stopped or timeout is reached, whichever occurs first
                    $Service.WaitForStatus('Stopped', $MaxTimeout)
                }
                catch [System.ServiceProcess.TimeoutException] {
                    Write-Error "$($Service.Name) did not stop within the timeout period $($MaxTimeout.TotalMinutes) minutes after attempting to stop tomcat"
                    Exit 1
                }

                Write-Output "Starting $($Service.Name)"
                Start-Service $Service.Name -ErrorAction Stop

                try {
                    # Wait until the Transact service is running or timeout is reached, whichever occurs first
                    $Service.WaitForStatus('Running', $MaxTimeout)
                    Write-Output "$($Service.Name) is currently running. Please wait a few minutes for Transact to come online"
                }
                catch [System.ServiceProcess.TimeoutException] {
                    Write-Error "$($Service.Name) did not start within the timeout period $($MaxTimeout.TotalMinutes) minutes"
                    Exit 1
                }
            }
            else {
                Write-Output "$($Service.Name) is not currently running"
                try {
                    Write-Output "Starting $($Service.Name)"
                    Start-Service $Service.Name -ErrorAction Stop

                    # Wait until the Transact service is running or timeout is reached, whichever occurs first
                    $Service.WaitForStatus('Running', $MaxTimeout)
                    Write-Output "$($Service.Name) is currently running. Please wait a few minutes for Transact to come online"
                }
                catch [System.ServiceProcess.TimeoutException] {
                    Write-Error "$($Service.Name) did not start within the timeout period $($MaxTimeout.TotalMinutes) minutes"
                    Exit 1
                }
            }
        }
    }
}