Skip to content

Commit cfaf139

Browse files
[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 <[email protected]>
1 parent becfba6 commit cfaf139

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

functions/private/Set-WinUtilService.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ Function Set-WinUtilService {
2424
# Check if the service exists
2525
$service = Get-Service -Name $Name -ErrorAction Stop
2626

27-
# Service exists, proceed with changing properties
28-
$service | Set-Service -StartupType $StartupType -ErrorAction Stop
27+
# Service exists, proceed with changing properties -- while handling auto delayed start for PWSH 5
28+
if (($PSVersionTable.PSVersion.Major -lt 7) -and ($StartupType -eq "AutomaticDelayedStart")) {
29+
sc.exe config $Name start=delayed-auto
30+
} else {
31+
$service | Set-Service -StartupType $StartupType -ErrorAction Stop
32+
}
2933
} catch [System.ServiceProcess.ServiceNotFoundException] {
3034
Write-Warning "Service $Name was not found"
3135
} catch {

0 commit comments

Comments
 (0)