mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2026-07-16 00:40:39 +00:00
45 lines
1.6 KiB
PowerShell
45 lines
1.6 KiB
PowerShell
Function Set-WinUtilService {
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
Changes the startup type of the given service
|
|
|
|
.PARAMETER Name
|
|
The name of the service to modify
|
|
|
|
.PARAMETER StartupType
|
|
The startup type to set the service to
|
|
|
|
.EXAMPLE
|
|
Set-WinUtilService -Name "HomeGroupListener" -StartupType "Manual"
|
|
|
|
#>
|
|
param (
|
|
$Name,
|
|
$StartupType
|
|
)
|
|
try {
|
|
Write-Host "Setting Service $Name to $StartupType"
|
|
Write-WinUtilLog -Component "Service" -Message "Setting service $Name startup type to $StartupType"
|
|
|
|
# Check if the service exists
|
|
$service = Get-Service -Name $Name -ErrorAction Stop
|
|
|
|
# Service exists, proceed with changing properties -- while handling auto delayed start for PWSH 5
|
|
if (($PSVersionTable.PSVersion.Major -lt 7) -and ($StartupType -eq "AutomaticDelayedStart")) {
|
|
sc.exe config $Name start=delayed-auto
|
|
} else {
|
|
$service | Set-Service -StartupType $StartupType -ErrorAction Stop
|
|
}
|
|
Write-WinUtilLog -Component "Service" -Message "Service $Name startup type set to $StartupType"
|
|
} catch [System.ServiceProcess.ServiceNotFoundException] {
|
|
Write-Warning "Service $Name was not found."
|
|
Write-WinUtilLog -Level "WARN" -Component "Service" -Message "Service $Name was not found."
|
|
} catch {
|
|
Write-Warning "Unable to set $Name due to unhandled exception."
|
|
Write-Warning $_.Exception.Message
|
|
Write-WinUtilLog -Level "ERROR" -Component "Service" -Message "Unable to set service $Name to $StartupType`: $($_.Exception.Message)"
|
|
}
|
|
|
|
}
|