50 lines
2.0 KiB
PowerShell
50 lines
2.0 KiB
PowerShell
function Enable-Service {
|
|
<#
|
|
.SYNOPSIS
|
|
Sets a Service to Manual Start Type. Sets Tier-0 Services to Automatic-Delayed Start Type.
|
|
|
|
.NOTES
|
|
Service must be started separately
|
|
#>
|
|
|
|
[CmdletBinding()]
|
|
param(
|
|
[Alias("ServiceObject")]
|
|
[Parameter(Mandatory=$true)]
|
|
[object]$service
|
|
)
|
|
|
|
$logLead = (Get-LogLeadName);
|
|
$tier0Services = Get-ServicesByTier -Tier 0
|
|
# Patching jobs don't pass in services, they pass in CimInstances from Get-ChocolateyServices. That breaks.
|
|
# So instead, we take any object that gets sent in and get the service that has that name.
|
|
# If the service object we get doesn't have a name, then how was any of this supposed to work anyways?
|
|
$service = Get-Service $service.Name
|
|
|
|
if (($tier0Services -NotContains $service.Name -and $service.StartType -eq "Manual") -or
|
|
($tier0Services -Contains $service.Name -and $service.StartType -eq "Automatic")) {
|
|
Write-Host "$logLead : Service is already enabled, no action necessary"
|
|
return
|
|
}
|
|
|
|
Write-Host ("$logLead : Preparing to enable service '{0}'." -f $service.Name)
|
|
Write-Verbose ("$logLead : Status before changes:{0}" -f ($service | Select-Object Name, StartType, Status))
|
|
|
|
# Tier 0 is hard coded to Automatic - Delayed -- SRE-17490
|
|
if($tier0Services -Contains $service.Name){
|
|
Write-Host ("$logLead : Service '{0}' is Tier0. Setting to Automatic-Delayed." -f $service.Name)
|
|
$params = @("config", $service.Name, "start=delayed-auto")
|
|
|
|
Invoke-SCExe $params
|
|
} else {
|
|
Set-Service -Name $service.Name -StartupType Manual
|
|
}
|
|
$service = Get-Service $service.Name
|
|
|
|
if (($tier0Services -NotContains $service.Name -and $service.StartType -ne "Manual") -or
|
|
($tier0Services -Contains $service.Name -and $service.StartType -ne "Automatic")) {
|
|
Write-Warning "$logLead : Unable to enable the $service.Name Service"
|
|
} else {
|
|
Write-Host ("$logLead : Service enabled. Status after changes: {0}" -f ($service | Select-Object Name, StartType, Status))
|
|
}
|
|
} |