Ephesoft.Transact.Service.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<#
.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
                }
            }
        }
    }
}