Just wanted to provide the entire code used in case someone else is looking for something similar - thanks to everyone for throwing ideas into the mix it was very helpful.
#Define Services
$Service1 = 'servicename'
$Service2 =
$Service3 =
$Service4 =
$Service5 =
$Service6 =
$Service7 =
$Service8 =
$Service9 =
$services = @(
$Service1,
$service2,
$service3,
$service4,
$Service5,
$Service6,
$Service7,
$Service8,
$Service9)
#Stop Services
Get-service |
Where { $Services -Contains $_.Name} |
Foreach {
$_ | stop-service
}
Set-Service |
Where { $Services -Contains $_.Name} |
Foreach {
$_ | -startuptype "Disabled"
}
#Verify Services
Get-service |
Where { $Services -Contains $_.Name} |
Foreach {
if ((Get-Service $_.Name).Status -eq "stopped") {Write-Host 'Service Start Pass (0)'} else {Write-Host 'Start Fail (1000)';Exit '1000'}
}
#Start Services
Set-Service |
Where { $Services -Contains $_.Name} |
Foreach {
$_ | -startuptype "Automatic"
}
Get-service |
Where { $Services -Contains $_.Name} |
Foreach {
$_ | start-service
}
#Verify Services
Get-service |
Where { $Services -Contains $_.Name} |
Foreach {
if ((Get-Service $_.Name).Status -eq "running") {Write-Host 'Service Start Pass (0)'} else {Write-Host 'Start Fail (2000)';Exit '2000'}
}
This allows me to have someone list any services they need to stop / start to deploy custom code out there - also then I will be moving this to stop executables in order to replace them and other files for verification. Sadly we are handed a collection of exe's and dll's to just hot swap for some deployments of software so this is why this was needed. I wanted a way of defining what I need to move in and out per deployment but didn't want to comment out lines throughout the script that I didn't need (i.e. only had two services so needed to comment out the others throughout the script).