Last active
December 5, 2016 10:59
-
-
Save slimflem/2d48ad29350c0f2860ae96b40721a4a3 to your computer and use it in GitHub Desktop.
Batch Start Windows Services with common service name prefix (OctopusDeploy)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
This script will start any non-disabled SystemX service on the machine on which it is run. | |
This script, as written, executes as part of an OctopusDeploy project. | |
#> | |
# Receive OctopusDeploy parameter that indicates the SystemX Service name prefix value. | |
# Capture supplied OctopusDeploy variable in a local variable. | |
$prefixValue = $OctopusParameters['SystemXServiceNamePrefix'] | |
Write-Output "SystemX Service prefix value is: $prefixValue" | |
$systemXServices = Get-Service "$prefixValue" -ErrorAction SilentlyContinue | |
if ($systemXServices -ne $null) | |
{ | |
foreach($systemXService in $systemXServices) | |
{ | |
$displayName = $systemXService.DisplayName | |
$serviceName = $systemXService.Name | |
#Write-Output "My name is $serviceName!" | |
# determine the Windows Service StartMode. | |
$wmiSvcObj = Get-WmiObject -Class Win32_Service -Property StartMode -Filter "Name='$serviceName'" | |
if ($wmiSvcObj.StartMode -eq "Disabled") | |
{ | |
Write-Output "$serviceName is disabled. Skipping." | |
continue; | |
} | |
if ($systemXService.Status -eq "Running") | |
{ | |
Write-Output "$displayName is already running." | |
} | |
else | |
{ | |
Start-Service $displayName | |
# start and wait - be patient. | |
$systemXService.WaitForStatus('Running', '00:00:30') | |
Write-Output "$displayName has started." | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Igal's Stop script provided a starting point for ideas. His script is in the comments.
https://library.octopusdeploy.com/#!/step-template/actiontemplate-windows-service-stop