From daad6d5d9a43124f3d1ab38a63d06c91e51aaf6e Mon Sep 17 00:00:00 2001 From: Pascal Ott Date: Wed, 17 Jun 2020 20:52:04 +0200 Subject: [PATCH] moved stopping service to new function --- .../DeployWindowsService.ps1 | 27 +--------- .../WindowsServiceManagerV4/utility.ps1 | 51 ++++++++++++++++++- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/WindowsServiceManager/WindowsServiceManagerV4/DeployWindowsService.ps1 b/WindowsServiceManager/WindowsServiceManagerV4/DeployWindowsService.ps1 index ea74524..e207de8 100644 --- a/WindowsServiceManager/WindowsServiceManagerV4/DeployWindowsService.ps1 +++ b/WindowsServiceManager/WindowsServiceManagerV4/DeployWindowsService.ps1 @@ -173,32 +173,7 @@ $scriptBlock = { } } elseif ($serviceObject) { - if ($serviceObject.State -eq 'Running') { - $stopServiceTimer = [Diagnostics.Stopwatch]::StartNew() - Write-Output "[$env:ComputerName]: Stopping [$ServiceName]" - do { - $serviceObject = Get-WindowsService -ServiceName $ServiceName - $results = $serviceObject.StopService() - - if ($stopServiceTimer.Elapsed.TotalSeconds -gt $Timeout) { - if ($StopProcess) { - Write-Verbose "[$env:ComputerName]: [$ServiceName] did not respond within [$Timeout] seconds, stopping process." - $allProcesses = Get-Process - $process = $allProcesses | Where-Object { $_.Path -like "$parentPath\*" } - if ($process) { - Write-Warning -Message "[$env:ComputerName]: Files are still in use by [$($process.ProcessName)], stopping the process!" - $process | Stop-Process -Force -ErrorAction SilentlyContinue - } - } - else { - return Write-Error -Message "[$env:ComputerName]: [$ServiceName] did not respond within [$Timeout] seconds." - } - } - $serviceObject = Get-WindowsService -ServiceName $ServiceName - } - while ($serviceObject.State -ne 'Stopped') - } - + $serviceObject = Stop-WindowsService -ServiceName $ServiceName -Timeout $Timeout -StopProcess:$StopProcess $parentPath = Get-FullExecuteablePath -StringContainingPath $serviceObject.PathName -JustParentPath Write-Output "[$env:ComputerName]: Identified [$ServiceName] installation directory [$parentPath]" diff --git a/WindowsServiceManager/WindowsServiceManagerV4/utility.ps1 b/WindowsServiceManager/WindowsServiceManagerV4/utility.ps1 index 6ebcb53..974b531 100644 --- a/WindowsServiceManager/WindowsServiceManagerV4/utility.ps1 +++ b/WindowsServiceManager/WindowsServiceManagerV4/utility.ps1 @@ -37,6 +37,55 @@ function Start-WindowsService { } } +function Stop-WindowsService { + param + ( + [string] + $ServiceName, + + [int] + $Timeout = 30, + + [switch] + $StopProcess = $false + ) + + $serviceObject = Get-WindowsService -ServiceName $ServiceName + + if ($serviceObject.State -eq 'Running') { + $stopServiceTimer = [Diagnostics.Stopwatch]::StartNew() + Write-Output "[$env:ComputerName]: Stopping Service [$ServiceName]" + do { + $serviceObject = Get-WindowsService -ServiceName $ServiceName + $results = $serviceObject.StopService() + + if ($stopServiceTimer.Elapsed.TotalSeconds -gt $Timeout) { + if ($StopProcess) { + Write-Verbose "[$env:ComputerName]: [$ServiceName] did not respond within [$Timeout] seconds, stopping process." + + $fullPath = Get-FullExecuteablePath -StringContainingPath $serviceObject.PathName + + $allProcesses = Get-Process + $process = $allProcesses | Where-Object { $_.Path -like "$parentPath\*" } + if ($process) { + Write-Warning -Message "[$env:ComputerName]: Files are still in use by [$($process.ProcessName)], stopping the process!" + $process | Stop-Process -Force -ErrorAction SilentlyContinue + } + } + else { + return Write-Error -Message "[$env:ComputerName]: [$ServiceName] did not respond within [$Timeout] seconds." + } + } + $serviceObject = Get-WindowsService -ServiceName $ServiceName + } + while ($serviceObject.State -ne 'Stopped') + + Write-Output "[$env:ComputerName]: Stopped Service [$ServiceName]" + + return $serviceObject + } +} + function Get-FullExecuteablePath { [CmdletBinding()] param ( @@ -62,4 +111,4 @@ function Get-FullExecuteablePath { } return $matchedPath -} \ No newline at end of file +}