black laptop computer keyboard in closeup photo
DevHacks

Automating Windows Services Startup with PowerShell Script

In this blog post, we will explore a PowerShell script that automates the process of starting a set of Windows services if they are stopped. This script can be a valuable tool for system administrators and developers who need a reliable way to ensure that critical services are running smoothly. By leveraging the power of PowerShell and Windows Task Scheduler, we can create a scheduled task that runs the script at regular intervals, providing an automated solution for service monitoring and startup.

Script: Starting Windows Services

##################################################
# Description: 
# Check for services in array, 
# if service is set auto and didn't run, start it.
#
# Author: Sohail Iqbal for innofy.co
##################################################

function Start-Services {
	param (
		[string[]]$Services
	)
	
	Write-Output "==== Start-Services ===="
	
    foreach ($ServiceName in $Services) {
		# get system time
		$runTime = (Get-Date).ToUniversalTime().ToString("o")
		
		Write-Output "$runTime | Checking $ServiceName..."
		
		# Get current status of service
		$CurrentStatus = Get-Service -Name $ServiceName | Where-Object {$_.StartType -eq "Automatic" -and $_.Status -ne "Running"}
		
		# Service found to be not running
		if ($CurrentStatus)
		{
			# Stopped
			Write-Output "$runTime | $ServiceName didn't start automatically, attempting start..."

			# Start service
			$StartStatus = Set-Service -Name $ServiceName -Status Running -PassThru
			if($StartStatus.Status -eq "Running") {
				Write-Output "$runTime | $ServiceName service started successfully."
			}
			else
			{
				Write-Output "$runTime | $ServiceName service failed to start."
			}
		}
		else
		{
			# Running
			$s = $CurrentStatus.Status
			Write-Output "$runTime | $ServiceName is already running... $s"
			Continue
		}
	}
}

# Services to check and start
$ServicesList = @("ACME.ReportsGenerator", "ACME.FileWatcherService", "ACME.ThubmnailGenerator", "ACME.MessageBroker")

# Start checking
Start-Services($ServicesList)Code language: PHP (php)

The PowerShell script provided above, titled “Start-Services,” checks a list of services specified in the $ServicesList array. It verifies whether each service has its start type set to “Automatic” and if the service is not currently running. If a service is found to be in this state, the script attempts to start it using the Set-Service cmdlet. The script logs the result of each service’s startup attempt, indicating success or failure.

Setting Up a Scheduled Task in Windows Task Scheduler:

  1. Open Windows Task Scheduler: You can access Task Scheduler by typing “Task Scheduler” in the Windows search bar or by navigating to Control Panel > Administrative Tools > Task Scheduler.
  2. Create a New Task: In the Task Scheduler window, click on “Create Task” in the right-hand Actions pane. Provide a name and description for the task.
  3. Configure Triggers: In the Triggers tab, click “New” to create a new trigger. Set the trigger to “Repeat task every” and enter “10 minutes” in the corresponding field. Adjust other settings as per your preference.
  4. Configure Actions: In the Actions tab, click “New” to create a new action. Set the action to “Start a program” and provide the path to the PowerShell executable (powershell.exe). In the “Add arguments” field, enter the path to the PowerShell script file that contains the code provided above.
  5. Configure Additional Settings: Adjust other settings such as the security options, conditions, and settings based on your requirements.
  6. Save and Test: Click “OK” to save the task. You can manually run the task to test its functionality. The task will now execute the PowerShell script every 10 minutes, automatically starting the specified services if necessary.

With the PowerShell script and the scheduled task created in Windows Task Scheduler, you now have an automated solution for starting a set of Windows services if they are stopped. This approach ensures the reliability and availability of critical services and reduces the need for manual intervention. By following the steps outlined in this blog post, you can implement this script in your system and enhance your service monitoring capabilities.

Hi, I’m webadmin

Leave a Reply

Your email address will not be published. Required fields are marked *