Skip to content

Instantly share code, notes, and snippets.

@slimflem
Last active December 5, 2016 10:59
Show Gist options
  • Save slimflem/2d48ad29350c0f2860ae96b40721a4a3 to your computer and use it in GitHub Desktop.
Save slimflem/2d48ad29350c0f2860ae96b40721a4a3 to your computer and use it in GitHub Desktop.
Batch Start Windows Services with common service name prefix (OctopusDeploy)
<#
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."
}
}
}
@slimflem
Copy link
Author

slimflem commented Apr 27, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment