Skip to content

Commit 43c1bec

Browse files
committed
Update Invoke-ServiceManager
rename, align help info, bump, etc.
1 parent 85a7d22 commit 43c1bec

File tree

1 file changed

+42
-41
lines changed

1 file changed

+42
-41
lines changed

ps-services/Manage-Service.ps1 renamed to ps-services/Invoke-ServiceManager.ps1

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,49 @@
1-
Function Manage-Service {
1+
Function Invoke-ServiceManager {
22
<#
33
.SYNOPSIS
4-
Perform various operations on Windows services.
4+
Manages Windows services, allowing various actions such as starting, stopping, restarting, and configuring service properties.
55
66
.DESCRIPTION
7-
This function allows you to perform different operations on Windows services, such as starting, stopping, restarting, pausing, continuing, enabling or disabling auto-start, setting recovery options, retrieving service description and dependencies, and more.
8-
7+
This function provides a range of functionalities to manage Windows services, including starting, stopping, restarting, pausing, continuing, enabling auto-start, disabling auto-start, setting recovery options, retrieving service information, and more.
8+
99
.PARAMETER ServiceName
10-
Mandatory - specifies the name of the Windows service to be managed.
10+
Specifies the name of the service to manage.
1111
.PARAMETER Action
12-
Mandatory - specifies the action to perform on the service.
12+
Action to perform, valid actions include: Start, Stop, Restart, Status, Pause, Continue, EnableAutoStart, DisableAutoStart, SetRecovery, GetDescription, GetDependencies, GetLogOnAccount, SetLogOnAccount, GetDisplayName, SetDisplayName, GetStartupType, GetServiceAccountInfo, ListRunningServices, SetRecoveryActions.
1313
.PARAMETER Force
14-
NotMandatory - forces the specified action, even if it may result in unintended consequences.
14+
Forces the action to be performed without prompting for confirmation.
1515
.PARAMETER WhatIf
16-
NotMandatory - simulates the action without actually performing it, providing a preview of the outcome.
16+
Simulates the action without actually performing it.
1717
.PARAMETER RetryCount
18-
NotMandatory - the number of times to retry the action in case of failure.
18+
Specifies the number of times to retry the action on failure.
1919
.PARAMETER RetryDelay
20-
NotMandatory - specifies the delay in seconds between retry attempts.
20+
Specifies the delay in seconds between retry attempts.
2121
.PARAMETER IncludeLogs
22-
NotMandatory - includes detailed action logs in the output.
22+
Specifies whether to include detailed action logs.
2323
.PARAMETER RestartDelay
24-
NotMandatory - delay in seconds before restarting a service during recovery.
24+
Specifies the delay in seconds before restarting a service during recovery.
2525
.PARAMETER RecoveryAttempts
26-
NotMandatory - the number of recovery attempts during service recovery.
26+
Specifies the number of recovery attempts during service recovery.
2727
.PARAMETER RecoveryDelay
28-
NotMandatory - the delay in seconds between recovery attempts.
28+
Specifies the delay in seconds between recovery attempts.
29+
.PARAMETER LogOnAccount
30+
Specifies the account under which the service runs.
2931
.PARAMETER DisplayName
30-
NotMandatory - display name of the Windows service, use this parameter to provide a user-friendly name for the service that is different from its actual name.
32+
Specifies the display name of the service.
3133
.PARAMETER RecoveryActions
32-
NotMandatory - specifies the recovery actions to be configured for the service during recovery.
34+
Specifies specific recovery actions for the service.
3335
.PARAMETER Command
34-
NotMandatory - command to be executed during recovery if the specified recovery actions are triggered.
36+
Specifies the command to execute during recovery.
3537
3638
.EXAMPLE
37-
Manage-Service -ServiceName "wuauserv" -Action Start
38-
Manage-Service -ServiceName "wuauserv" -Action SetLogOnAccount -LogOnAccount "NT AUTHORITY\LocalService"
39-
Manage-Service -ServiceName "wuauserv" -Action GetStartupType
40-
Manage-Service -ServiceName "wuauserv" -Action GetServiceAccountInfo
41-
Manage-Service -ServiceName "wuauserv" -Action SetRecoveryActions -RecoveryActions "RestartService" -Command "Restart-Service -Name 'wuauserv'"
39+
Invoke-ServiceManager -ServiceName "wuauserv" -Action Start
40+
Invoke-ServiceManager -ServiceName "wuauserv" -Action SetLogOnAccount -LogOnAccount "NT AUTHORITY\LocalService"
41+
Invoke-ServiceManager -ServiceName "wuauserv" -Action GetStartupType
42+
Invoke-ServiceManager -ServiceName "wuauserv" -Action GetServiceAccountInfo
43+
Invoke-ServiceManager -ServiceName "wuauserv" -Action SetRecoveryActions -RecoveryActions "RestartService" -Command "Restart-Service -Name 'wuauserv'"
4244
4345
.NOTES
44-
v0.0.7
46+
v0.0.9
4547
#>
4648
[CmdletBinding()]
4749
param (
@@ -95,7 +97,7 @@ Function Manage-Service {
9597
switch ($Action) {
9698
"Start" {
9799
if ($WhatIf) {
98-
Write-Output "Simulating: Would start service '$ServiceName'."
100+
Write-Output "Simulating: Would start service '$ServiceName'"
99101
}
100102
else {
101103
$Service | Start-Service -Verbose
@@ -104,7 +106,7 @@ Function Manage-Service {
104106
}
105107
"Stop" {
106108
if ($WhatIf) {
107-
Write-Output "Simulating: Would stop service '$ServiceName'."
109+
Write-Output "Simulating: Would stop service '$ServiceName'"
108110
}
109111
else {
110112
$Service | Stop-Service -Verbose
@@ -113,7 +115,7 @@ Function Manage-Service {
113115
}
114116
"Restart" {
115117
if ($WhatIf) {
116-
Write-Output "Simulating: Would restart service '$ServiceName'."
118+
Write-Output "Simulating: Would restart service '$ServiceName'"
117119
}
118120
else {
119121
$Service | Restart-Service -Verbose
@@ -126,7 +128,7 @@ Function Manage-Service {
126128
}
127129
"Pause" {
128130
if ($WhatIf) {
129-
Write-Output "Simulating: Would pause service '$ServiceName'."
131+
Write-Output "Simulating: Would pause service '$ServiceName'"
130132
}
131133
else {
132134
$Service | Suspend-Service -Verbose
@@ -135,7 +137,7 @@ Function Manage-Service {
135137
}
136138
"Continue" {
137139
if ($WhatIf) {
138-
Write-Output "Simulating: Would continue service '$ServiceName'."
140+
Write-Output "Simulating: Would continue service '$ServiceName'"
139141
}
140142
else {
141143
$Service | Resume-Service -Verbose
@@ -144,30 +146,30 @@ Function Manage-Service {
144146
}
145147
"EnableAutoStart" {
146148
if ($WhatIf) {
147-
Write-Output "Simulating: Would enable auto-start for service '$ServiceName'."
149+
Write-Output "Simulating: Would enable auto-start for service '$ServiceName'"
148150
}
149151
else {
150152
Set-Service -Name $ServiceName -StartupType Automatic -Verbose
151-
Write-Output "Auto-start has been enabled for service '$ServiceName'."
153+
Write-Output "Auto-start has been enabled for service '$ServiceName'"
152154
}
153155
}
154156
"DisableAutoStart" {
155157
if ($WhatIf) {
156-
Write-Output "Simulating: Would disable auto-start for service '$ServiceName'."
158+
Write-Output "Simulating: Would disable auto-start for service '$ServiceName'"
157159
}
158160
else {
159161
Set-Service -Name $ServiceName -StartupType Disabled -Verbose
160-
Write-Output "Auto-start has been disabled for service '$ServiceName'."
162+
Write-Output "Auto-start has been disabled for service '$ServiceName'"
161163
}
162164
}
163165
"SetRecovery" {
164166
if ($WhatIf) {
165-
Write-Output "Simulating: Would set recovery options for service '$ServiceName'."
167+
Write-Output "Simulating: Would set recovery options for service '$ServiceName'"
166168
}
167169
else {
168170
$RecoveryOptions = New-ServiceRecoveryOptions -RestartService -ResetPeriod $RestartDelay -DaysToRestart $RecoveryAttempts -RestartWait $RecoveryDelay
169171
Set-Service -Name $ServiceName -Recovery $RecoveryOptions -Verbose
170-
Write-Output "Recovery options have been set for service '$ServiceName'."
172+
Write-Output "Recovery options have been set for service '$ServiceName'"
171173
}
172174
}
173175
"GetDescription" {
@@ -185,11 +187,11 @@ Function Manage-Service {
185187
}
186188
"SetLogOnAccount" {
187189
if ($WhatIf) {
188-
Write-Output "Simulating: Would set log-on account for service '$ServiceName' to '$LogOnAccount'."
190+
Write-Output "Simulating: Would set log-on account for service '$ServiceName' to '$LogOnAccount'"
189191
}
190192
else {
191193
$Service | Set-ServiceLogOnAccount -Account $LogOnAccount -Verbose
192-
Write-Output "Log-on account for service '$ServiceName' has been set to '$LogOnAccount'."
194+
Write-Output "Log-on account for service '$ServiceName' has been set to '$LogOnAccount'"
193195
}
194196
}
195197
"GetDisplayName" {
@@ -198,11 +200,11 @@ Function Manage-Service {
198200
}
199201
"SetDisplayName" {
200202
if ($WhatIf) {
201-
Write-Output "Simulating: Would set display name for service '$ServiceName' to '$DisplayName'."
203+
Write-Output "Simulating: Would set display name for service '$ServiceName' to '$DisplayName'"
202204
}
203205
else {
204206
$Service | Set-Service -DisplayName $DisplayName -Verbose
205-
Write-Output "Display name for service '$ServiceName' has been set to '$DisplayName'."
207+
Write-Output "Display name for service '$ServiceName' has been set to '$DisplayName'"
206208
}
207209
}
208210
"GetStartupType" {
@@ -227,7 +229,7 @@ Function Manage-Service {
227229
}
228230
"SetRecoveryActions" {
229231
if ($WhatIf) {
230-
Write-Output "Simulating: Would set recovery actions for service '$ServiceName'."
232+
Write-Output "Simulating: Would set recovery actions for service '$ServiceName'"
231233
}
232234
else {
233235
$RecoveryOptions = New-ServiceRecoveryOptions -Action $RecoveryActions -Command $Command
@@ -242,8 +244,7 @@ Function Manage-Service {
242244
}
243245
}
244246
catch {
245-
$ErrorMessage = "An error occurred: $_"
246-
Write-Error -Message $ErrorMessage
247+
Write-Error -Message "An error occurred: $_"
247248
$ErrorMessage | Out-File -Append -FilePath $LogFilePath
248249
}
249250
}

0 commit comments

Comments
 (0)