-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-service.ps1
63 lines (55 loc) · 1.45 KB
/
run-service.ps1
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
# Checking the services are existing and running
# Developed by Capella87
# Check PowerShell Version
$PSVersion = $PSVersionTable.PSVersion
$exeName = ""
if ($PSVersion.Major -le 5)
{
Write-Host "This script requires PowerShell version 5 or higher"
exit
}
# Check OS is Windows
if ($PSVersion.Major -ge 6)
{
if ($IsWindows -eq $false)
{
Write-Host "This script is only for Windows"
exit
}
$exeName = "pwsh.exe"
}
elseif ($PSVersion.Major -eq 5)
{
if ($env:OS -ne "Windows_NT")
{
Write-Host "This script is only for Windows"
exit
}
$exeName = "powershell.exe"
}
# Check the services are existing
$services = "RabbitMQ", "MariaDB"
Write-Host $services
# Run the service with administrator privileges
for ($i = 0; $i -lt $services.Length; $i++)
{
$serviceName = $services[$i]
$service = Get-Service -Name $serviceName
if ($null -eq $service)
{
Write-Host "The service $serviceName is not exist. Check out the service name or install the program"
exit
}
if ($service.Status -eq "Stopped")
{
Write-Host "The service $serviceName is stopped"
Write-Host "Starting the service $serviceName"
# Start the service with administrator privileges
Start-Process -FilePath $exeName -ArgumentList "Start-Service -Name $serviceName" -Verb RunAs
}
else
{
Write-Host "The service $serviceName is running"
continue
}
}