[Tweaks] Conditionally determine when to use sc and Set-Service (#3761)

* [Tweaks] Conditionally determine when to use sc and Set-Service

* Clean up comments in Set-WinUtilService.ps1

Removed comments explaining the handling of auto delayed start for PWSH 5.

---------

Co-authored-by: Chris Titus <contact@christitus.com>
This commit is contained in:
CodingWonders 2025-12-01 21:29:27 +01:00 committed by GitHub
parent becfba603d
commit cfaf139a6a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -24,8 +24,12 @@ Function Set-WinUtilService {
# Check if the service exists # Check if the service exists
$service = Get-Service -Name $Name -ErrorAction Stop $service = Get-Service -Name $Name -ErrorAction Stop
# Service exists, proceed with changing properties # Service exists, proceed with changing properties -- while handling auto delayed start for PWSH 5
$service | Set-Service -StartupType $StartupType -ErrorAction Stop 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
}
} catch [System.ServiceProcess.ServiceNotFoundException] { } catch [System.ServiceProcess.ServiceNotFoundException] {
Write-Warning "Service $Name was not found" Write-Warning "Service $Name was not found"
} catch { } catch {