Merge pull request #7886 from chrishoffman/win_service
Bugfix and better error propagation for win_service module
This commit is contained in:
commit
558f4d7b1a
2 changed files with 22 additions and 7 deletions
|
@ -61,12 +61,12 @@ author: Chris Hoffman
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
# Restart a service
|
# Restart a service
|
||||||
win_service:
|
win_service:
|
||||||
name: ncover
|
name: spooler
|
||||||
state: restarted
|
state: restarted
|
||||||
|
|
||||||
# Set service startup mode to auto and ensure it is started
|
# Set service startup mode to auto and ensure it is started
|
||||||
win_service:
|
win_service:
|
||||||
name: ncover
|
name: spooler
|
||||||
start_mode: auto
|
start_mode: auto
|
||||||
state: started
|
state: started
|
||||||
'''
|
'''
|
||||||
|
|
|
@ -46,7 +46,7 @@ If ($params.start_mode) {
|
||||||
$svcName = $params.name
|
$svcName = $params.name
|
||||||
$svc = Get-Service -Name $svcName -ErrorAction SilentlyContinue
|
$svc = Get-Service -Name $svcName -ErrorAction SilentlyContinue
|
||||||
If (-not $svc) {
|
If (-not $svc) {
|
||||||
Fail-Json $result "Service not installed"
|
Fail-Json $result "Service '$svcName' not installed"
|
||||||
}
|
}
|
||||||
|
|
||||||
If ($startMode) {
|
If ($startMode) {
|
||||||
|
@ -60,15 +60,30 @@ If ($startMode) {
|
||||||
|
|
||||||
If ($state) {
|
If ($state) {
|
||||||
If ($state -eq "started" -and $svc.Status -ne "Running") {
|
If ($state -eq "started" -and $svc.Status -ne "Running") {
|
||||||
Start-Service -Name $svcName
|
try {
|
||||||
|
Start-Service -Name $svcName -ErrorAction Stop
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
Fail-Json $result $_.Exception.Message
|
||||||
|
}
|
||||||
Set-Attr $result "changed" $true;
|
Set-Attr $result "changed" $true;
|
||||||
}
|
}
|
||||||
ElseIf ($state -eq "stopped" -and $svcName -ne "Stopped") {
|
ElseIf ($state -eq "stopped" -and $svc.Status -ne "Stopped") {
|
||||||
Stop-Service -Name $svcName
|
try {
|
||||||
|
Stop-Service -Name $svcName -ErrorAction Stop
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
Fail-Json $result $_.Exception.Message
|
||||||
|
}
|
||||||
Set-Attr $result "changed" $true;
|
Set-Attr $result "changed" $true;
|
||||||
}
|
}
|
||||||
ElseIf ($state -eq "restarted") {
|
ElseIf ($state -eq "restarted") {
|
||||||
Restart-Service -Name $svcName
|
try {
|
||||||
|
Restart-Service -Name $svcName -ErrorAction Stop
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
Fail-Json $result $_.Exception.Message
|
||||||
|
}
|
||||||
Set-Attr $result "changed" $true;
|
Set-Attr $result "changed" $true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue