Windows – Start Services thru Powershell

Background

I wish I can be like the rest of these guys who write scripts just for fun.

Oh No, Not yours truly.

Windows Patches were applied to our computers and unfortunately SQL Server Services did not start.

I mean they are set for auto-start, but that didn’t help me out this time.

Code

And, so need to develops scripts that can be ran manually or scheduled.

outline

  1. service.txt
    • Text file that contains the list of Services
  2. serviceStart.ps1
    • PowerShell script that starts each service in above list if service is not running
  3. invoke.cmd
    • Invoke PowerShell Script

service.txt

MSSQLSERVER
SQLSERVERAGENT

serviceStart.ps1

Set-StrictMode -Version 1

[System.Collections.ArrayList] $listofServices = $null;
[string] $log = $null;
[string] $filename =$null;
[string] $fileLog =$null;


[object] $objService=$null;

$listofServices = New-Object System.Collections.ArrayList;

$filename = "services.txt"
$fileLog = "log\serviceAutomation.log";


function folderMgmt($fileLog) 
{

	[string] $logFolder = $null;
	[boolean]$folderExist= $false;
	

	## prepare diagnostic statement
	$log = "Reviewing file {0} ..." -f $fileLog;

	## write debug statement
	Write-Debug -message $log
	Write-Host $log
	
	## Get Log Folder from filename
	$logFolder = Split-Path -Path $fileLog;


	## prepare diagnostic statement
	$log = "Log Folder is  {0} ..." -f $logFolder;

	## write debug statement
	Write-Debug $log
	
	## prepare diagnostic statement
	$log = "Check if folder {0} exist!" -f $logFolder;

	## write debug statement
	Write-Debug -message $log

	
	## Check Folder, if exists
	$folderExist = Test-Path $logFolder
	
	
	# if folder does not exist
	if ($folderExist -eq $false )
	{
	
		## prepare diagnostic statement
		$log = "Find File {0}, create folder {1}" -f $fileLog, $logFolder;

		## write debug statement
		Write-Debug -message $log

		## Create Folder
		New-Item $logFolder -type directory >$null;

	} ## if folder does not exist
	
} ## function folderMgmt


function readFile($filename)
{

	foreach ($service in get-content $filename)
	{

		## Add read line into $listofServices Array
		$listofServices.Add($service) >$null;

		$log = "Read {0}" -f $service;

		Write-Debug -message $log
		
	}

}
 

function processFile($fileLog)
{

	## Iterate list of Services
	$listofServices | ForEach-Object {

		# get service Name
		$service = $_;
		
		# get Service Node
		$objService = Get-Service $service;
		
		# get Service Status
		$serviceStatus = $objService.Status;
		
		# Display Current Step
		$log = "Processing Service {0}, Status {1}" -f $service, $serviceStatus;

		Write-Host $log
		
		# If Service is stopped
		if ($serviceStatus -eq "Stopped")
		{
		
			# Start Service
			Start-Service -InputObject $objService  -PassThru | Format-List >> $fileLog
		
		}

		
	}

}

folderMgmt($fileLog);
  
readFile($filename) >$null;

processFile($fileLog) >$null; 

	

invoke.cmd


set "_app=c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe"
set "_PSExecutionPolicy=bypass"

set "_currentFolder=%cd%"
set "_script=serviceStart.ps1"
set "_scriptFullName=%cd%\%_script%"

%_app% -NoProfile -Executionpolicy %_PSExecutionPolicy% -file %_scriptFullName%

Source Code Control

GitHub

Availed on GitHub here.

References

  1. Microsoft – Developer Network
    • Microsoft.PowerShell.Management
      • Split-Path
        Link
      • Service
  2. Microsoft Technet
    • Hey, Scripting Guy! Blog
      • Avoid PowerShell Errors by Initializing Variables
        Link
    • Technet Magazine
      • Windows PowerShell Scripting One Line at a Time
        Link
  3. Windows IT Pro
    • Creating your own PowerShell Functions
      Link
    • Arrays & Functions
      Link
  4. PwrShell.Net
    • Array
      • Back to Basics Arrays
        Link
  5. ss64.com
    • Arrays
      • Syntax Arrays
        Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s