1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Better propagation of cmdlet errors

This commit is contained in:
Chris Hoffman 2014-06-21 21:59:53 -04:00
parent 31f5069212
commit 844e90093b

View file

@ -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 $svcName -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;
} }
} }