Files
winutil/functions/private/Set-WinUtilService.ps1
Chris Titus 2cb20893fc Expand Pester coverage across install, tweaks, and UI workflows (#4792)
* Harden Pester CI and discard runspace return values

* Add prioritized test backlog

* Add WinUtil action logging

* Add config integrity tests

* Add XAML control wiring tests

* Add registry and service helper tests

* Add package manager tests

* Add runspace behavior tests

* Add tweak orchestration tests

* Add install workflow tests

* Add update profile tests

* Add AppX removal tests

* Log package installer output

* Add UI state helper tests

* Pin Pester test runner version

* Expand Win11 Creator tests

* Add preferences and theme tests

* Add search filter tests

* Add compile contract tests

* Use single WinUtil session log

* Remove installer output logging helper

* Handle locked WinUtil transcript log

* Avoid appending to active transcript log

* Log install uninstall package identities
2026-07-01 21:49:15 -05:00

53 lines
2.0 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
if (($service.PSObject.Properties.Name -contains "StartType") -and ([string]$service.StartType -eq [string]$StartupType) ) {
Write-Host "Service $Name is already set to $StartupType"
Write-WinUtilLog -Component "Service" -Message "Service $Name startup type is already $StartupType; no change needed."
return
}
# 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 {
if ($_.FullyQualifiedErrorId -like "NoServiceFoundForGivenName,*") {
Write-Warning "Service $Name was not found."
Write-WinUtilLog -Level "WARN" -Component "Service" -Message "Service $Name was not found."
} else {
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)"
}
}
}