Skip to content

Manage services and their startup mode

Managing a lot of services during a maintenance window or when they need to be shut down in a particular order can be somewhat cumbersome.

Lets say you have 10 services that need to be shut down and then started in a particular order and with some wait time in between, sitting and doing this manually is outright boring, and then think if you need to do this on several servers every time.

So i built this script to take care of just that, it start/stops the services in the order they are put in the variables $startServices and $stopServices, and waits the amount of time that the variable $timeSleep is set to between each service.

The script even set the startup mode for the services, so the script even takes care of that part.

Both the -Action and -SetService parameters are required and the script cannot handle different startup modes per service yet, will look into that for a newer version of the script.

Stop services and set to disabled.

.\ServiceMaintenance.ps1 -Action Stop -SetService "Disabled"

Start services and set startup mode to automatic

.\ServiceMaintenance.ps1 -Action Start -SetService "Automatic"

The options for -SetService is Automatic, Disabled, Manual and Automatic (Delayed Start)

ServiceMaintenance.ps1
##############################################################################################
#                                                                                            #
# Script to start or stop given services                                                     #
# and setting them in desired startup mode.                                                  #
#                                                                                            #
# Ex. .\ServiceMaintenance.ps1 -Action Start -SetService "Automatic"                         #
#     .\ServiceMaintenance.ps1 -Action Stop -SetService "Manual"                             #
#                                                                                            #
# -Action takes either start/stop and is case insensitive                                    #
# -SetService takes the following input, Manual/Automatic/Automatic (Delayed Start)/Disabled #
#                                                                                            #
# Author: Marcus Uddenhed                                                                    #
#                                                                                            #
##############################################################################################

# Get Action parameter(start/stop)
param (
    [Parameter(Mandatory=$true)][string]$Action,
    [Parameter(Mandatory=$true)][string]$SetService
)

## Global parameters
# Add the Service Names here, can be one or many,
# It's not Display Name that goes here.
#
# Services to start and in what order.
$startServices=("a","b","c")
# Services to stop and in what order.
$stopServices=("c","b","a")
# Sleep time in seconds between each service start and stop.
$timeSleep="10"

## Create functions that actually do the work.

# Stop services and set to desired Start Type.
function stopServices(){
    # Set service counter
    $sCount = $stopServices.Count
    # Loop through services
    foreach($s in $stopServices){
        # Get service name
        $service = Get-Service -Name $s -ComputerName "./"
        # Inform to console
        Write-Output "Stopping service '$s'..."
        # Do the work.
        if ($service.Status -eq 'Running') {
            $service | Stop-Service -Force
            $service | Set-Service -StartupType "$SetService"
            Write-Output "Stopped service '$s' and set to '$SetService' startup type..."
            # Sleep for a while before continue with next service, skip on last.
            if($sCount -gt 1 ){
                Write-Output "Sleeping '$timeSleep' seconds between services..."
                Start-Sleep -Seconds $timeSleep
                $sCount = $sCount-1
            }
        }
        else {
            # Inform if service status iss different than expected.
            Write-Output "Service '$s' is already stopped or in another start state..."
        }
    }
}

# Start services and set to desired Start Type.
function startServices(){
    # Set service counter
    $sCount = $startServices.Count
    # Loop through services
    foreach($s in $startServices){
        # Get service name.
        $service = Get-Service -Name $s -ComputerName "./"
        # Inform to console.
        Write-Output "Starting service '$s'..."
        # Do the work.
        if ($service.Status -eq 'Stopped') {
            $service | Set-Service -StartupType "$SetService"
            $service | Start-Service
            Write-Output "Started service '$s' and set to '$SetService' startup type..."
            # Sleep for a while before continue with next service, skip on last.
            if($sCount -gt 1 ){
                Write-Output "Sleeping '$timeSleep' seconds between services..."
                Start-Sleep -Seconds $timeSleep
                $sCount = $sCount-1
            }
        }
        else {
            # Inform if service status iss different than expected.
            Write-Output "Service '$s' is already running or not in a stopped state..."
        }
    }
}

## Do the magic.

# Start services in case input is to start.
if($Action -ieq "start"){
    startServices
}
# Stop services in case input is to stop.
if($Action -ieq "stop"){
    stopServices
}