Skip to content

Commit c04e77f

Browse files
(MODULES-9230) Force Windows Service Action (puppetlabs#99)
(MODULES-9230) Force Windows Service Action
2 parents b49c561 + 8bf24d3 commit c04e77f

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

tasks/init.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
"description": "The name of the service to operate on.",
1111
"type": "String[1]"
1212
},
13+
"force": {
14+
"description": "Force a Windows service to restart even if it has dependent services. This parameter is passed for Windows services only.",
15+
"type": "Optional[Boolean]"
16+
},
1317
"provider": {
1418
"description": "The provider to use to manage or inspect the service, defaults to the system service manager. Only used when the 'puppet-agent' feature is available on the target so we can leverage Puppet.",
1519
"type": "Optional[String[1]]"

tasks/windows.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
"name": {
1111
"description": "The short name of the Windows service to operate on.",
1212
"type": "String[1]"
13+
},
14+
"force": {
15+
"description": "Force the service to stop or restart, even if other services depend on it",
16+
"type": "Optional[Boolean]"
1317
}
1418
}
1519
}

tasks/windows.ps1

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ param(
77

88
[Parameter(Mandatory = $true)]
99
[String]
10-
$Action
10+
$Action,
11+
12+
[Parameter(Mandatory = $false)]
13+
[Switch]
14+
$Force
1115
)
1216

1317
function ErrorMessage($Action, $Name, $Message)
@@ -36,7 +40,7 @@ function ValidateParams
3640

3741
$ErrorActionPreference = 'Stop'
3842

39-
function Invoke-ServiceAction($Service, $Action)
43+
function Invoke-ServiceAction($Service, $Action, $Force)
4044
{
4145
$inSyncStatus = 'in_sync'
4246
$status = $null
@@ -51,11 +55,11 @@ function Invoke-ServiceAction($Service, $Action)
5155
'stop'
5256
{
5357
if ($Service.Status -eq 'Stopped') { $status = $InSyncStatus }
54-
else { Stop-Service -inputObject $Service }
58+
else { Stop-Service -inputObject $Service -Force:$Force }
5559
}
5660
'restart'
5761
{
58-
Restart-Service -inputObject $Service
62+
Restart-Service -inputObject $Service -Force:$Force
5963
$status = 'Restarted'
6064
}
6165
# no-op since status always returned
@@ -80,7 +84,7 @@ try
8084
{
8185
ValidateParams -Action $action
8286
$service = Get-Service -Name $Name
83-
$status = Invoke-ServiceAction -Service $service -Action $action
87+
$status = Invoke-ServiceAction -Service $service -Action $action -Force:$Force
8488

8589
# TODO: could use ConvertTo-Json, but that requires PS3
8690
# if embedding in literal, should make sure Name / Status doesn't need escaping

0 commit comments

Comments
 (0)