| 1 |
## Define the service name in a variable |
| 2 |
$ServiceName = 'EventLog' |
| 3 |
|
| 4 |
## Read the service from Windows to return a service object |
| 5 |
$ServiceInfo = Get-Service -Name $ServiceName |
| 6 |
|
| 7 |
## If the server is not running (ne) |
| 8 |
if ($ServiceInfo.Status -ne 'Running') { |
| 9 |
## Write to the console that the service is not running |
| 10 |
Write-Host 'Service is not started, starting service' |
| 11 |
## Start the service |
| 12 |
Start-Service -Name $ServiceName |
| 13 |
## Update the $ServiceInfo object to reflect the new state |
| 14 |
$ServiceInfo.Refresh() |
| 15 |
## Write to the console the Status property which indicates the state of the service |
| 16 |
Write-Host $ServiceInfo.Status |
| 17 |
} else { ## If the Status is anything but Running |
| 18 |
## Write to the console the service is already running |
| 19 |
Write-Host 'The service is already running.' |
| 20 |
} |
| 21 |
|
| 22 |
## From https://adamtheautomator.com/powershell-script-examples/ |
| 23 |
|