Start-ServicesInOrder.ps1


Description

Purpose

Starts a list of services in the order they are provided.

Detailed Description

This function starts a list of services in the order they are provided. It waits for each service to start before moving on to the next one.

Back to Top

Usage

Example 1

Start-ServicesInOrder -ServiceNames "Service1", "Service2", "Service3"

Starts Service1, waits for it to start, then starts Service2, waits for it to start, then starts Service3.

Example 2

$services = @(

“Ransomcare Admin Service”, “Ransomcare Accumulative Sensors Service”, “Ransomcare Database Service”, “Ransomcare Hub Service”, “Ransomcare ML Service”, “Ransomcare Share Service”, “Ransomcare Sharepoint Service”, “Ransomcare Validation Service” ) Start-Services -ServiceNames $services

Back to Top

Notes

Author: Unknown Date: Unknown

Back to Top


Script

function Start-ServicesInOder {

    <#
    .SYNOPSIS
    Starts a list of services in the order they are provided.

    .DESCRIPTION
    This function starts a list of services in the order they are provided. It waits for each service to start before moving on to the next one.

    .PARAMETER ServiceNames
    The names of the services to start.

    .EXAMPLE
    Start-ServicesInOrder -ServiceNames "Service1", "Service2", "Service3"
    Starts Service1, waits for it to start, then starts Service2, waits for it to start, then starts Service3.

    .EXAMPLE
    $services = @(
        "Ransomcare Admin Service",
        "Ransomcare Accumulative Sensors Service",
        "Ransomcare Database Service",
        "Ransomcare Hub Service",
        "Ransomcare ML Service",
        "Ransomcare Share Service",
        "Ransomcare Sharepoint Service",
        "Ransomcare Validation Service"
    )

    Start-Services -ServiceNames $services

    .NOTES
    Author: Unknown
    Date: Unknown
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string[]]$ServiceNames
    )

    foreach ($service in $ServiceNames) {
        try {
            Start-Service -Name $service -ErrorAction Stop
            do {
                Start-Sleep -Milliseconds 500
                $status = (Get-Service -Name $service).Status
            } until ($status -eq "Running")
            Write-Output "$service - started successfully."
        }
        catch {
            Write-Output "Failed to start $service - $($_.Exception.Message)"
        }
    }
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top