2023-03-07 12:28:00 -08:00
|
|
|
Function Set-WinUtilService {
|
|
|
|
|
<#
|
|
|
|
|
|
2023-10-19 17:12:55 -05:00
|
|
|
.SYNOPSIS
|
|
|
|
|
Changes the startup type of the given service
|
2023-03-07 12:28:00 -08:00
|
|
|
|
2023-10-19 17:12:55 -05:00
|
|
|
.PARAMETER Name
|
|
|
|
|
The name of the service to modify
|
|
|
|
|
|
|
|
|
|
.PARAMETER StartupType
|
|
|
|
|
The startup type to set the service to
|
|
|
|
|
|
|
|
|
|
.EXAMPLE
|
2023-03-07 12:28:00 -08:00
|
|
|
Set-WinUtilService -Name "HomeGroupListener" -StartupType "Manual"
|
2023-10-19 17:12:55 -05:00
|
|
|
|
|
|
|
|
#>
|
2023-03-07 12:28:00 -08:00
|
|
|
param (
|
|
|
|
|
$Name,
|
|
|
|
|
$StartupType
|
|
|
|
|
)
|
2023-07-15 10:42:11 -05:00
|
|
|
try {
|
|
|
|
|
Write-Host "Setting Service $Name to $StartupType"
|
2023-10-19 17:12:55 -05:00
|
|
|
|
2023-07-15 10:42:11 -05:00
|
|
|
# Check if the service exists
|
|
|
|
|
$service = Get-Service -Name $Name -ErrorAction Stop
|
2023-10-19 17:12:55 -05:00
|
|
|
|
2025-12-01 21:29:27 +01:00
|
|
|
# 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
|
|
|
|
|
}
|
2024-08-06 23:35:17 +03:00
|
|
|
} catch [System.ServiceProcess.ServiceNotFoundException] {
|
2026-03-26 21:26:21 +01:00
|
|
|
Write-Warning "Service $Name was not found."
|
2024-08-06 23:35:17 +03:00
|
|
|
} catch {
|
2026-03-26 21:26:21 +01:00
|
|
|
Write-Warning "Unable to set $Name due to unhandled exception."
|
2023-07-15 10:42:11 -05:00
|
|
|
Write-Warning $_.Exception.Message
|
2023-03-07 12:28:00 -08:00
|
|
|
}
|
2023-10-19 17:12:55 -05:00
|
|
|
|
2023-03-07 12:28:00 -08:00
|
|
|
}
|