Last active
September 20, 2017 17:11
-
-
Save Colby-PDQ/e786b08e51539d873ffb6da3f95c3f61 to your computer and use it in GitHub Desktop.
A sanitized version of a service control script I wrote at a previous company. It was one of the first scripts I wrote, so please forgive some of the egregious sins like Write-Host ;)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Created by Colby Bouma | |
# This script shuts down or starts up services in a specific order | |
# | |
# Usage: & '.\Service Control.ps1' [-Environment] {PROD, DEV, TEST} [[-Target_Name] <server short name>] [-Start] | |
# You must always specify an environment that exists in "Service Control.xml" | |
# The default is to shut down the specified environment. | |
# The [-Start] parameter will cause it to start up the specified environment. | |
# The [-Target_Name] parameter may be used to run this script against 1 specified server, using the <Server> name from "Service Control.xml" | |
# Example of shutting down DEV: & '.\Service Control.ps1' DEV | |
# Example of starting up PROD: & '.\Service Control.ps1' PROD -Start | |
# Example of shutting down the second DEV App server: & '.\Service Control.ps1' DEV APP2 | |
# Example of mixing parameter order: & '.\Service Control.ps1' -Start -Target_Name WEB3 -Environment PROD | |
############################################################################### | |
## Setup | |
############################################################################### | |
param ( | |
[string]$Environment = $(throw "PROD, DEV, or TEST is required."), | |
[switch]$Start = $false, | |
[string]$Target_Name | |
) | |
# Import config file | |
[xml]$Config_File = Get-Content "Service Control.xml" | |
############################################################################### | |
## Functions | |
############################################################################### | |
function Service_Control { | |
param ( [string]$Server_Name, [string]$Service_Name ) | |
# Retrieve the status of the service | |
$Service_Object = Get-Service -ComputerName $Server_Name -Name $Service_Name | |
Write-Host $Server_Name $Service_Name - $Service_Object.Status | |
# Make sure the startup type of the service is set to Manual | |
Set-Service -ComputerName $Server_Name -Name $Service_Name -StartupType Manual | |
if ( $Start -eq $false ) { | |
# Shutdown mode | |
# Stop the service if it is running | |
if ( $Service_Object.Status -eq "Running" ) { | |
$Service_Object.Stop() | |
$Service_Object.WaitForStatus("Stopped", (New-TimeSpan -Seconds 120)) | |
if ( $Service_Object.Get_Status() -eq "Stopped" ) { | |
Write-Host $Server_Name $Service_Name - Stopped | |
} else { | |
Write-Host Failed to stop $Server_Name $Service_Name, exiting | |
Exit $ErrorCode | |
} | |
} | |
} else { | |
# Startup mode | |
# Start the service if it is stopped | |
if ( $Service_Object.Status -eq "Stopped" ) { | |
$Service_Object.Start() | |
$Service_Object.WaitForStatus("Running", (New-TimeSpan -Seconds 60)) | |
if ( $Service_Object.Get_Status() -eq "Running" ) { | |
Write-Host $Server_Name $Service_Name - Running | |
} else { | |
Write-Host Failed to start $Server_Name $Service_Name, exiting | |
Exit $ErrorCode | |
} | |
} | |
} | |
# Echo a blank line | |
Write-Host | |
} | |
############################################################################### | |
## Main | |
############################################################################### | |
$Services = @() | |
# Build the Services array | |
foreach ( $Service in $Config_File.Environment.$Environment.Service.SelectNodes("*") ) { | |
$Services += , ($Service.Server,$Service.Name) | |
} | |
# Reverse the array if -Start is specified | |
# Services have to be brought up in reverse of shutdown order | |
if ( $Start -eq $true ) { | |
[array]::Reverse($Services) | |
} | |
# Iterate through the services | |
foreach ( $Service_Name in $Services ) { | |
# Skip empty services, these don't exist in the specified environment | |
if ( $Service_Name[1] -ne "" ) { | |
# If $Target_Name is not blank, only run against specified server | |
if ( $Target_Name -ne "" ) { | |
if ( $Target_Name -eq $Service_Name[0] ) { | |
Service_Control $Config_File.Environment.$Environment.Server.($Service_Name[0]) $Service_Name[1] | |
} | |
} else { | |
Service_Control $Config_File.Environment.$Environment.Server.($Service_Name[0]) $Service_Name[1] | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--This is an example config file for 'Service Control.ps1'--> | |
<Environment> | |
<DEV> | |
<Server> | |
<APP1>DEV-APP1</APP1> | |
<APP2>DEV-APP2</APP2> | |
<WEB1>DEV-WEB1</WEB1> | |
<WEB2>DEV-WEB2</WEB2> | |
</Server> | |
<Service> | |
<CoolApp> | |
<Name>CoolApp.Server</Name> | |
<Server>APP1</Server> | |
</CoolApp> | |
<CoolApp> | |
<Name>CoolApp.Server</Name> | |
<Server>APP2</Server> | |
</CoolApp> | |
<WebServer> | |
<Name>nginx</Name> | |
<Server>WEB1</Server> | |
</WebServer> | |
<WebServer> | |
<Name>nginx</Name> | |
<Server>WEB2</Server> | |
</WebServer> | |
</Service> | |
</DEV> | |
<PROD> | |
<Server> | |
<APP1>PROD-APP1</APP1> | |
<APP2>PROD-APP2</APP2> | |
<APP3>PROD-APP3</APP3> | |
<WEB1>PROD-WEB1</WEB1> | |
<WEB2>PROD-WEB2</WEB2> | |
<WEB3>PROD-WEB3</WEB3> | |
</Server> | |
<Service> | |
<CoolApp> | |
<Name>CoolApp.Server</Name> | |
<Server>APP1</Server> | |
</CoolApp> | |
<CoolApp> | |
<Name>CoolApp.Server</Name> | |
<Server>APP2</Server> | |
</CoolApp> | |
<CoolApp> | |
<Name>CoolApp.Server</Name> | |
<Server>APP3</Server> | |
</CoolApp> | |
<WebServer> | |
<Name>nginx</Name> | |
<Server>WEB1</Server> | |
</WebServer> | |
<WebServer> | |
<Name>nginx</Name> | |
<Server>WEB2</Server> | |
</WebServer> | |
<WebServer> | |
<Name>nginx</Name> | |
<Server>WEB3</Server> | |
</WebServer> | |
</Service> | |
</PROD> | |
</Environment> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment