From 31f50692126518c1271cfabaae1d3672eff7a249 Mon Sep 17 00:00:00 2001 From: Chris Hoffman Date: Sat, 21 Jun 2014 21:58:26 -0400 Subject: [PATCH 1/3] Using generic windows service for example --- library/windows/win_service | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/windows/win_service b/library/windows/win_service index d26ee0840b2..c378be120b1 100644 --- a/library/windows/win_service +++ b/library/windows/win_service @@ -61,12 +61,12 @@ author: Chris Hoffman EXAMPLES = ''' # Restart a service win_service: - name: ncover + name: spooler state: restarted # Set service startup mode to auto and ensure it is started win_service: - name: ncover + name: spooler start_mode: auto state: started ''' From 844e90093b8b820a6fd2f71dc0cfca1de56c2010 Mon Sep 17 00:00:00 2001 From: Chris Hoffman Date: Sat, 21 Jun 2014 21:59:53 -0400 Subject: [PATCH 2/3] Better propagation of cmdlet errors --- library/windows/win_service.ps1 | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/library/windows/win_service.ps1 b/library/windows/win_service.ps1 index 535707d329d..bed140e7ffe 100644 --- a/library/windows/win_service.ps1 +++ b/library/windows/win_service.ps1 @@ -46,7 +46,7 @@ If ($params.start_mode) { $svcName = $params.name $svc = Get-Service -Name $svcName -ErrorAction SilentlyContinue If (-not $svc) { - Fail-Json $result "Service not installed" + Fail-Json $result "Service '$svcName' not installed" } If ($startMode) { @@ -60,15 +60,30 @@ If ($startMode) { If ($state) { 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; } 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; } 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; } } From 1c2d244964fea876740a6d2998b193f0c227161d Mon Sep 17 00:00:00 2001 From: Chris Hoffman Date: Sat, 21 Jun 2014 22:01:02 -0400 Subject: [PATCH 3/3] Fixing bug with stopped state --- library/windows/win_service.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/windows/win_service.ps1 b/library/windows/win_service.ps1 index bed140e7ffe..70a3aa0005c 100644 --- a/library/windows/win_service.ps1 +++ b/library/windows/win_service.ps1 @@ -68,7 +68,7 @@ If ($state) { } Set-Attr $result "changed" $true; } - ElseIf ($state -eq "stopped" -and $svcName -ne "Stopped") { + ElseIf ($state -eq "stopped" -and $svc.Status -ne "Stopped") { try { Stop-Service -Name $svcName -ErrorAction Stop }