-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet-ServiceStartupType.ps1
More file actions
56 lines (47 loc) · 2.3 KB
/
Copy pathSet-ServiceStartupType.ps1
File metadata and controls
56 lines (47 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<#
.SYNOPSIS
Setzt den Starttyp eines Dienstes, inklusive 'Automatisch (verzoegert)'.
.DESCRIPTION
Set-Service kennt den verzoegerten Autostart nicht - dafuer braucht es
`sc.exe config ... start= delayed-auto`. Diese Funktion kapselt beides
hinter einer Schnittstelle und akzeptiert Kurzformen: m, a, ad, d.
.NOTES
Erfordert administrative Rechte. Der Starttyp sagt nichts ueber den
aktuellen Zustand - ein auf Disabled gesetzter Dienst laeuft weiter, bis
er beendet wird.
#>
function Set-ServiceStartupType {
param (
[string]$ServiceName,
[ValidateSet('Manual', 'Automatic', 'AutomaticDelayedStart', 'Disabled', 'm', 'a', 'ad', 'd')]
[string]$StartupType
)
# Mapping of abbreviations to full names
switch ($StartupType.ToLower()) {
'm' { $startMode = 'demand' }
'manual' { $startMode = 'demand' }
'a' { $startMode = 'auto' }
'automatic' { $startMode = 'auto' }
'ad' { $startMode = 'delayed-auto' }
'automaticdelayedstart' { $startMode = 'delayed-auto' }
'd' { $startMode = 'disabled' }
'disabled' { $startMode = 'disabled' }
}
# Execute sc.exe and capture the output and exit code
$process = Start-Process "sc.exe" -ArgumentList "config $ServiceName start= $startMode" -NoNewWindow -Wait -PassThru
$exitCode = $process.ExitCode
# Return exit code
return $exitCode
}
# Usage examples:
# $exitCode = Set-ServiceStartupType -ServiceName "wuauserv" -StartupType "m"
# Write-Output "Exit Code: $exitCode"
# # Usage examples:
# Set-ServiceStartupType -ServiceName "wuauserv" -StartupType "m" # Set to Manual
# Set-ServiceStartupType -ServiceName "wuauserv" -StartupType "a" # Set to Automatic
# Set-ServiceStartupType -ServiceName "wuauserv" -StartupType "ad" # Set to Automatic (Delayed Start)
# Set-ServiceStartupType -ServiceName "wuauserv" -StartupType "d" # Set to Disabled
# Set-ServiceStartupType -ServiceName "wuauserv" -StartupType "manual" # Set to Manual
# Set-ServiceStartupType -ServiceName "wuauserv" -StartupType "automatic" # Set to Automatic
# Set-ServiceStartupType -ServiceName "wuauserv" -StartupType "automaticdelayedstart" # Set to Automatic (Delayed Start)
# Set-ServiceStartupType -ServiceName "wuauserv" -StartupType "disabled" # Set to Disabled